如何在c++中循环回到开头
How do I loop back to the beginning in c++
我最近开始用c++编程,所以对于我的程序,当用户说"是"时,我需要循环回到开始,当用户说"否"时,我需要结束程序。我在想怎么才能回到开头?
#include <iostream>
using namespace std;
int main() {
int x;
int y;
char yes, no, answer;
{
cout << "Please enter a number for the x coordinate" << endl;
cin >> x;
cout << "Please enter a number for the y coordinate" << endl;
cin >> y;
if (x == 0) {
if (y == 0)
cout << "you are on the origin" << endl;
else
cout << "you are on the y axis" << endl;
}
else if (x > 0) {
if (y == 0)
cout << "you are on the x coordinate" << endl;
else if (y > 0)
cout << "you are in the 1st quadrant" << endl;
else
cout << "you are in the 4th qaudrant" << endl;
}
else if (x < 0) {
if (y > 0)
cout << "you are in the 2nd quadrant" << endl;
else if (y < 0)
cout << "you are in the 3rd quadrant" << endl;
}
cout << "Do you want to run the program again either types yes or no" << endl;
cin >> answer;
if (answer == 'yes' || answer == 'Yes')
system("pause");
}
}
可以将代码放入循环中:
while(true) {
... (existing code goes here)
if (answer != 'yes' && answer != 'Yes')
break;
}
相关文章:
- 如何循环打印顶点结构
- 如何在C++中从两个单独的for循环中添加两个数组
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 正在尝试了解输入验证循环
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 循环后如何继续阅读
- Ardunio UNO解决了多个重叠的定时器循环
- Eigen如何在容器循环中干净地附加矩阵
- 在某些循环内使用vector.push_back时出现分段错误
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- 为什么我的for循环不能正确获取argv
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- while循环中while循环的时间复杂度是多少
- 如何找到循环列表的开头?
- 循环回到z之后的字母表的开头
- cout 切断 for 循环中的字符串开头
- 在情况下,如何循环回到开关语句的开头
- 如何循环回到开关用例语句C++的开头
- C++ 不以 0 开头的循环
- 如何在c++中循环回到开头