当我按's'和'你'时,为什么我得不到正确答案?

时间:2022-08-14 19:31:27

I am writing a simple program to judge the day by the first letter in C. I hava some trouble debugging .Thank all you who see and answer the question in advance.

我正在编写一个简单的程序,用C中的第一个字母来判断这一天。我在调试时遇到了一些麻烦。谢谢所有提前看到并回答问题的人。

#include<stdio.h>
int main(void)

{
//Sunday  Monday Tuesday Wednesday Thursday Friday Saturday 

char letter;
printf("Input the first letter.\n");
scanf("%c",&letter); 

switch(letter)
{
    case 's': printf("Please in put the second number:\n");
          if(getchar()=='u')
              printf("It's Sunday\n");
          else if(getchar()=='a')
              printf("It's Saturday\n");
          else 
              printf("Input wrong!\n");             
          break;
    case 'm': printf("It's Monday\n");
          break;
    case 't': printf("Please in put the second number\n");
          if(getchar()=='u')
              printf("It's Tuesday\n");
          else if(getchar()=='h')
              printf("It's Thursday\n");
          else 
              printf("Input wrong!\n");             
          break;
    case 'w':  printf("It's Wednesday.");
           break;
    case 'f':  printf("It's Friday.  "); 
           break;
    default : printf("You input the wrong letter \n");
}
return 0;
}

4 个解决方案

#1


5  

Let's look at a chunk of your code:

让我们看看你的一大块代码:

      if(getchar()=='u')
          printf("It's Sunday\n");
      else if(getchar()=='a')
          printf("It's Saturday\n");
      else 
          printf("Input wrong!\n");             
      break;

Here's how that code will execute:

以下是该代码的执行方式:

  1. getchar() will be called and we will compare it against 'u'. Let's assume that fails. This means that we have consumed one character from the input keyboard.
  2. 将调用getchar(),我们将它与'u'进行比较。我们假设失败了。这意味着我们从输入键盘中​​消耗了一个字符。

  3. getchar() will be called again and compared to 'a'!
  4. 将再次调用getchar()并将其与'a'进行比较!

Instead of this, you probably want something like:

而不是这个,你可能想要这样的东西:

      int c = getchar();
      if(c == 'u')
          printf("It's Sunday\n");
      else if(c == 'a')
          printf("It's Saturday\n");
      else 
          printf("Input wrong!\n");             
      break;

Now, you're going to still have additional problems because after your scanf(), the next character in the input stream is going to be '\n'.

现在,你仍然会遇到其他问题,因为在你的scanf()之后,输入流中的下一个字符将是'\ n'。

To get around this, use scanf(" %c", &letter) for both the first and second character of the input.

要解决此问题,请对输入的第一个和第二个字符使用scanf(“%c”,&letter)。

#2


1  

Remember that stdin is buffered and includes '\n'.

请记住,stdin是缓冲的并包含'\ n'。

printf("Input the first letter.\n");
scanf("%c", &letter); 

Say the user is thinking "saturday". When the user enters s Enter, scanf("%c",...) will consume the 's', leaving the '\n' in stdin.

假设用户正在考虑“星期六”。当用户输入s Enter时,scanf(“%c”,...)将消耗's',在stdin中保留'\ n'。

When code gets to

代码到达时

case 's': 
  printf("Please input the second letter:\n");
  if(getchar()=='u')
    printf("It's Sunday\n");

getchar() will return '\n'.

getchar()将返回'\ n'。

Instead, read the input and toss any preceding white-space with

相反,读取输入并抛出任何前面的空白区域

scanf(" %c",&letter); // Note leading space.

Also, as others noted, do not read a char again when trying to distinguish Saturday and Sunday.

此外,正如其他人所指出的那样,在尝试区分星期六和星期日时,不要再读一个字符。

// Ensure letter has _some_ known value should scanf() reach EOF.
char letter = 0;
printf("Input the first letter.\n");
scanf(" %c", &letter); 

switch(letter) {
    case 's': 
      printf("Please input the second letter:\n");
      scanf(" %c", &letter); 
      if (letter == 'u')
        printf("It's Sunday\n");
      else if (letter == 'a')
        printf("It's Saturday\n");
      else 
        printf("Input wrong!\n");             
      break;

#3


0  

You are using getchar() twice for one case statement. That's not how you should do it. You should getchar() first, then save that into a variable then compare it. That's the way to go.

您对一个case语句使用了两次getchar()。那不是你应该怎么做的。你应该首先使用getchar(),然后将其保存到变量中然后进行比较。这是要走的路。

#4


-1  

For example to get to saturday you need to type s followed by summat not u then a

例如,要到星期六,你需要输入s,然后输入summat而不是你的a

Ditto for tues and thurs

同样适合星期二和星期四

Most probably at the terminal they need to hit the enter key before your program gets a look in. So just read a string

很可能在终端,他们需要在程序查看之前按Enter键。所以只需读取一个字符串

#1


5  

Let's look at a chunk of your code:

让我们看看你的一大块代码:

      if(getchar()=='u')
          printf("It's Sunday\n");
      else if(getchar()=='a')
          printf("It's Saturday\n");
      else 
          printf("Input wrong!\n");             
      break;

Here's how that code will execute:

以下是该代码的执行方式:

  1. getchar() will be called and we will compare it against 'u'. Let's assume that fails. This means that we have consumed one character from the input keyboard.
  2. 将调用getchar(),我们将它与'u'进行比较。我们假设失败了。这意味着我们从输入键盘中​​消耗了一个字符。

  3. getchar() will be called again and compared to 'a'!
  4. 将再次调用getchar()并将其与'a'进行比较!

Instead of this, you probably want something like:

而不是这个,你可能想要这样的东西:

      int c = getchar();
      if(c == 'u')
          printf("It's Sunday\n");
      else if(c == 'a')
          printf("It's Saturday\n");
      else 
          printf("Input wrong!\n");             
      break;

Now, you're going to still have additional problems because after your scanf(), the next character in the input stream is going to be '\n'.

现在,你仍然会遇到其他问题,因为在你的scanf()之后,输入流中的下一个字符将是'\ n'。

To get around this, use scanf(" %c", &letter) for both the first and second character of the input.

要解决此问题,请对输入的第一个和第二个字符使用scanf(“%c”,&letter)。

#2


1  

Remember that stdin is buffered and includes '\n'.

请记住,stdin是缓冲的并包含'\ n'。

printf("Input the first letter.\n");
scanf("%c", &letter); 

Say the user is thinking "saturday". When the user enters s Enter, scanf("%c",...) will consume the 's', leaving the '\n' in stdin.

假设用户正在考虑“星期六”。当用户输入s Enter时,scanf(“%c”,...)将消耗's',在stdin中保留'\ n'。

When code gets to

代码到达时

case 's': 
  printf("Please input the second letter:\n");
  if(getchar()=='u')
    printf("It's Sunday\n");

getchar() will return '\n'.

getchar()将返回'\ n'。

Instead, read the input and toss any preceding white-space with

相反,读取输入并抛出任何前面的空白区域

scanf(" %c",&letter); // Note leading space.

Also, as others noted, do not read a char again when trying to distinguish Saturday and Sunday.

此外,正如其他人所指出的那样,在尝试区分星期六和星期日时,不要再读一个字符。

// Ensure letter has _some_ known value should scanf() reach EOF.
char letter = 0;
printf("Input the first letter.\n");
scanf(" %c", &letter); 

switch(letter) {
    case 's': 
      printf("Please input the second letter:\n");
      scanf(" %c", &letter); 
      if (letter == 'u')
        printf("It's Sunday\n");
      else if (letter == 'a')
        printf("It's Saturday\n");
      else 
        printf("Input wrong!\n");             
      break;

#3


0  

You are using getchar() twice for one case statement. That's not how you should do it. You should getchar() first, then save that into a variable then compare it. That's the way to go.

您对一个case语句使用了两次getchar()。那不是你应该怎么做的。你应该首先使用getchar(),然后将其保存到变量中然后进行比较。这是要走的路。

#4


-1  

For example to get to saturday you need to type s followed by summat not u then a

例如,要到星期六,你需要输入s,然后输入summat而不是你的a

Ditto for tues and thurs

同样适合星期二和星期四

Most probably at the terminal they need to hit the enter key before your program gets a look in. So just read a string

很可能在终端,他们需要在程序查看之前按Enter键。所以只需读取一个字符串