2017-04-08 14 views
-1

好吧,我可能沒有解釋得很好,但基本上我用switch語句編寫了一個基於菜單的程序,我希望整個程序在選擇某個選項並執行其中的所有功能後再次啓動。 這裏舉一個例子代碼:如何保持我的程序輸出終止並再次啓動程序而不丟失數據?

#include <stdio.h> 
#include <stdlib.h> 
#include <iostream> 

using namespace std; 



int main() { 
    string name; 
    int num,age,cls; 
    cout <<"Enter your name: "; 
    cin>>name; 
    cout <<"Enter your number: "; 
    cin>>num; 
    cout<<"Enter your age: "; 
    cin>>age; 
    cout <<"Enter your class: "; 
    cin>>cls; 
    int choice; 
    cout<<"\tPlease choose an option: "<<endl; 
    cout <<"1. Display my name\n2. Display my number\n3. Display my age\n4. 
Display my class"<<endl; 
    cin>>choice; 
    switch (choice){ 
     case 1: 
      cout<<"Your name is :"<<name; 
      break; 
     case 2: 
      cout <<"Your number is: "<<num; 
      break; 
     case 3: 
      cout <<"Your age is: "<<age; 
      break; 
     case 4: 
      cout <<"Your class is: "<<cls; 
      break; 
     default: 
      cout <<"Invalid option!"; 
      break; 
    } 
    return 0; 
} 

我該怎麼辦,從在年底終止保持程序後一個選項是選擇,而是對另一種選擇再次顯示整個菜單中進行選擇。或者在給出無效輸入並且默認情況下執行後,再次顯示整個菜單。

+1

爾。你會用循環? – melpomene

+0

學習循環的時間 – stark

+0

不,我聽說有一個功能可以做到這一點。我真的不知道,那就是我所問的。也許它是getch();或者其他。 – slowjoe44

回答

1

你只需要添加一個while循環並支持一個菜單選項來退出循環。

int main() { 
    string name; 
    int num,age,cls; 
    cout <<"Enter your name: "; 
    cin>>name; 
    cout <<"Enter your number: "; 
    cin>>num; 
    cout<<"Enter your age: "; 
    cin>>age; 
    cout <<"Enter your class: "; 
    cin>>cls; 

    int choice; 

    bool exitRequested = false; 
    while (!exitRequested) 
    { 
     cout<<"\tPlease choose an option: "<<endl; 
     cout <<"1. Display my name\n2. Display my number\n3. Display my age\n4. Display my class\n5. Exit program"<<endl; 

     cin>>choice; 
     switch (choice){ 
      case 1: 
       cout<<"Your name is :"<<name; 
       break; 
      case 2: 
       cout <<"Your number is: "<<num; 
       break; 
      case 3: 
       cout <<"Your age is: "<<age; 
       break; 
      case 4: 
       cout <<"Your class is: "<<cls; 
       break; 
      case 5: 
       exitRequested = true; 
       break; 
      default: 
       cout <<"Invalid option!"; 
       break; 
     } 
    } 
    return 0; 
} 
0
<pre>*you can use a while loop with a counter variable for simplicity* 
/int main() { 
    string name; 
    int num,age,cls; 
    cout <<"Enter your name: "; 
    cin>>name; 
    cout <<"Enter your number: "; 
    cin>>num; 
    cout<<"Enter your age: "; 
    cin>>age; 
    cout <<"Enter your class: "; 
    cin>>cls; 
    int choice,k=1; 
while(k>0) 
{ 
    cout<<"\tPlease choose an option: "<<endl; 
    cout <<"1. Display my name\n2. Display my number\n3. Display my age\n4. 
Display my class"<<endl; 
    cin>>choice; 
    switch (choice){ 
     case 1: 
      cout<<"Your name is :"<<name; 
      break; 
     case 2: 
      cout <<"Your number is: "<<num; 
      break; 
     case 3: 
      cout <<"Your age is: "<<age; 
      break; 
     case 4: 
      cout <<"Your class is: "<<cls; 
      break; 
     default: 
      cout <<"Invalid option!"; 
      break; 
    } 
cout<<("do you want to renter the details and run again:\n"); 
cout<<("enter 1 for yes and o for no"); 
cin>>k; 
} 
    return 0; 
} 

>

相關問題