0

I am having trouble with the user input filling up the 0 index in my input movie array. Then when i print them out in the switch statement it leave index 0 blank and only prints the first four user inputs. How can i make userName not be picked up by the for loop? Is there a way to make user name stay in scope of itself?

#include<iostream>
#include<string>
#include "Movie.h"
using namespace std;

string inputMovie[5];
void displayMenu();
string userName;

int main()
{
    char choice;



    {

        cout << "Welcome to the favorite movie program." << endl;
        cout << "What is your name?" << endl;
        cin >> userName;

        cout << "Hello " << userName << " do you have five favorite movies?" << endl;
    }
    cout << "If so, please input your favorite five movies:" << endl;
    for(int i = 0; i < 5; i++) {
        getline(cin, inputMovie[i]);
        if (i < 4) {
            cout << "Please enter a movie: " << endl;
        }

    }
    Movie M1{inputMovie[0]};
    Movie M2{inputMovie[1]};
    Movie M3{inputMovie[2]};
    Movie M4{inputMovie[3]};
    Movie M5{inputMovie[4]};




    do {
        displayMenu();
        cin >> choice;
        while (toupper(choice) < 'A' || toupper(choice) > 'B') {
            cin >> choice;
        }
        switch (choice) {
            case 'a':
            case 'A':
                cout << "Your five all time favorite movies are: " << M1 << ", " << M2 << ", ";
                cout << M3 << ", " << M4 << ".";
//                cout << M1 << endl;
//                cout << M2 << endl;
//                cout << M3 << endl;
//                cout << M4 << endl;
//                cout << M5 << endl;
                cout << endl;
                break;

        }
    }while (toupper(choice) != 'B');
    return 0;
}

void displayMenu()
{
    cout << "             MENU               " << endl;
    cout << "A: Display Your Favorite Movies." << endl;
    cout << "B: Exit Program." << endl;
    cout << "Enter your choice: " << endl;
}
  • Hi String Wade, welcome to StackOverflow. it seems like you haven't included the content of "Movie.h". I guess this is where the class `Movie` is defined. We need all the information to understand your problem. – EvensF Mar 14 '20 at 20:11
  • `cin >> userName;` reads one whitespace-delimited token from the stream. If you input "String Wade", `userName` will get "String". "Wade" winds up in the first movie. It does not remove the whitesppace from the stream, setting up the duplicate. – user4581301 Mar 14 '20 at 20:11

0 Answers0