[英]How do I loop back to the beginning in c++
I recently started programming in C++, so for my program I need to loop back to the beginning when the user says yes and end the program when the user says no.我最近开始用 C++ 编程,所以对于我的程序,我需要在用户说是时循环回到开始,并在用户说否时结束程序。 I was wondering how would I loop back to the beginning?
我想知道我将如何循环回到开头?
#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");
}
}
You can put the code inside a loop:您可以将代码放入循环中:
while(true) {
... (existing code goes here)
if (answer != 'yes' && answer != 'Yes')
break;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.