2012-08-17 75 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一個焦炭更大一些,你會遇到的問題。