C++ Input Into Structure: _getch()

453 views Asked by At

In C++, I'm trying to input movie's names and years of releasing and store them in a database/structure. Before I ask for the titles and years to be inputted. I have the user log on with credentials. In this case, the username is "rusty" and the password is "rusty".

The issue I'm having is the after the credentials are verified, the first movie title to input into the database/structure is skipped. I believe this has something to do with me using the _getch function, but I'm not totally sure.

My code is below. My output looks like this:

 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

There are 2 answers

1
Amol Bavannavar On BEST ANSWER

Flush all the content's of input buffer.

Please go to following link to flush contents of input buffer.

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

0
Nikhil On

I am not sure what causing you this problem.But can you try using cin.clear(). Place it before you ask for input of title.

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