gpt4 book ai didi

C++ 新手 - 基本计算器问题

转载 作者:搜寻专家 更新时间:2023-10-31 00:17:58 24 4
gpt4 key购买 nike

我正在尝试学习一些 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) );
}

最佳答案

当你应该在 getUserOper 中使用 char 时你却使用了 int

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

关于C++ 新手 - 基本计算器问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12009533/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com