dimanche 13 décembre 2015

deleting an error msg if the input is wrong

do{
            border();
            gotoxy(5,  6); p("Vegetables Green :  [1] Spinach   [2] Romain Lettuce  [3] Mustard Greens");
            gotoxy(5,  8); p("Root Vegetables  :  [4] Carrot    [5] Potato          [6] Yam");
            gotoxy(5, 10); p("Others           :  [7] Asparagus [8] Brocolli        [9] Melon");
            gotoxy(25,12); p("[10] Strawberry ");
            gotoxy(5, 14); p("Choose your %s food for today's meals: ", day[x]);
            s("%d", &choice);

            if(!(choice >= 1 && choice <= 10)){
                gotoxy(5, 15); p("Invalid choice, please enter a number between 1 - 10.");
                getch();
                system("cls");
                }
        }while(!(choice >= 1 && choice <= 10));

        userServings[x] = choice - 1;

        do{

            gotoxy(5, 15); p("Will you cook the %s? (y/n) ", food[choice - 1]);
            s("%s", &yn);

            if(!(strcmp(yn, "y") == 0 || strcmp(yn, "n") == 0)){
            gotoxy(5, 16);  p("Error, please enter 'y' for yes and 'n' for no.");
                }

        }while(!(strcmp(yn, "y") == 0 || strcmp(yn, "n") == 0));

        if(!strcmp(yn, "y")){
            isCooked[x] = 1;
            gotoxy(5, 16); p("How much %s (in grams) will you cook?    ", food[choice - 1]);

            while(scanf("%f", &grams)==1){            
                if(grams>=20 && grams<=200){         
                    userGrams[x] = grams;            
                    break;                           
                }else{
                    gotoxy(5, 17); printf("Error, grams should not be less than 20g and not greater than 200g.\n");
                    gotoxy(5, 18); printf("How much %s (in grams) will you cook? ");
                }
            }

        }else{
            isCooked[x] = 0;
            gotoxy(5, 16); p("How much %s (in grams) will you eat raw? ", food[choice - 1]);

            while(scanf("%f", &grams)==1){            
                if(grams>=20 && grams<=200){         
                    userGrams[x] = grams;            
                    break;                           
                }else{
                    gotoxy(5, 17); p("Invalid Input !! Grams should be not less than 20g and not greater than 200g.\n");
                    gotoxy(5, 18); p("How much %s (in grams) will you cook? ");
                }
            }
        }
            getch();
            system("cls");


    }

Hi, I am trying to create a program with restrictions, where the user will choose from 1-10 different types of food then if the user enter 11 there will be a prompt saying it is invalid then after the command getch(); the prompt saying error will be deleted.

Aucun commentaire:

Enregistrer un commentaire