C++ switch 语句回到主菜单后,如何能让第一次执行所保留的值下次可以继续使用?0
xbtyxjam2017.01.02浏览45次分享举报
#include
using namespace std;
int input()
{
char name;
float weight;
int age;
char favouritefood;
cout<< "please input the name, weight(kg), age, favourite food in order\n";
cin >> name >> weight >> age >> favouritefood;
return 0;
}
int read()
{
char name;
float weight;
int age;
char favouritefood;
cout<< "the name is" << name;
cout<< "\n the weight is" << weight << "(kg)";
cout<< "\n the age is" << age;
cout<< "\n the favourite food is" << favouritefood;
return 0;
}
——————下面是重点————————
int main()
{
char choice;
cout<< "what do you want? \n";
cout<
cin >> choice;
switch (choice)
{
case 'a':
input();
return main();
case 'b':
read ();
return main();
case 'q':
cout<< "Thank you. Goodbye!!!";
default:
break;
}
}
我想要的是第一次我输入a保存值之后,我接着输入b可以读到之前的值,但是实际中我的第一次选了a之后跳出循环,下次再选b的时候上次输入的值就得不到了。求大神帮忙!