2012-08-17 87 views
2

我想学习一些C++,并且我决定构建一个基本的I/O计算器。它正确运行,直到第二个getUserInput(),然后它自动输入0并终止。我无法弄清楚发生了什么!C++新手 - 基本计算器问题

#include <iostream> 
using namespace std; 

int getUserInput() {          // Get user numbers 
    cout << "Enter a number: " << endl; 
    int userInputNumber; 
    cin >> userInputNumber; 
    return userInputNumber; 
} 

char getUserOper() {          // Get user math operator 
    cout << "Enter a math operator: " << endl; 
    int userInputOper; 
    cin >> userInputOper; 
    return userInputOper; 
} 

int doMath(int x, char oper, int y) {      // Does math based on provided operator 
    if(oper=='+') { 
     return x + y; 
    } 
    if(oper=='-') { 
     return x - y; 
    } 
    if(oper=='*') { 
     return x * y; 
    } 
    if(oper=='/') { 
     return x/y; 
    } 
    else { 
     return 0; 
    } 
} 

void printResult(int endResult) {       // Prints end result 
    cout << endResult; 
} 

int main() { 
    int userInputOne = getUserInput(); 
    char userOper = getUserOper(); 
    int userInputTwo = getUserInput(); 
    printResult(doMath(userInputOne, userOper, userInputTwo)); 
} 
+3

您的操作员应该存储在'char'中。输入整数的符号不太好。 – chris 2012-08-17 16:25:30

+0

您需要检查用户是否输入了整数。请参阅http://web.eecs.utk.edu/~cs102/lectures/cinLecture.html – 2012-08-17 16:25:50

+3

您绝对不能在布尔上下文外使用'cin >>'。永远。在解决问题之前,甚至不要进一步观察。 – 2012-08-17 16:26:30

回答

3

你的使用,当你当你做CIN >> userInputOper的\ n是仍然应该使用一个char在getUserOper

char getUserOper() {          // Get user math operator 
    cout << "Enter a math operator: " << endl; 
    char userInputOper; 
    cin >> userInputOper; 
    return userInputOper; 
} 
1

使用在getUserOper char

char getUserOper() {          // Get user math operator 
     cout << "Enter a math operator: " << endl; 
     char userInputOper; 
     cin >> userInputOper; 
     return userInputOper; 
    } 
1

INT在缓冲区中,然后第二次使用。导致无效输入存在和未定义的行为。所以,做

cin >> userInputOper; 
//cin.ignore(); // removes one char from the buffer in this case the '\n' from when you hit the enter key, however " " is a delimiter so if the user enters 23 54 only 23 gets entered and 54 remains in the buffer as well as the '\n' which will get used on the next call 
cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n'); // clears everything in the buffer 
return userInputOper; 

您也应该检查错误输入

int myInt; 
while (!(cin >> myInt)) 
{ 
cout << "Bad input try again\n"; 
cin.clear(); // this only clears the fail state of the stream, doesn't remove any characters 
cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n'); // removes all chars from the buffer up to the '\n' 
} 
cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n'); 

你或许应该采取一个字符为运营商虽然不是完全必要的。当用户输入比焦炭255

+0

我试图理解这一点。感谢您的输入! – josephndenton 2012-08-18 16:12:19

+0

然后至少给一个投票:( – EddieV223 2012-08-22 05:26:50

1
char getUserOper() {          // Get user math operator 
     cout << "Enter a math operator: " << endl; 
     char userInputOper; 
     cin >> userInputOper; 
     return userInputOper; 
    } 

您需要使用的,而不是INT一个焦炭更大一些,你会遇到的问题。