c ++我正在编写一个带菜单的程序。我正在使用do while循环和switch语句来执行菜单。我在不同的情况下调用函数。当我调用一个包含cin的函数时,我的程序会跳过cin。如何在我的switch语句中使用cin?
这是我菜单的功能。在研究之后,我尝试了不同的格式。 案例1,2和5不起作用。
void Execute_Main_Menu(Student &Temp, Student List[], int File_Size, ifstream &fin)
{
char Choice ;
ofstream fout ;
do
{
cout << "\n\nWhat would you like to do? Enter 1-8: " ;
cin >> Choice ;
Student Temp ;
switch(Choice)
{
case '1' :
{
File_Size=Read_Data(List, CAP) ;
Another_Task() ;
break ;
}
case '2' : Open_Output_File(fout) ;
Print_List_To_File(List, File_Size, fout) ;
Another_Task() ;
break ;
case '3' : Sort_Menu() ;
Execute_Sort_Menu(List, File_Size) ;
Another_Task() ;
break ;
case '4' : Print_List(List, File_Size) ;
Another_Task() ;
break ;
case '5' : Print_Student(Temp, List, File_Size) ;
break ;
case '6' : cout << "F" ;
break ;
case '7' : cout << "G" ;
break ;
case '8' : cout << "\n\nThank You. Good Bye." ;
exit (0) ;
default : cout << "\nBad Input. Must be 1-8." ;
}
}
while(Choice != 8);
}
以下是我正在调用的函数中的cin示例
bool Open_Output_File(ofstream &fout)
{
string Output ;
cout << "\nEnter the name of the output file: " ;
getline(cin, Output) ;
fout.open(Output.c_str()) ;
if (fout.fail())
return false ;
else
return true ;
}
void Execute_Sort_Menu(Student List[], int File_Size)
{
char Choice ;
do
{
cout << "\nWhat would you like to do? Enter 1 or 2: " ;
cin >> Choice ;
switch(Choice)
{
case '1' : Sort_B_Last_Name(List, File_Size) ;
Print_List(List, File_Size) ;
break ;
case '2' : Sort_B_Average(List, File_Size ) ;
Print_List(List, File_Size) ;
break ;
default : cout << "\nYou have to enter the number 1 or the number 2." ;
}
}
while(Choice !='1' && Choice !='2');
}
bool Open_Input_File(ifstream &fin)
{
string Input ;
cout << "Enter the name of the input file: " ;
getline(cin, Input) ;
fin.open(Input.c_str()) ;
if (fin.fail())
return false ;
else
return true ;
}
int Read_Data(Student List[], int Size)
{
ifstream fin ;
int i = 0;
if (Open_Input_File(fin)== true )
{
string Course_Name, Course_Id, Location ;
getline(fin, Course_Name) ;
getline(fin, Course_Id) ;
getline(fin, Location) ;
Read_Student(List[i], fin);
while(!fin.eof())
{
i++ ;
if(i == Size)
{
cout << "\nArray is full.\n" ;
return (i);
}
Read_Student(List[i], fin);
}
}
else
{
cout <<"\nBad File Name. Did not open. Program terminated.\n\n";
exit(0);
}
return (i);
fin.close() ;
}
答案 0 :(得分:2)
我拿了你的代码并删除了主交换机逻辑:这是有效的。我添加了第二个带有cin语句的函数,它调用得恰当。
#include <iostream>
using namespace std;
void another_function_with_cin()
{
char Choice;
cout << "What should I echo? ";
cin >> Choice;
cout << "Echoing: " << Choice << std::endl;
}
void Execute_Main_Menu()
{
char Choice ;
do
{
cout << "\n\nWhat would you like to do? Enter 1-8: " ;
cin >> Choice ;
switch(Choice)
{
case '1' :
{
cout << 1 << endl;
another_function_with_cin();
break ;
}
case '2' :
{
cout << 2 << endl;
another_function_with_cin();
break;
}
case '3' :
{
cout << 3 << endl;
another_function_with_cin();
break;
}
case '4' :
{
cout << 4 << endl;
another_function_with_cin();
break;
}
case '5' :
{
cout << 5 << endl;
another_function_with_cin();
break;
}
case '6' :
{
cout << 6 << endl;
another_function_with_cin();
break;
}
case '7' :
{
cout << 7 << endl;
another_function_with_cin();
break;
}
case '8' :
{
cout << 8 << endl;
another_function_with_cin();
break;
}
default : cout << "\nBad Input. Must be 1-8." ;
}
}while(Choice != 8);
}
int main(int argc, char const *argv[])
{
Execute_Main_Menu();
return 0;
}
我怀疑你是如何使用getline的。您也可以尝试使用cin作为字符串...
std::string filename;
cin >> filename;
此外,当您打开文件时,如果成功,您应该记得关闭它。
std::ofstream fout(filename.c_str());
if(fout.is_open)
{
fout.close();
}