Tscope5
textio04.c
////////////////////////////////////////////////////////////////////////////////
//
// __ ______
// / /_______________ ____ ___ / ____/
// / __/ ___/ ___/ __ \/ __ \/ _ \ /___ )
// / /_(__ ) /__/ /_/ / /_/ / __/ ____/ /
// \__/____/\___/\____/ .___/\___/ /_____/
// /_/
//
// textio04.c
// - Scanf example.
////////////////////////////////////////////////////////////////////////////////
#include <tscope5.h>
#include <stdio.h>
int main()
{
// open a display
ts5_set_coordinate_system(TS5_DISPLAY_COORDINATES);
ts5_set_text_alignment(TS5_ALIGN_LEFT);
// helper variable to position the scanf echo
double w;
// test variable that catches the return value of ts5_scanf
// return value: number of variables that have been read successfully
int test;
// ask the name of the participant
char subj[50];
subj[0] = '\0';
do {
w = ts5_printf(20.0, 20.0, "Name: ");
test = ts5_scanf(20.0 + w, 20.0, "%s", subj);
} while (test != 1);
// ask for the participants gender
char gender = 0;
do {
w = ts5_printf(20.0, 20.0, "Gender (m/f): ");
test = ts5_scanf(20.0 + w, 20.0, "%c", &gender);
} while (test != 1 || (gender != 'm' && gender != 'f'));
// ask for the participants handedness
char hand = 0;
do {
w = ts5_printf(20.0, 20.0, "Hand (l/r): ");
test = ts5_scanf(20.0 + w, 20.0, "%c", &hand);
} while (test != 1 || (hand != 'l' && hand != 'r'));
// ask for the participants age
int age = 0;
do {
w = ts5_printf(20.0, 20.0, "Age: ");
test = ts5_scanf(20.0 + w, 20.0, "%d", &age);
} while (test != 1 || (age<0 || age>120));
// print the results on the screen
char tmp1[20], tmp2[20];
if (gender == 'm') {
sprintf(tmp1, "male");
}
else {
sprintf(tmp1, "female");
}
if (hand == 'l') {
sprintf(tmp2, "left handed");
}
else {
sprintf(tmp2, "right handed");
}
ts5_printf(20.0, 20.0, "%s is %d years old,", subj, age);
ts5_printf(20.0, 40.0, "%s and %s.", tmp1, tmp2);
// wait for a click
ts5_draw_mouse_button(ax(0.9), ay(0.8));
return 0;
}