c - 输入数据后开关总是默认

标签 c while-loop switch-statement

也许第一段代码运行没有问题 但是,我输入评论数据后总是默认

char answer;

do{
    printf("Do you want to add new comment?Y/N: ");
    scanf("%c",&answer);
    fflush(stdin);

    switch(tolower(answer)) 
    {
        case 'y':
            comment();
            break;

        case 'n':
            main();
            break;

        default:
            printf("Wrong choice !\n\n");
            break;
    }
}while(tolower(answer)!=='y'||'n'); 

有comment()的代码,我猜问题出在这里。

FILE*fp=fopen("comment.txt","a+");

if(fp == NULL)
{
    printf("FIle not Found");
    exit(1);
}
else
{
    printf("Please enter your name: ");
    gets(c.name);
    printf("Pleas enter the date: ");
    gets(c.date);
    printf("Please enter the movie name: ");
    gets(c.movie);
    printf("Please enter your comment in 100 words:\n");
    printf("Please press [Tab]and[Enter] to submit your comment\n");
    scanf("%[^\t]",c.comment);

    fprintf(fp,"%s    %s\n%s\n%s\n\n",c.name,c.date,c.movie,c.comment);
}
fclose(fp);

有人可以帮帮我吗?谢谢!!

最佳答案

尝试使用 strlwr() 而不是 tolower()

关于c - 输入数据后开关总是默认,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47592922/

相关文章:

java - hasNextLine() 在 While 循环中始终返回 true

c - 作业-下标值既不是数组也不是指针也不是 vector

C - While 循环内的 If 语句。连续扫描()

c - 需求中的 for 循环错误 (C)

linux - bash 中的简单 while 循环

java:切换和反射哪个效率更高?

android - Java代码效率。我应该使用开关吗?

c++ - 如何在该程序中将if else语句转换为switch语句? Visual Studio 2019 C++

c - 为什么在 C 中写入二进制文件时 fwrite 会打印空字节?

c - "Stack around the variable ' 缓冲区 ' was corrupted"- 这里出了什么问题