簡體   English   中英

C ++輸入結構:_getch()

[英]C++ Input Into Structure: _getch()

在C ++中,我試圖輸入電影的名稱和發行年份,並將它們存儲在數據庫/結構中。 在我要求輸入標題和年份之前。 我讓用戶使用憑據登錄。 在這種情況下,用戶名是“ rusty”,密碼是“ rusty”。

我遇到的問題是在驗證憑據后,跳過了要輸入到數據庫/結構中的第一個電影標題。 我相信這與使用_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;
    }

刷新輸入緩沖區的所有內容。

請轉到以下鏈接刷新輸入緩沖區的內容。

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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM