2015-09-19 25 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; 

}