C++ c++;在switch语句中使用cin

C++ c++;在switch语句中使用cin,c++,C++,我正在写一个带有菜单的程序。我使用do while循环和switch语句来执行菜单。我在不同的情况下调用函数。当我调用一个包含cin的函数时,我的程序跳过cin。如何使cin在switch语句中工作 这是我菜单的功能。经过研究,我尝试了不同的格式,但都不起作用。 情况1、2和5不起作用 void Execute_Main_Menu(Student &Temp, Student List[], int File_Size, ifstream &fin) {

我正在写一个带有菜单的程序。我使用do while循环和switch语句来执行菜单。我在不同的情况下调用函数。当我调用一个包含cin的函数时,我的程序跳过cin。如何使cin在switch语句中工作

这是我菜单的功能。经过研究,我尝试了不同的格式,但都不起作用。 情况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);
    }
void Execute_主菜单(学生和临时工、学生列表[]、int文件大小、ifstream和fin)
{
字符选择;
流式流量计;
做
{
选择;
学生临时工;
开关(选择)
{
案例“1”:
{
文件大小=读取数据(列表、CAP);
另一项任务();
打破
}
案例“2”:打开输出文件(fout);
将列表打印到文件(列表、文件大小、fout);
另一项任务();
打破
案例“3”:排序菜单();
执行排序菜单(列表、文件大小);
另一项任务();
打破
案例“4”:打印列表(列表、文件大小);
另一项任务();
打破
案例“5”:打印学生(临时、列表、文件大小);
打破

案例“6”:cout我取出了您的代码并删除了主开关逻辑:这是可行的。我添加了第二个带有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;
}
此外,当您打开一个文件时,如果成功,您应该记住关闭它

std::ofstream fout(filename.c_str());
if(fout.is_open)
{
    fout.close();
}

您可能是在最初选择后因为读取单个
char
s off
std::cin
,而在不减少特定类型的空格的情况下提取换行符。同样可能是
std::cin
由于某种原因设置了失败或错误位。但前者的可能性更大。fi首先,非常感谢您的回复。我认为您认为getline的问题是对的。我希望继续使用getline,以防有人想输入多个单词。您知道为什么getline不起作用吗?
std::string filename;
cin >> filename;
std::ofstream fout(filename.c_str());
if(fout.is_open)
{
    fout.close();
}