Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/155.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+;中循环到顶部+;?你能给我举个例子吗?_C++ - Fatal编程技术网

C++ 如何在C+;中循环到顶部+;?你能给我举个例子吗?

C++ 如何在C+;中循环到顶部+;?你能给我举个例子吗?,c++,C++,我想让我的计算器在完成计算后返回顶部?我试过while循环,看过关于它的教程,但我就是不能把它放在上下文中 如果你能告诉我如何在这个程序中实际使用它,那就太棒了 #include<iostream> using namespace std; int main() { double num1, num2; char op; cout << "********C++ CALCULATOR********" << endl; cout <&l

我想让我的计算器在完成计算后返回顶部?我试过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;
  }
}
#包括
使用名称空间std;
int main(){
双num1,num2;
char op;

难道不能把所有的东西都包装成一个无止境的循环吗

#include<iostream>

using namespace std;

int main() {
    for (;;) {

        // your code here
    }
}
#包括
使用名称空间std;
int main(){
对于(;;){
//你的代码在这里
}
}

我想你想要这样的东西:

#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;
  }
}
#包括
使用名称空间std;
int main(){
双num1=0,num2=0;
char op='';
char-answer='';
while(answer!='n'){//检查条件

cout对于要至少运行一次的内容,也可以尝试do/while语句

你可以用“while(再一次!='n');”代替单词“do”,在循环线处用大括号(去掉“while!='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

}

}
#包括
使用名称空间std;
int main(){
双num1=0,num2=0;
char op;
再次烧焦;
不{//设置循环的起点

我基本上可以这样做,而('10'='10){//code here}@AlanKong:不,你没有。你应该向我们展示你的实际尝试。我们不会(读:不应该)在这里为你提供免费的解决方案。你能解释for循环的含义吗?for循环看起来像“for(初始化;条件;增量-减量){/*body*/}”.initialization-只发生一次-当您进入循环时。condition-是检查每个新迭代的规则。increment-decreation-修改迭代变量。Body-重复的代码。例如,以下循环:For(int i=0;i<10;++i){cout@AlanKong:你应该看看你的书。非常感谢。非常感谢。