C++中有关cin的一个问题?
我在做一个练习时有这么一个代码段:
……
enum Choices { TEXTFILE = 1, UPDATE, NEW, DELETE, END };
int choice;
while ( ( choice = enterChoice() ) != END ) {
switch ( choice ) {
……
其中enterChoice()函数如下
int enterChoice()
{
cout << "\nEnter your choice" << endl
<< "1 - store a formatted text file of accounts\n"
<< " called \"print.txt\" for printing\n"
<< "2 - update an account\n"
<< "3 - add a new account\n"
<< "4 - delete an account\n"
<< "5 - end program\n? ";
int menuChoice;
cin >> menuChoice;
return menuChoice;
}
程序可以编译,运行。
当提示输入时,我输入数字,程序正常运行,但是我输入了字母就进入无限循环。想了半天不知道问题究竟是怎么了。估计是
int menuChoice;
cin >> menuChoice;
return menuChoice;
这里有点问题,应该输入int型但输入了字符型。请问如何解决这个问题呢?