-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;
}
我該怎麼辦,從在年底終止保持程序後一個選項是選擇,而是對另一種選擇再次顯示整個菜單中進行選擇。或者在給出無效輸入並且默認情況下執行後,再次顯示整個菜單。
爾。你會用循環? – melpomene
學習循環的時間 – stark
不,我聽說有一個功能可以做到這一點。我真的不知道,那就是我所問的。也許它是getch();或者其他。 – slowjoe44