-1

我试图让我的计算器在完成计算后循环回到顶部?我已经尝试过 while 循环并查看了有关它的教程,但我无法将其置于上下文中。

如果你能告诉我如何在这个程序中实际使用它,那就太好了。

#include<iostream>
using namespace std;

int main() {
  double num1, num2;
  char op;
  cout << "********C++ CALCULATOR********" << endl;
  cout << "Please enter your first number" << endl;
  cin  >> num1;
  cout << "Please enter your operand (+, -, *, /)\n" << endl;
  cin  >> op;
  cout << "Please enter your second number\n" << endl;
  cin  >> num2;
  if (op== '+') {
    cout << "The answer is: " << num1 + num2 << endl;
  } else if (op == '-') {
    cout << "The answer is: " << num1 - num2 << endl;
  } else if (op == '/') {
    cout << "The answer is: " << num1 / num2 << endl;
  } else if (op == '*') {
    cout << "The answer is: " << num1 * num2 << endl;
  } else {
    cout << "That was an invalid command!" << endl;
  }
}
4

3 回答 3

2

我想你想要这样的东西:

#include<iostream>
using namespace std;

int main() {
  double num1 = 0, num2 = 0;
  char op = '';
  char answer = '';
  while(answer != 'n') {  // Check condition
    cout << "********C++ CALCULATOR********" << endl;
    cout << "Please enter your first number" << endl;
    cin  >> num1;
    cout << "Please enter your operand (+, -, *, /)" << endl;
    cin  >> op;
    cout << "Please enter your second number\n" << endl;
    cin  >> num2;
    if (op == '+') {
      cout << "The answer is: " << num1 + num2 << endl;
    } else if (op == '-') {
      cout << "The answer is: " << num1 - num2 << endl;
    } else if (op == '/') {
      cout << "The answer is: " << num1 / num2 << endl;
    } else if (op == '*') {
      cout << "The answer is: " << num1 * num2 << endl;
    } else {
      cout << "That was an invalid command!\n Exit." << endl;
    }
    cout << "Do you want repeat? \"y\" or \"n\"\n" << endl;
    cin  >> answer;
  }
}

while 结构由一段代码和一个条件组成。评估条件,如果条件为真,则执行块内的代码。如此重复,直到条件变为假。因为 while 循环在块执行之前检查条件,所以控制结构通常也称为预测试循环。与 do while 循环比较,后者在循环执行后测试条件。

于 2013-10-13T10:48:26.853 回答
1

对于您希望至少运行一次的内容,您也可以尝试使用 do/while 语句。

代替单词“do”,您可以使用“while (again!='n');' 在那个循环线花括号(删除“while again!='n';”在过程中检查)有一个标准的 while 循环。

while(value==true) { ... }

do { ... } while(value==true);

不过,这将需要一个正确初始化的测试变量。

我在更大的 do/while 循环中加入了第二个 while 循环,以供进一步演示。

#include<iostream>

using namespace std;

int main() {
double num1 = 0, num2 = 0;
char op;
char again;

do {  // set start point for loop
cout << "********C++ CALCULATOR********" << endl;
cout << "Please enter your first number" << endl;
cin  >> num1;
cout << "Please enter your operand (+, -, *, /)" << endl;
cin  >> op;
cout << "Please enter your second number" << endl;
cin  >> num2;

if (op == '+') {
cout << "The answer is: " << num1 + num2 << endl;
} else if (op == '-') {
cout << "The answer is: " << num1 - num2 << endl;
} else if (op == '/') {
cout << "The answer is: " << num1 / num2 << endl;
} else if (op == '*') {
cout << "The answer is: " << num1 * num2 << endl;
} else {
cout << "That was an invalid command!" << endl;
}

cout << "\nRun again? \"y\" or \"n\"" << endl; // prompt user
cin  >> again;

// here is a while loop in do/while statement to check for valid input if the user wants
// to go again 
while(again!='y' && again!='n'){
cout << "Invalid input. Run again? y or n" << endl;
cin >> again;
}

cout << endl;

} while(again!='n'); // now check if user wants to go again // end of do loop
// the condition for while loop could be like
// while (1); // any non zero being true
// if you want executions until program termination

}

}
于 2013-10-13T12:12:28.210 回答
0

只需将所有内容包装成一个无限循环

#include<iostream>

using namespace std;

int main() {
    for (;;) {

        // your code here
    }
}
于 2013-10-13T10:37:33.927 回答