/*
          __
         / /_______________  ____  ___
        / __/ ___/ ___/ __ \/ __ \/ _ \
       / /_(__  ) /__/ /_/ / /_/ /  __/
       \__/____/\___/\____/ .___/\___/
                         /_/

    text4.c
    formatted input
*/

#include <tscope.h>

int main()
{

    // set colors and font in the usual way
    ts_scrsize(SIZE1);
    ts_font(COURIER, 12, BOLD);
    ts_bgcolor(WHITE);
    ts_fgcolor(BLUE);
    ts_fill(TRUE);
    ts_coordinates(STANDARD);

    // helper variable to position the scanf echo
    int w;

    // test variable that catches the return value of ts_scanf
    // return value: number of variables that have been read successfully
    int test;

    // ask the name of the participant
    char ppn[50];
    ppn[0] = '\0';
    do {
        ts_clrscr();
        w = ts_printf(20, 20, "Name: ");
        test = ts_scanf(20 + w, 20, "%s", ppn);
    } while (test != 1);

    // ask for the participants sex
    char mf = 0;
    do {
        ts_clrscr();
        w = ts_printf(20, 20, "Sex (m/f): ");
        test = ts_scanf(20 + w, 20, "%c", &mf);
    } while (test != 1 || (mf != 'm' && mf != 'f'));


    // ask for the participants handedness
    char lr = 0;
    do {
        ts_clrscr();
        w = ts_printf(20, 20, "Hand (l/r): ");
        test = ts_scanf(20 + w, 20, "%c", &lr);
    } while (test != 1 || (lr != 'l' && lr != 'r'));

    // age
    int age = 0;
    do {
        ts_clrscr();
        w = ts_printf(20, 20, "Age: ");
        test = ts_scanf(20 + w, 20, "%d", &age);
    } while (test != 1 || (age < 0 || age > 120));

    // print results on the screen
    ts_clrscr();
    char tmp1[20], tmp2[20];
    if (mf == 'm')
        sprintf(tmp1, "male");
    else
        sprintf(tmp1, "female");
    if (lr == 'l')
        sprintf(tmp2, "left handed");
    else
        sprintf(tmp2, "right handed");

    ts_printf(20, 20, "%s is %d years old,", ppn, age);
    ts_printf(20, 40, "%s and %s.", tmp1, tmp2);

    ts_button(SXMAX - 20, SYMAX - 20);
    return 0;
}

END_OF_MAIN();


top
Persoonlijke pagina Universiteit GentTscope
Allegro | Cygwin | Gcc
© See license.html for copyright information