Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 显示无效数据项的错误消息_C_String_Char_Int - Fatal编程技术网

C 显示无效数据项的错误消息

C 显示无效数据项的错误消息,c,string,char,int,C,String,Char,Int,这是我的第一个基于文本的游戏项目。当在提示数字的区域中输入文本时,如何显示错误消息,反之亦然。或者完全拒绝不正确输入的能力 #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 i

这是我的第一个基于文本的游戏项目。当在提示数字的区域中输入文本时,如何显示错误消息,反之亦然。或者完全拒绝不正确输入的能力

#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;
}

超级问题标题。半焦q1;扫描%s,&q1;是错误的吗?scanf将在下一个位置添加一个空字节,从而导致未定义的行为。您可能希望将问题放在代码之前。您能否详细说明您需要什么,因此,如果用户在输入字符的同时输入您想要处理的数字并给出错误消息?scanf已经为您完成了这项工作。因为您使用了%d,所以scanf将使用一个整数。如果输入非整数,scanf将返回0而不是1。