这是我的第一个项目(一种基于文本的游戏)。如何在提示数字的区域中输入文本时如何显示错误消息,反之亦然。或者可能完全拒绝不正确的输入。
#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 is your name?\n");
gets(Name);
printf("You have entered: ");
puts(Name);
printf("Is this okay? [y/n]\n");
scanf("%s",&q1);
if(q1!='y'&&q1!='n')
printf("User error: '%c' not recognized.\nPlease enter 'y' to continue or 'n' to edit your name\n",&q1);
if(q1!='y'&&q1!='n')
scanf("%s",&q1);
}
printf("Okay, %s.",Name);
printf(" Out of the three choices which would you like to do most?\n1. Start a garden.\n2. Build a house.\n3. Travel to somewhere you've never been.\n");
scanf("%d",q2);
for (;q2[0]>3||q2[0]<1;)
{
printf("User error: '%c' not recognized.\nPlease enter one of the options given.",q2);
scanf("%d",q2);
}
if(q2[0]==1)
printf("What would you plant in your garden?\n1. Flowers\n2. Vegetables\n3. Fruit and Berries\n");
else
if(q2[0]==2)
printf("Where would you build your house?\n1. In the Mountains\n2. In the Desert\n3. By the Sea\n");
else
if(q2[0]==3)
printf("What would bring you to such a place?\n1. New faces and cultures\n2. Treasures and adventure\n3. Escape from the ordinary\n");
else
for(;q2[0]>3||q2[0]<1;)
{
printf("User error: '%c' not recognized.\nPlease enter one of the options given.",&q2);
scanf("%d",q3);
}
scanf("%d",q3);
qT[0]=(q2[0]*12)/q3[0];
printf("%f",qT[0]);
return 0;
}