2015-09-19 3 views
-2

Недавно я начал программирование на 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"); 
    } 
} 
+6

Для того, чтобы «петля в начале», C++ предоставляет ряд операторов цикла. Вы можете прочитать о них в своем любимом учебнике на C++. –

ответ

1

Вы можете поместить код внутри цикла:

while(true) { 
    ... (existing code goes here) 

    if (answer != 'yes' && answer != 'Yes') 
     break; 

} 
Смежные вопросы