Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;新手-基本计算器问题 我试图学习一些C++,我决定建立一个基本的I/O计算器。它正确运行,直到第二个getUserInput(),然后自动输入0并终止。我搞不懂发生了什么事 #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) ); } #包括 使用名称空间std; int getUserInput(){//获取用户编号 cout用户输入号码; 返回userInputNumber; } char getUserOper(){//获取用户数学运算符 cout userInputOper; 返回userInputOper; } int-doMath(intx,char-oper,inty){//根据提供的运算符进行数学运算 如果(操作=='+'){ 返回x+y; } 如果(操作=='-'){ 返回x-y; } 如果(操作=='*'){ 返回x*y; } 如果(操作=='/')){ 返回x/y; } 否则{ 返回0; } } void printResult(int endResult){//打印最终结果 不能在getUserOper中使用char char getUserOper() { // Get user math operator cout << "Enter a math operator: " << endl; char userInputOper; cin >> userInputOper; return userInputOper; } char getUserOper() { // Get user math operator cout << "Enter a math operator: " << endl; char userInputOper; cin >> userInputOper; return userInputOper; } char getUserOper(){//获取用户数学运算符 cout userInputOper; 返回userInputOper; }_C++ - Fatal编程技术网

C++;新手-基本计算器问题 我试图学习一些C++,我决定建立一个基本的I/O计算器。它正确运行,直到第二个getUserInput(),然后自动输入0并终止。我搞不懂发生了什么事 #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) ); } #包括 使用名称空间std; int getUserInput(){//获取用户编号 cout用户输入号码; 返回userInputNumber; } char getUserOper(){//获取用户数学运算符 cout userInputOper; 返回userInputOper; } int-doMath(intx,char-oper,inty){//根据提供的运算符进行数学运算 如果(操作=='+'){ 返回x+y; } 如果(操作=='-'){ 返回x-y; } 如果(操作=='*'){ 返回x*y; } 如果(操作=='/')){ 返回x/y; } 否则{ 返回0; } } void printResult(int endResult){//打印最终结果 不能在getUserOper中使用char char getUserOper() { // Get user math operator cout << "Enter a math operator: " << endl; char userInputOper; cin >> userInputOper; return userInputOper; } char getUserOper() { // Get user math operator cout << "Enter a math operator: " << endl; char userInputOper; cin >> userInputOper; return userInputOper; } char getUserOper(){//获取用户数学运算符 cout userInputOper; 返回userInputOper; }

C++;新手-基本计算器问题 我试图学习一些C++,我决定建立一个基本的I/O计算器。它正确运行,直到第二个getUserInput(),然后自动输入0并终止。我搞不懂发生了什么事 #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) ); } #包括 使用名称空间std; int getUserInput(){//获取用户编号 cout用户输入号码; 返回userInputNumber; } char getUserOper(){//获取用户数学运算符 cout userInputOper; 返回userInputOper; } int-doMath(intx,char-oper,inty){//根据提供的运算符进行数学运算 如果(操作=='+'){ 返回x+y; } 如果(操作=='-'){ 返回x-y; } 如果(操作=='*'){ 返回x*y; } 如果(操作=='/')){ 返回x/y; } 否则{ 返回0; } } void printResult(int endResult){//打印最终结果 不能在getUserOper中使用char char getUserOper() { // Get user math operator cout << "Enter a math operator: " << endl; char userInputOper; cin >> userInputOper; return userInputOper; } char getUserOper() { // Get user math operator cout << "Enter a math operator: " << endl; char userInputOper; cin >> userInputOper; return userInputOper; } char getUserOper(){//获取用户数学运算符 cout userInputOper; 返回userInputOper; },c++,C++,您应该在getUserOper中使用字符时使用int char getUserOper() { // Get user math operator cout << "Enter a math operator: " << endl; char userInputOper; cin >> userInputOper; re

您应该在getUserOper中使用字符时使用int

char getUserOper() {                                        // Get user math operator
        cout << "Enter a math operator: " << endl;
        char userInputOper;
        cin >> userInputOper;
        return userInputOper;
    }
char getUserOper() {                                        // Get user math operator
    cout << "Enter a math operator: " << endl;
    char userInputOper;
    cin >> userInputOper;
    return userInputOper;
}
char getUserOper(){//获取用户数学运算符
cout userInputOper;
返回userInputOper;
}

当您执行cin>>时,UserInputPer\n仍在缓冲区中,然后第二次被使用。这会导致存在无效输入和未定义的行为。您也可以这样做

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;
cin>>userInputOper;
//cin.ignore();//从缓冲区中删除一个字符。在本例中,当您按enter键时,会从中删除“\n”,但是”“是一个分隔符,因此如果用户输入23 54,则只会输入23 54,54会保留在缓冲区中以及将在下一次调用中使用的“\n”
cin.ignore(std::numeric_limits::max(),'\n');//清除缓冲区中的所有内容
返回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');
int-myInt;
而(!(cin>>myInt))
{
cout
char getUserOper(){//获取用户数学运算符
cout userInputOper;
返回userInputOper;
}

您需要使用char而不是int

您的运算符应该存储在
char
中。为整数输入符号效果不太好。您需要检查用户是否输入了整数。请看,您决不能在布尔上下文之外使用
cin>
。永远不要再看了在你修复它之前。什么能代替cin>>?我正在努力理解这一点。谢谢你的输入!