【问题标题】:Detect a new line character检测换行符
【发布时间】:2012-09-17 18:17:32
【问题描述】:

我正在阅读结构数组中的标准输入学生。在为一个学生介绍了详细信息后,我询问了另一个学生的详细信息。如果选择是Y,我会添加新学生,如果选择是N,break。但是,如果选择只是 ENTER 怎么办?如何检测换行符?我尝试使用 getchar(),但它跳过了 stdin 的第一次读取。当我调试时,它不会停止到第一行 test=getchar(),而是停止到第二行。

#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <stdlib.h>

struct student
{
char name[20];
int age;
};

int main()
{
struct student NewStud[5];
char test;
int count=0;
for(count=0;count<5;count++)
{
    printf("Enter the details for %s student: ",count>0?"another":"a");
    printf("\nName : ");
    scanf("%s",NewStud[count].name);
    printf("\nAge : ");
    scanf("%d",&NewStud[count].age);
    printf("Would you like to continue? (Y/N)");
    test=getchar();
    if(test=='\n')
    {
        printf("Invalid input. Would you like to continue? (Y/N)");
        test=getchar();
    }
    while(tolower(test) !='n' && tolower(test) != 'y')
    {
        printf("Invalid input.Would you like to continue? (Y/N)");
        test=getchar();
    }
    if(tolower(test) == 'n')
    {
        break;
    }
    if(tolower(test) == 'y')
    {
        continue;
    }
}


getch();
}

【问题讨论】:

  • 这看起来不像 C++。将来请标记 C 或 C++,因为这两种语言可以有不同的风格来完成您的任务。
  • 很难理解你的问题是什么。 “它跳过标准输入的第一次阅读”到底是什么意思? (您的问题有 99% 的可能性是有人键入“Y enter”并且您只读取了一个字符,而在您下次调用 getchar 时让 enter 读取。使用读取行的函数。)
  • 这看起来像是一道作业题。作业问题得到作业标签。我很确定它是,所以我会继续为你添加它。如果您不同意,您可以自行删除。编辑:呃,已经过时并且正在被删除? 转到元
  • @DavidSchwartz - 如果我错了,劳拉可以纠正我,但我很确定问题是显示提示“...(Y/N)”,然后它就正确了离开“无效输入”消息。由于使用scanf(),导致输入缓冲区中留下了'\n'

标签: c


【解决方案1】:

问题是scanf() 在输入流中留下一个换行符,您必须先使用它才能在getchar() 中获得“有效”数据。

例如:

scanf("\n%s",NewStud[count].name);
getchar();
printf("\nAge : ");     
scanf("%d",&NewStud[count].age);
getchar();
printf("Would you like to continue? (Y/N)");
test=getchar();   // Now this will work

查看此link 了解更多信息。这是给fgets的,但是getchar()也是同样的问题

【讨论】:

    【解决方案2】:

    当然它会跳过第一次阅读,你把它放在一个 if 语句中,像这样:if(test=='\n')

    您获得了该学生的所有信息,然后用户按下回车键,因此您返回 for(count=0;count&lt;5;count++) 并请求新学生的新输入。 我认为您想要做的是改用 while 语句。

    【讨论】:

      【解决方案3】:

      你可以替换

      > test=getchar();
      >     if(test=='\n')
      >     {
      >         printf("Invalid input. Would you like to continue? (Y/N)");
      >         test=getchar();
      >     }
      

      while((test=getchar()) == '\n')
      {
        printf("Invalid input. Would you like to continue? (Y/N)");
      }
      

      【讨论】:

        【解决方案4】:

        test 的值与“\n”进行比较,如以下示例:

        int main() {
            int test;
            test = getchar();
            printf("[%d]\n", test);
            if(test == '\n') printf("Enter pressed.\n");
            return(0);
        }
        

        ps:你的test 必须是int

        【讨论】:

        • getchar() 返回一个字符,因为 '\0' 是一个字符,所以没有理由使用 int。 OP 并不是说​​她的测试失败了,只是在循环的第一次迭代中。
        • @Mike C 标准库中的getchar() 返回一个int。它不能返回 char,因为它必须能够区分 EOF 条件和有效字符。
        猜你喜欢
        • 1970-01-01
        • 2016-02-16
        • 1970-01-01
        • 2021-07-23
        • 2012-03-04
        • 2013-07-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多