0

I have a kind of odd problem with my function inputData. It works fine for the first movie's input. However, when it goes to do the second movie it skips over asking you to enter in the second movie's name and goes straight to prompting you to enter in the second movie's sales. The movie's name needs to be able to include spaces. Any ideas? Anything is greatly appreciated. Thank you

struct Movie{
string movieName;
double firstWeek;
double secondWeek;
double thirdWeek;
double fourthWeek;
double total;
double avg;

};

// prototypes

 void dispMenu();
    char menuChoice();
    void inputData(Movie *mov);
    float getValidSale();
    void TotalAvgSales(Movie *mov);
    void displayMovieData(Movie mov);

int main() {

cout << "Welcome to Movie Data\n";

// movie 1 and 2 structure variables
Movie movie1, movie2;

char choice = 'B';
do { // loop while not D
    choice = menuChoice(); // get menu choice

    switch (choice) // menu options
    {
        case 'A':
        case 'a':
            // input for first movie
            cout << "First Movie\n";
            inputData(&movie1);
            // input for second movie
            cout << endl << "Second Movie\n";
            inputData(&movie2);
            break;
        case 'B':
        case 'b':
            // compute for movie 1
            TotalAvgSales(&movie1);
            // compute for movie 2
            TotalAvgSales(&movie2);
            cout << "Completed!\n";
            break;
        case 'C':
        case 'c':
            // display for first movie
            cout << "First Movie\n";
            displayMovieData(movie1);
            // display for second movie
            cout << "Second Movie\n";
            displayMovieData(movie2);
        case 'd':
            exit;
    }
} while (choice != 'D');

}

// displays the menu

void dispMenu(){
    cout << "Menu Options:\n";
    cout << "A) Add Data\n";
    cout << "B) Compute Total and Avg\n";
    cout << "C) Display Data\n";
    cout << "D) Quit\n";
}

// gets menu choice and verifies that it is valid input

char menuChoice(){
    char choice; // option selected
    dispMenu(); // displays menu
    cin >> choice;
    choice = toupper(choice); // if they enter lowercase, changes it to uppercase
    cin.ignore(1, '\n'); // ignores input buffer

    while ( choice < 'A' || choice > 'D') // make sure it is A B C or D
    {
        dispMenu(); // display menu
        cin >> choice;
        choice = toupper(choice); // if enter lower case, make upper case
        cin.ignore(1, '\n'); // ignore input buffer
    }
    return choice;
}

float getValidSale(){
    float sale;
    cout << "Please enter in the number of tickets sold...\n";
    cin >> sale;

    while ( sale < 0){
        cout << "Please enter in the number of tickets sold (>0)...";
        cin >> sale;
    }
    return sale;
}

here is the problem function below

void inputData(Movie *mov){
    // enter movie name
    cout << "Please enter in Movie name...";
    getline (cin, mov->movieName);

    // enter first week sales
    cout << "First Week Number of Tickets Sold...\n";
    cout << "Please enter the number of tickets sold...";
    cin >> mov->firstWeek;

    // verify input >0a
    while(mov->firstWeek < 0){
        cout << "Please enter in the number of tickets sold (>0)...";
        cin >> mov->firstWeek;
    }

    // enter second week sales
    cout << "Second Week Number of Tickets Sold...\n";
    cout << "Please enter the number of tickets sold...";
    cin >> mov->secondWeek;

    // verify input >0
    while(mov->secondWeek < 0){
        cout << "Please enter in the number of tickets sold (>0)...";
        cin >> mov->secondWeek;
    }

    // enter third week sales
    cout << "Third Week Number of Tickets Sold...\n";
    cout << "Please enter the number of tickets sold...";
    cin >> mov->thirdWeek;

    // verify input >0
    while(mov->thirdWeek < 0){
        cout << "Please enter in the number of tickets sold (>0)...";
        cin >> mov->thirdWeek;
    }

    // enter second week sales
    cout << "Fourth Week Number of Tickets Sold...\n";
    cout << "Please enter the number of tickets sold...";
    cin >> mov->fourthWeek;

    // verify input >0
    while(mov->fourthWeek < 0){
        cout << "Please enter in the number of tickets sold (>0)...";
        cin >> mov->fourthWeek;
    }
}

// calculate the average sales

void TotalAvgSales(Movie *mov){
    // adding up the sales of each week
    mov -> total = mov->firstWeek + mov->secondWeek + mov->thirdWeek + mov->fourthWeek;
    // finding the average
    mov -> avg = mov->total/4;
}

// display the data

void displayMovieData(Movie mov)
{
    cout << fixed << setprecision(2);
    cout << "First Movie\n";
    cout << "Division Name: " << mov.movieName << endl;
    cout << "First Quarter Sales: "<< mov.firstWeek << endl;
    cout << "Second Quarter Sales: "<< mov.secondWeek << endl;
    cout << "Third Quarter Sales: " << mov.thirdWeek << endl;
    cout << "Fourth Quarter Sales: "<< mov.fourthWeek << endl;
    cout << "Total Number of Tickets Sold: " << mov.total << endl;
    cout << "Average Number of Tickets Sold: " << mov. avg << endl;
}
sydneeod
  • 39
  • 2
  • The shown code uses both `std::getline` and `>>` to read input. You discover little surprises like this if you don't fully understand how they work. Lesson learned: if your task at hand is to read a line of input, this is what `std::getline` is for, and not `>>`. This is what `std::getline`'s job is: input a line. The `>>` operator does not, I repeat, ***does not*** input a line of text. It does something subtly different. If you are not aware of ***exactly*** what it does, hillarity, of this nature, ensues. Replace all your `>>`s with `std::getline`s. See the linked question for more info. – Sam Varshavchik Apr 16 '20 at 01:31

1 Answers1

0

I believe I fixed it. Right above the getline (cin, mov->movieName); i added a cin.clear(); and a cin.ignore(); This seems to work.

sydneeod
  • 39
  • 2