gpt4 book ai didi

c++ - 我无法让循​​环工作,当我按下按钮退出我的开关时它不起作用

转载 作者:行者123 更新时间:2023-11-30 05:08:31 25 4
gpt4 key购买 nike

现在我可以使用 while 循环,但现在它正在循环第一个操作的答案,而不是要求输入另一个数字以进行更多操作,并且当我按 x 退出时它不会退出。我只是想知道将要求输入另一组数字的代码行放在哪里。然后输入 Y 或 X yes 继续,x 退出我可以放置代码行的地方。我只需要知道这些都在哪里,谢谢 :)

#include <iostream>
using namespace std;
char op, x, y;





double scanNumber() {
double d;

cin >> d;

return d;
}

bool readOperator(string s, char &operation) {
char c;
cout << "\nEnter Operator: ";
cin >> c;
if (s.find(c) != -1) {
operation = c;
return true;
}
return false;
}

double add(double d1, double d2) {
return d1 + d2;
}

double sub(double d1, double d2) {
return d1 - d2;
}

double mul(double d1, double d2) {
return d1*d2;
}


double division(double d1, double d2) {
return d1 / d2;
}
int main()

{

double d1, d2;


cout << "Enter 1st Number: ";
d1 = scanNumber();

cout << "Enter 2nd Number: ";
d2 = scanNumber();

char operation;
while (!readOperator("+-*/", operation)) {
cout << "Invalid Operator Please Pick Another One" << endl;


}

bool valid;
double result;



do{
switch (operation)
{

case '+':
result = add(d1, d2);
break;
case '-':
result = sub(d1, d2);
break;
case '*':
result = mul(d1, d2);
break;
case '/':
if (d2 == 0) {
cout << "For division, 2nd operator can't be 0." << endl;

}
else {
result = division(d1, d2);
}
break;
case 'c':
cout << "Clearing the Calculator " << endl;
valid = false;
break;
case 'x':
exit(0);
default:
cout << "invalid input";

}

cout << "\nResult: " << result << endl;
cout << "\nDo another?(Enter 'y'for yes or 'x' to exit ) ";

cin >> y, x;
}while (op != 'x');

return 0;
}

最佳答案

您没有逻辑可以在计算后再次重复循环。因此,您需要一个 do 来与最后留下的单独 while (op != 'x'); 配对。

你可以这样做:

int main() {

double d1, d2;

do { //main loop now starts here
cout << "Enter 1st Number: ";
d1 = scanNumber();
cout << "Enter 2nd Number: ";
d2 = scanNumber();

char operation;

//since you have a switch on the c and x operator they need to be taken into
//consideration in this while loop
while (!readOperator("+-*/cCxX", operation)) {
cout << "Invalid Operator Please Pick Another One" << endl;
}

bool valid;
double result;

switch (operation) {
case '+': result = add(d1, d2); break;
case '-': result = sub(d1, d2); break;
case '*': result = mul(d1, d2); break;
case '/':
if (d2 == 0) {
cout << "For division, 2nd operator can't be 0." << endl;
}
else {
result = division(d1, d2);
}
break;
case 'c':
case 'C':
cout << "Clearing the Calculator " << endl;
valid = false;
break;
case 'x':
case 'X': return 0;
default: cout << "invalid input";
}

//You need to use the valid as well to know if you show the result
if (valid) cout << "\nResult: " << result << endl;
cout << "\nDo another(Enter 'y'for yes or 'x' to exit ) ? ";
cin >> ch;

} while (ch != 'x'); //now the } ends here and the condition uses ch

return 0; //return is now after the while
}

使用建议的输入运行示例:

enter image description here

关于c++ - 我无法让循​​环工作,当我按下按钮退出我的开关时它不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46820986/

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