C++ 结构输入:_getch()

C++ Input Into Structure: _getch()

本文关键字:getch 结构 输入 C++      更新时间:2023-10-16

在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 usernamen";//"rusty"
        cin >> uName;
        cout << "Please enter your passwordn";//"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 << "nnAccess granted! Welcome " << uName << "nn" << 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;
    }

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

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

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

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

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