简体   繁体   English

C++ 面向菜单程序的困难

[英]Difficulty in a menu oriented program for C++

I am having an issue when trying to use a getline command where a user can enter in a movie and then add to the collection of movies (stored in "movies.txt")我在尝试使用 getline 命令时遇到问题,用户可以在其中输入电影然后添加到电影集合(存储在“movies.txt”中)

My code is compiling, but it starts out with the 3rd case automatically.我的代码正在编译,但它自动从第 3 种情况开始。 When I press "q" to quit that case, it reverts to the menu, yet when I try and write out the file or print the collection, no movie titles have been saved.当我按“q”退出这种情况时,它会返回到菜单,但是当我尝试写出文件或打印集合时,没有保存任何电影标题。 Where I should go from here?我应该从这里去哪里? I feel like I'm on the cusp of understanding this.我觉得我正处于理解这一点的风口浪尖上。

    #include <iostream>
    #include <string>
    #include <fstream>
    using namespace std;

    const int ARRAY_SIZE = 200;
    string movieTitle [ARRAY_SIZE];
    int loadData (string pathname);
    int writeData (string pathname);
    int getTitle (string movieTitle[]);
    void showAll (int count);

    int main()
    {
        loadData("movies.txt");
        char userInput;
        string movieTitle[ARRAY_SIZE];
        int count = getTitle(movieTitle);

        bool endOfProgram = false;
        while (endOfProgram ==false)
        {
            cout << "1. Read in Collection" << endl;
            cout << "2. Print Collection" << endl;
            cout << "3. Add a Movie to the Collection" << endl;
            cout << "4. Write out Collection" << endl;
            cout << "5. Quit the Program" <<endl;
            cin >> userInput;

            switch(userInput)
            {
                case('1'): 
                {
                    loadData("movies.txt");
                    break;
                }

                case('2'):
                {
                    showAll(loadData("movies.txt"));
                    break;
                }
                case('3'):
                {
                    cout << getTitle(movieTitle);
                    break;
                }
                case('4'):
                {
                    cout <<"Write out Collection" << endl;
                    writeData("movies.txt");
                    break;

                case('5'):
                {
                    endOfProgram=true;
                    cout << "Have a nice day" <<endl;
                    break;
                }
            }
            }
        }
    }
    int loadData (string pathname)
    {
        int count = 0;
        ifstream inFile;
        inFile.open(pathname.c_str());

        if (!inFile)
            return -1;
        else
        {
            while(!inFile.eof())
            {
                getline(inFile, movieTitle[count]);
                count++;
            }
        }
        return count;
    }

    int writeData (string pathname)
    {
        ofstream outfile;
        outfile.open("movies.txt");
        if(!outfile.is_open())
        {
            cout << "Cannot open movies.txt" << endl;
            return -1;
        }

        outfile.close();
        return 0;
    }
    void showAll (int count)
    {
        cout << "\n";
        for (int i=0; i< count; i++)
        {
            cout << movieTitle[i] << endl;
        }
        cout << "\n";
    }

    int getTitle (string movieTitle[])
    {
        string movie;
        int count = 0;

        while(true)
        {
            cout <<"Enter Movie Titles (Type 'q' to quit)" <<endl;
            cin >> movie;
            if (movie == "q")
            {
                break;
            }
            movieTitle [count] = movie;
            count++;
        }
        return count;
    }

I believe cin reads until eol is found, ie the user presses return.我相信 cin 会一直读取直到找到 eol,即用户按下回车键。 So look for integer in the userInput variable, and pass that to your switch statement因此,在 userInput 变量中查找整数,并将其传递给您的 switch 语句

int nr = atoi(userInput.c_str())
switch(nr){
    case 1:
    case 2: etc ...

In your codes it is not clear why it directly goes to case '3'.在您的代码中,不清楚为什么它直接进入案例“3”。 It should wait for a user input first.它应该首先等待用户输入。 Seems like something already available in buffer.似乎缓冲区中已经有一些可用的东西。 Just put one cout statement in case '3': and check what it print.只需在 case '3': 中放置一个cout语句,然后检查它打印的内容。 If possible put break point there and run the application in debug mode and check the value.如果可能,在那里放置断点并在调试模式下运行应用程序并检查值。 Hope this will help you.希望这会帮助你。

     case('3'):
            {
                cout<<"Value of userInput is: "<<userInput<<endl; 
                cout << getTitle(movieTitle);
                break;
            }

Alternately you can add the below line of code just before cin like below或者,您可以在cin之前添加以下代码行,如下所示

std::cin.clear();
cin >> userInput;

I recommend inputting an integer instead of a character for your input.我建议为您的输入输入一个整数而不是一个字符。 You will need to change the case values too:您还需要更改case值:

int selection = 0;
//...
cin >> selection;
switch (selection)
{
  case 1: 
  //...
}

You won't have to worry about characters in the buffer.您不必担心缓冲区中的字符。 The stream will fail if an integer is not read.如果未读取整数,则流将失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM