C 输入字符而不是整数

C 输入字符而不是整数,c,scanf,C,Scanf,我正在使用do-while循环将菜单打印到屏幕上。我把选择看作一个整数。问题是,如果用户输入一个字符,程序就会崩溃。我怎样才能避免呢 #include <stdio.h> int menu() { // prints the main menu of labs/// int choice; printf("1)Lab 5 ( Repetetitions ).\n2)Lab 10 ( Passing 1D-Arrays to functions )

我正在使用do-while循环将菜单打印到屏幕上。我把选择看作一个整数。问题是,如果用户输入一个字符,程序就会崩溃。我怎样才能避免呢

#include <stdio.h>
int menu() {            // prints the main menu of labs///
    int choice;
    printf("1)Lab 5 ( Repetetitions ).\n2)Lab 10 ( Passing 1D-Arrays to functions ).\n3)GPA Calculation.\n4)EXIT.\n\nEnter your choice: ");
    scanf("%d", &choice);
    return choice;
}

int main() {
    int choice;

    do {

        choice = menu();
        if (choice != 4) {

            if (choice == 1)
                //lab5(choice);

            else if (choice == 2)
                //lab10(choice);


            else if (choice == 3)
                //  lab11(choice);
            else
                printf("invalid choice\n");
        }

    } while (choice != 4);
    return 0;
}        
#包括
int menu(){//打印实验室的主菜单///
智力选择;
printf(“1)实验5(重复)。\n2)实验10(将1D数组传递给函数)。\n3)GPA计算。\n4)退出。\n\n输入您的选择:”;
scanf(“%d”,选择(&C);
回报选择;
}
int main(){
智力选择;
做{
选择=菜单();
如果(选项!=4){
如果(选项==1)
//lab5(选择);
else if(选项==2)
//lab10(选择);
else if(选项==3)
//lab11(选择);
其他的
printf(“无效选择\n”);
}
}while(选项!=4);
返回0;
}        

这应该适用于您,您需要检查scanf的返回值

int menu() {            // prints the main menu of labs///
    int choice;
    printf("1)Lab 5 ( Repetetitions ).\n2)Lab 10 ( Passing 1D-Arrays to functions ).\n3)GPA Calculation.\n4)EXIT.\n\nEnter your choice: ");
    if(scanf("%d", &choice) == 1)
    {
        return choice;
    }
    else
    {
        return 0;
    }
}

这应该适用于您,您需要检查scanf的返回值

int menu() {            // prints the main menu of labs///
    int choice;
    printf("1)Lab 5 ( Repetetitions ).\n2)Lab 10 ( Passing 1D-Arrays to functions ).\n3)GPA Calculation.\n4)EXIT.\n\nEnter your choice: ");
    if(scanf("%d", &choice) == 1)
    {
        return choice;
    }
    else
    {
        return 0;
    }
}
scanf()(和函数系列)从输入缓冲区返回成功转换的次数,如果转换(%d)失败,则函数返回0(或EOF)。在这种情况下,无法转换的字符不会从缓冲区中删除,这就是为什么会出现无休止的循环,转换将永远失败

scanf
调用之后,我使用它来刷新输入缓冲区,程序的行为符合预期

(不知道这会持续多久)

scanf()(和函数系列)从输入缓冲区返回成功转换的次数,如果转换(%d)失败,则函数返回0(或EOF)。在这种情况下,无法转换的字符不会从缓冲区中删除,这就是为什么会出现无休止的循环,转换将永远失败

scanf
调用之后,我使用它来刷新输入缓冲区,程序的行为符合预期

(不知道这会持续多久)

/*使用此代码*/
#包括
#包括
作废orderAsk(int-orderStorage[1]);
int main()
{     
int-orderStorage[1];
orderAsk(orderStorage);
printf(“%d”,orderStorage[0]);
返回0;
}
无效orderAsk(int-orderStorage[1]){
int d;
d=scanf(“%d”,&orderStorage[0]);
如果(d!=1){
系统(“cls”);
fflush(stdin);
printf(“错误!重新输入\n”);
orderAsk(orderStorage);
}
}
/*使用此代码*/
#包括
#包括
作废orderAsk(int-orderStorage[1]);
int main()
{     
int-orderStorage[1];
orderAsk(orderStorage);
printf(“%d”,orderStorage[0]);
返回0;
}
无效orderAsk(int-orderStorage[1]){
int d;
d=scanf(“%d”,&orderStorage[0]);
如果(d!=1){
系统(“cls”);
fflush(stdin);
printf(“错误!重新输入\n”);
orderAsk(orderStorage);
}
}

为了避免这种情况,请读入一个字符串,然后自己将其转换为整数。检查
scanf
中的返回值以确保其正确。如果没有清除缓冲区,请再试一次。
main()
函数作为一个简单的
switch()
语句会更加清晰。这里有一点幽默:从程序中移除爆炸物和爆破帽,那么用户不能“炸掉它”来避免这种情况,读入字符串,然后自己将其转换为整数。检查
scanf
中的返回值,确保其正确。如果没有清除缓冲区,请再试一次。
main()
函数作为一个简单的
switch()
语句会更加清晰。这里有一点幽默:从程序中移除炸药和爆破帽,那么用户就不能“炸毁它”。您应该检查
scanf
的返回值。您的代码将刷新伪输入,但如果输入失败,它将使用不确定值
choice
。无论选择成功与否,它都将刷新输入。此外,一些文档称,如果转换失败,它将返回EOF,但我选中了,在这种情况下,它将返回0。什么是官方行为?通常它返回成功匹配的数量
EOF
仅在需要更多字符才能开始转换时才会返回,但没有更多字符。关于此语句:“scanf函数返回从输入缓冲区成功读取的字符数”,这不是真的!scanf()(和函数族)返回成功输入转换的次数。成功时,返回值将与“format”参数中的格式说明符数量匹配。您应该检查
scanf
的返回值。您的代码将刷新伪输入,但如果输入失败,它将使用不确定值
choice
。无论选择成功与否,它都将刷新输入。此外,一些文档称,如果转换失败,它将返回EOF,但我选中了,在这种情况下,它将返回0。什么是官方行为?通常它返回成功匹配的数量
EOF
仅在需要更多字符才能开始转换时才会返回,但没有更多字符。关于此语句:“scanf函数返回从输入缓冲区成功读取的字符数”,这不是真的!scanf()(和函数族)返回成功输入转换的次数。成功时,返回的值将与“format”参数中的格式说明符数量匹配。
/*USE This Code*/  
#include <stdio.h>
#include <stdlib.h>
void orderAsk(int orderStorage[1]);
int main()
{     
    int orderStorage[1];
    orderAsk(orderStorage);
    printf("%d",orderStorage[0]);
    return 0;
}

void orderAsk(int orderStorage[1]){
    int d;
    d = scanf("%d", &orderStorage[0]);
    if ( d!= 1) {
        system("cls");
        fflush(stdin);
        printf("Error! Re-Enter\n");
        orderAsk(orderStorage);
    }
}