c++在switch语句中使用cin

c++ using cin in a switch statement

本文关键字:cin 语句 switch c++      更新时间:2023-10-16

c++我正在编写一个带有菜单的程序。我使用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 << "nnWhat 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 << "nnThank 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.nn";
            exit(0);
        }
        return (i);
        fin.close() ;
    }

我接受了您的代码并去掉了主开关逻辑:这很有效。我添加了第二个带有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 << "nnWhat 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();
}