2

This is my code I don't really see the problem here I'm sure its something simple that I just need another fresh pair of eyes to find.

#include <iostream>
#include <string>

using namespace std;

//Struct
struct MovieData
{
    string title;
    string director;
    int year;
    int runTime;
    double productionCost;
    double fYRevenue;

};
//Prototypes
MovieData getMovieData();
void printMovieData(MovieData );

int main()
{
    //Variables
    MovieData m1, m2;

    cout << "Enter data for movie 1:";
    m1 = getMovieData(); //call get data

    cout << "Enter data for movie 2:";
    m2 = getMovieData(); // Call get data

    printMovieData(m1); //Call print
    printMovieData(m2); //Call print

    return 0;
}
/*
Passes a struct of type movie data and 
collects data needed to print out 
later on
*/
MovieData getMovieData()
{
    MovieData m;
    char line;

    cout << "\n\nWhat is the title of the movie?: ";
    getline(cin, m.title);

    cout << "\nWho was the director of the movie?: ";
    getline(cin, m.director);

    cout << "\nWhat year was the movie made?: ";
    cin >> m.year;

    cout << "\nHow long is the movie in minutes?: ";
    cin >> m.runTime;

    cout << "\nHow much did it cost to produce this movie?: ";
    cin >> m.productionCost;

    cout << "\nHow much did the movie make in its first year?: ";
    cin >> m.fYRevenue;
    cout << "\n";

    return m;
}
/*
Passes a variable of type movie data
then prints out the information inside
*/
void printMovieData(MovieData m)
{
    cout << "\n\nThe movie data for " << m.title << " is as follows.\n"; 
    cout << "Title: " << m.title;
    cout << "\nDirector: " << m.director;
    cout << "\nYear Made: " << m.year;
    cout << "\nRunning Time: " << m.runTime;
    cout << "\nProduction cost: " << m.productionCost;
    cout << "\nRevenue: " << m.fYRevenue ;

}

If you guys can find the problem I'd greatly appreciate it I've been messing with it for a bit and I just cannot understand the issue here. The second time getMovieData is called the first title is a blank line. However when I had them as cin << m.title; etc it was skipping the entire function to the end when I entered input with two parts such a The Flood same with the Directors name Carl Simons would skip the entire thing as well.

leon17848
  • 21
  • 3
  • 1
    state what is the problem(no question asked) – Creris May 03 '15 at 21:23
  • When you mix getline and formatted input you need to make sure to remove the newline after the formatted input is done otherwise the next getline will be an empty line. Presumably right after you read the revenue, but you should show an example of your input for better answers. http://stackoverflow.com/questions/25475384/when-and-why-do-i-need-to-use-cin-ignore-in-c – Retired Ninja May 03 '15 at 21:27
  • 1
    possible duplicate of [std::cin:: and why a newline remains](http://stackoverflow.com/questions/15904963/stdcin-and-why-a-newline-remains) – Raymond Chen May 03 '15 at 22:09

3 Answers3

0

You are mixing the use of cin >> and getline within your getMovieData function. This leads to confusing behaviour because cin >> does not consume the trailing newline from the input buffer (when you read the revenue for the first movie), while getline does not expect to see the trailing newline remaining in the buffer (when you read the title for the next movie).

Use getline for all user input and this problem will go away. If you need to read a number, use getline along with stoi or other numeric conversion function. For example:

std::string input;
getline(cin, input);
m.fYRevenue = stoi(input);

If you read several integers, you can wrap the above lines into a function of its own.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
0

Don't forget to clear out your buffer!

its not perfect but its a start for you to work with...

MovieData getMovieData()
{
    MovieData m;
    char line;

    //you have to clear out your buffer here
    cin.ignore();
    cin.sync();

    cout << "\n\nWhat is the title of the movie?: ";
    getline(cin, m.title);

    cout << "\nWho was the director of the movie?: ";
    getline(cin, m.director);

    cout << "\nWhat year was the movie made?: ";
    cin >> m.year;

    cout << "\nHow long is the movie in minutes?: ";
    cin >> m.runTime;

    cout << "\nHow much did it cost to produce this movie?: ";
    cin >> m.productionCost;

    cout << "\nHow much did the movie make in its first year?: ";
    cin >> m.fYRevenue;
    cout << "\n";

    return m;
}
lopezdp
  • 1,538
  • 3
  • 21
  • 38
-1

Try :

MovieData getMovieData()
{
    MovieData m;
    char line;

    cout << "\n\nWhat is the title of the movie?: ";
    cin>> m.title;

    cout << "\nWho was the director of the movie?: ";
    cin >> m.director;

    cout << "\nWhat year was the movie made?: ";
    cin >> m.year;

    cout << "\nHow long is the movie in minutes?: ";
    cin >> m.runTime;

    cout << "\nHow much did it cost to produce this movie?: ";
    cin >> m.productionCost;

    cout << "\nHow much did the movie make in its first year?: ";
    cin >> m.fYRevenue;
    cout << "\n";

    return m;
}
Tanner
  • 22,205
  • 9
  • 65
  • 83
  • This answer has a problem when reading the title of a movie that has more than one word. The question states this in the last sentence ("it was skipping the entire function to the end when I entered input with two parts"). – Greg Hewgill May 04 '15 at 22:49