C ++输入结构:_getch()

时间:2014-11-14 04:44:40

标签: c++ database getch

在C ++中,我试图输入电影的名称和数年的发布并将它们存储在数据库/结构中。在我要求输入标题和年份之前。我让用户使用凭据登录。在这种情况下,用户名是"锈蚀"密码是"锈蚀"。

我遇到的问题是在验证凭据之后,跳过输入数据库/结构的第一个电影标题。我相信这与我使用_getch功能有关,但我并不完全确定。

我的代码如下。我的输出如下:

 Please enter your username
 rusty
 Please enter your password

 Access granted! Welcome rusty

 Enter title: Enter year: (input movie year)
 Enter title: (input movie title)
 Enter year: (input movie year)
 Enter title: (input movie title)
 ....




    #include <iostream>
    #include <string>
    #include <sstream>
    #include <conio.h>
    using namespace std;

    //function prototype
    int username_and_pass();

    #define NUM_MOVIES 6

    struct movies_list{
        string title;
        int year;
    }films[NUM_MOVIES];

    // prototype with function declaration
    void sort_on_title(movies_list films[], int n) 
    {
        movies_list temp; 

        for (int i = 0; i < n - 1; i++)
        {
                if (films[i].title>films[i + 1].title)
                {
                    temp = films[i];
                    films[i] = films[i + 1];
                    films[i + 1] = temp;
                }
        }
    }// end of sort_on_title function

    void printmovie(movies_list movie)
    {
        cout << movie.title;
        cout << " (" << movie.year << ") \n";
    }

    void search_on_title(movies_list films[], int n, string title) 
    {
        bool flag = false; 
        for (n = 0; n < NUM_MOVIES; n++)
        {
            if (films[n].title == title) 
            {
                cout << "Title: " << films[n].title << endl;
                cout << "Year of Release: " << films[n].year << endl;
                cout << "\n";
                flag = true; 
            }
        }
        if (flag == false)
            cout << "Search on title not found!" << endl;
    }// end of search_on_title function

    void search_on_year(movies_list films[], int n, int year)
    {
        bool flag = false; // check on existence of record
        for (n = 0; n < NUM_MOVIES; n++)
        {
            if (films[n].year == year) // display if true
            {
                cout << "Title: " << films[n].title << endl;
                cout << "Year of Release: " << films[n].year << endl;
                cout << "\n";
                flag = true; 
            }
        }
        if (flag = false)
            cout << "Search on title not found!" << endl;
    }// end of search_on_title function


    int menu()
    {
        int choice;
        cout << " " << endl;
        cout << "Enter 1 to search on titles " << endl;
        cout << "Enter 2 to search on years " << endl;
        cin >> choice;
        cout << "\n";
        return choice;
    }// end of menu function


    int username_and_pass()
    {
        string uName;
        string password;
        int value;
        char ch;
        cout << "Please enter your username\n";//"rusty"
        cin >> uName;
        cout << "Please enter your password\n";//"rusty"
        ch = _getch();

        while (ch != 13)//As long as the user doesn't press Enter 
        {//(enter is ASCII code 13) continue reading keystrokes from the screen.
                password.push_back(ch);
                cout << '*';
                ch = _getch();
            }

            if (uName == "rusty" && password == "rusty")
            {
                cout << "\n\nAccess granted! Welcome " << uName << "\n\n" << endl;
                value = 1
            }

            else
            {
                cout << "Invalid credentials" << endl;
                value = 0;
            }
            return value;
    }// end of username_and_pass function


    int main()
    {
        string mystr;
        int n;
        string response;
        int value = 0;

        do
        {
            value = username_and_pass();
        } while (value==0);

        if(value==1)
        {
                for (n = 0; n < NUM_MOVIES; n++)
                {
                    cout << "Enter title: ";
                    getline(cin, films[n].title);
                    cout << "Enter year: ";
                    getline(cin, mystr);
                    stringstream(mystr) >> films[n].year;
                }

                //sorts records
                sort_on_title(films, NUM_MOVIES);

                cout << "\nYou have entered these movies:\n";
                for (n = 0; n < NUM_MOVIES; n++)
                    printmovie(films[n]);

                //menu
                int choice = 0;
                choice = menu();
                if (choice == 1)
                {
                    string searchTerm;
                    cout << "What is the movie you want to search for? " << endl;
                    cin >> searchTerm;
                    cout << " " << endl;
                    search_on_title(films, NUM_MOVIES, searchTerm);
                }

                else
                {
                    int searchTermYear;
                    cout << "What is the year you want to search for? " << endl;
                    cin >> searchTermYear;
                    cout << " " << endl;
                    search_on_year(films, NUM_MOVIES, searchTermYear);
                }

                cout << "Would you like to query the database again? (Y/N)" << endl;
                cin >> response;
                if (response == "Y" || "y")
                {
                    choice = menu();

                    if (choice == 1)
                    {
                        string searchTerm;
                        cout << "What is the movie you want to search for? " << endl;
                        cin >> searchTerm;
                        cout << " " << endl;
                        search_on_title(films, NUM_MOVIES, searchTerm);
                    }

                    else
                    {
                        int searchTermYear;
                        cout << "What is the year you want to search for? " << endl;
                        cin >> searchTermYear;
                        cout << " " << endl;
                        search_on_year(films, NUM_MOVIES, searchTermYear);
                    }
                }
        }
        return 0;
    }

2 个答案:

答案 0 :(得分:1)

刷新输入缓冲区的所有内容。

请转到以下链接以刷新输入缓冲区的内容。

https://stackoverflow.com/a/7898516/4112271

答案 1 :(得分:0)

我不确定是什么原因导致了这个问题。但是你可以尝试使用cin.clear()。在你要求输入标题之前放置它。

       cin.clear();       
       cout << "Enter title: ";
       getline(cin, films[n].title);
       cout << "Enter year: ";
       getline(cin, mystr);
       stringstream(mystr) >> films[n].year;