2012-11-19 40 views
1

工作使用一个非常简单的计算器程序,提示用户对操作执行用户,接着提示两个整数要在其上执行此操作。程序应该在这些操作之后循环,除非用户输入字符'q',此时程序应该退出。的getchar不是在开关情况(c)

#include <stdio.h> 

int main (void) 
    { 
     char c; 
     int number[2], num1, num2, result; 
     double num1d, num2d, resultd; 
     int done=1; 

     while(done) 
     { 
     printf("\t What sort of operation would you like to perform? \n \t Type + - */accordingly. \n"); 
     c = getchar(); 
     printf("\tplease enter a number \n"); 
     scanf("%d",&number[0]); 
     printf("\tplease enter another number \n"); 
     scanf("%d",&number[1]); 
     num1 = number[0]; 
     num2 = number[1]; 

     switch(c) 
      { 
      case('-'): 
      result = num1-num2; 
      printf("\nThe first number you entered subtracted by the second number is %d.\n", result); 
      break; 

      case('+'): 
      result = num1+num2; 
      printf("The first number you entered added to the second number is %d.\n", result); 
      break; 

      case('*'): 
      result = num1*num2; 
      printf("The first number you entered multiplied with the second number is %d.\n", result); 
      break; 

      case('/'): 
      num1d = (double) num1; 
      num2d = (double) num2; 
      resultd = num1d/num2d; 
      printf("The first number you entered divided by the second number is %g.\n", resultd);; 
      break; 

      case('q'): 
      printf(" Now Exiting...\n"); 
      done=0; 
      break; 

      default: 
      puts("Invalid key pressed. Press q to exit"); 
      break; 
      } 
     } 

     return 0; 
    } 

正确Works的单个计算中,但随后执行奇怪;特别是它打印

printf("\t What sort of operation would you like to perform? \n \t Type + - */accordingly. \n"); 
printf("\tplease enter a number \n"); 

一起。

清除输入缓冲器while (getchar() != '\n');不能解决此的标准方法。一两次,这个文本显示不正确,用户仍然可以使用该程序,就好像指令正在显示它们应该一样(所以用户可以键入一个操作,例如+,回车,然后是一些整数和回车,程序将从该点开始正确执行)然而,每隔一段时间,程序都会将“无效键按下,按q退出”,无论输入如何。

回答

3

其他人在这里说的是真的,getchar()返回int但这不是你的问题。

问题是getchar()在使用后会留下换行符。如果你打算使用getchar()你必须总是之后消耗换行符。这个简单的修复:

printf("\t What sort of operation would you like to perform? \n \t Type + - */accordingly. \n"); 
    c = getchar(); 
    getchar();  //<-- here we do an extra getchar for the \n 
    printf("\tplease enter a number \n"); 
    scanf("%d",&number[0]); 
    printf("\tplease enter another number \n"); 
    scanf("%d",&number[1]); 

并且这将消除该问题。每次输入<somechar><enter>它真的穿上缓冲两个字符,例如,如果我打+,并输入我收到时间:

'+''\n' // [+][\n] 

getchar()将只能得到这些第一,那么当getchar()被再次调用它将不会等待您的输入它只会采取'\n'并继续前进到scanf()

+0

感谢兄弟,这很有用:) –

3

你不应该用更高层次的输入功能,如scanf()混合字符一个字符。最好使用scanf()来输入命令字符,但当然你必须在命令后面按回车键。我相信这是你问题的根源。

顺便说一句,请注意getchar(),尽管它的名字,返回intchar。这是因为它可以返回EOF,这是一个特殊的常量,其值与所有字符的值不同。

此外,您应该总是检查I/O函数的返回值,如scanf(),如果输入不匹配模式字符串,它们可能会失败。

作为调试提示,您可以在解释它之前打印c的值,以便您更轻松地查看和了​​解程序的流程。

+0

我听说过有关getchar()返回整数的事实 - 这让我颇感意外。但是,你知道为什么这可能会导致这种奇怪的行为吗? – Stumbler

+0

@邓肯 - 它没有。你从getchar()中获取'char'的事实只意味着你可能会溢出你的输入(即一个'int'可以容纳多于一个'char')。你的问题是留下换行符(请参阅我的回答)。 – Mike

0

你的getchar应该返回int。原因是如下

getchar reads characters from the program's standard input 
and returns an int value suitable for storing into a char. 
The int value is for one reason only: not only does getchar 
return all possible character values, but it also returns an 
extra value to indicate that end-of-input has been seen. 
The range of a char might not be enough to hold this extra value, 
so the int has to be used. 

所以基本上你需要改变char cint c在你的代码

+0

虽然'getchar()'返回'int'的确是这样,但是这个建议并不能解决这个问题。 – Mike

2

我猜测它的工作原理是第一次,但没有下一次。这是因为scanf电话离开输入缓冲区换行符所以下次getchar是所谓的循环,将返回换行符。添加一个空格在scanf格式后调用

scanf("%d ",&number[0]); 

,它会丢弃从缓冲区中剩余的空白。

使用调试器来遍历代码并检查要验证的变量。