c - Present error messages for invalid data entry -
this first project (kind of text based game). how present error messages when text entered in area numbers prompted , vice versa. or possibly ability reject improper input altogether.
#include <stdio.h> #include <stdlib.h> int main() { char name[20]; char q1; int q2[2],q3[2]; float qt[3]; for(q1='n';q1!='y';) { printf("what name?\n"); gets(name); printf("you have entered: "); puts(name); printf("is okay? [y/n]\n"); scanf("%s",&q1); if(q1!='y'&&q1!='n') printf("user error: '%c' not recognized.\nplease enter 'y' continue or 'n' edit name\n",&q1); if(q1!='y'&&q1!='n') scanf("%s",&q1); } printf("okay, %s.",name); printf(" out of 3 choices most?\n1. start garden.\n2. build house.\n3. travel somewhere you've never been.\n"); scanf("%d",q2); (;q2[0]>3||q2[0]<1;) { printf("user error: '%c' not recognized.\nplease enter 1 of options given.",q2); scanf("%d",q2); } if(q2[0]==1) printf("what plant in garden?\n1. flowers\n2. vegetables\n3. fruit , berries\n"); else if(q2[0]==2) printf("where build house?\n1. in mountains\n2. in desert\n3. sea\n"); else if(q2[0]==3) printf("what bring such place?\n1. new faces , cultures\n2. treasures , adventure\n3. escape ordinary\n"); else for(;q2[0]>3||q2[0]<1;) { printf("user error: '%c' not recognized.\nplease enter 1 of options given.",&q2); scanf("%d",q3); } scanf("%d",q3); qt[0]=(q2[0]*12)/q3[0]; printf("%f",qt[0]); return 0; }
Comments
Post a Comment