9 Replies - 1001 Views - Last Post: 06 September 2012 - 06:19 AM Rate Topic: -----

#1 HareCoder  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 05-September 12

Movie data program, pointers, and loops...oh my!

Posted 05 September 2012 - 05:21 PM

The program I am trying to write is supposed to accept input from the user about their 2 favorite movies and the display the information back to them. I want to be able to use a function at the end to display the data back to the user. Here is the code I've written SO FAR; it isn't quite finished.

#include <iostream>
#include <string>

using namespace std;

//Define Structure
struct MovieData{
	string title;
	string director;
	int yearReleased;
	int runningTime;
};

//Define variables
const int NUM_MOVIES = 2; //Data to display 2 movies
MovieData movies[NUM_MOVIES]; //Structure variable
int index; //Loop counter

//Begin Function Protos
void printData(MovieData *object);
//End Function Protos

//Begin Main Function
int main()
{
	//Get the information for the movies
	cout << "Enter the desired information for " << NUM_MOVIES << " of your favorite movies." << endl;
	cout << "NOTE: The running time should be in minutes (i.e. 120 min.).";
	cout << endl;
	
	for (index = 0; index < NUM_MOVIES; index++)
	{
		//Get the title of the movie
		cout << "Title of movie #";
		cout << (index + 1) << ": ";
		getline(cin, movies[index].title);

		cout << "Director of movie #";
		cout << (index + 1) << ": ";
		getline(cin, movies[index].director);

		cout << "Year released: ";
		cin >> movies[index].yearReleased;
		if (movies[index].yearReleased >= 2013 || movies[index].yearReleased <= 1887); 
		{
			do 
			{
				cout << "You have entered an invalid year. Try again: ";
				cin >> movies[index].yearReleased;
			} while (movies[index].yearReleased >= 2013 || movies[index].yearReleased <= 1887);
		}

		cout << "Running time (in minutes): ";
		cin >> movies[index].runningTime;
		cout << endl;
	}

	//Display the data for each movie
	cout << "Here is the data for each movie you entered:" << endl;

	for (index = 0; index < NUM_MOVIES; index++)
	{
		cout << "Movie #" << (index + 1) << endl;
		printData;
	}

	system("pause");
	return 0;
} //End Main Function

//Begin printData Function, which will allow the data to be displayed
//at the end of the program runtime.
void printData(MovieData *object)
{
	cout << "Movie Title: " << object->title << endl;	
	cout << "Director: " << object->director << endl;	
	cout << "Year of Release: " << object->yearReleased <<endl;	
	cout << "Running Time (in minutes): " << object->runningTime << " min." << endl; 
}
//End of Program



I have three issues:

1) My 'for' loop is, for some reason, not allowing the user to input a string for the "Title" after Movie #1. In other words, once the user inputs the running time for the first movie, what SHOULD happen is the program asks for the title of movie #2. What ACTUALLY happens is "Title of Movie #2: Director of Movie #2: " is displayed instead, allowing the user to input a DIRECTOR, but completely skipping the title once the loop starts over.

2) I'm a slightly new programmer, and I have no idea how to call on the printData function to display the information I want it to. I tried including "printData();" at the end of the code right before the return, but it gives errors saying "Too few arguments in function call." I remove the (), and no errors occur on compile, and nothing happens on run, either. Instead, it just displays
Movie #1:
Movie #2:
as it should thanks to the 'for' loop.

3) The error loop for the year is not acting correctly either. When I insert a VALID year, it still tells me the year is invalid and asks to TRY AGAIN. The loop functions correctly from that point on, but the loop shouldn't even be called up if a valid year is inserted (at least that's what I assumed).

Any pointers? (LOL, I just realized the double meaning.)

Is This A Good Question/Topic? 1
  • +

Replies To: Movie data program, pointers, and loops...oh my!

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4068
  • View blog
  • Posts: 12,549
  • Joined: 25-December 09

Re: Movie data program, pointers, and loops...oh my!

Posted 05 September 2012 - 05:41 PM

For your first issue, this is being caused by the mixing of the cin exaction operator>> and getline(). the extraction operator leaves the end of line character in the input buffer which getline() then considers the end of it's input. Therefore you will need to remove this character. One way would be to use cin.ignore().

For your second issue you may want to study the links below. These tutorials should help explain how to properly use C++ functions. Also I recommend that you pass the structure by reference instead of using the pointer.

Next stop using the global variables, you are passing the required variables into the function so make the variables local.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 HareCoder  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 05-September 12

Re: Movie data program, pointers, and loops...oh my!

Posted 05 September 2012 - 05:49 PM

I tried using cin.ignore() before, yet it still gave me the same issue. Where in the code should it be placed? I had it placed right after the last getline(), which would prompt the user to hit the enter key again before moving forward to the Year variable.

Also, I tried using the variables locally and had the same results.

I'm not quite sure what you mean by "passing the structure by reference." Could you define?
Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1343
  • View blog
  • Posts: 4,606
  • Joined: 19-February 09

Re: Movie data program, pointers, and loops...oh my!

Posted 05 September 2012 - 06:16 PM

3. Semicolon at end of line.

44    if (movies[index].yearReleased >= 2013 || movies[index].yearReleased <= 1887); // <- semicolon



.

This post has been edited by #define: 05 September 2012 - 06:16 PM

Was This Post Helpful? 1
  • +
  • -

#5 jimblumberg  Icon User is online

  • member icon


Reputation: 4068
  • View blog
  • Posts: 12,549
  • Joined: 25-December 09

Re: Movie data program, pointers, and loops...oh my!

Posted 05 September 2012 - 06:21 PM

Quote

Where in the code should it be placed? I had it placed right after the last getline()

It needs to be placed after the extraction operator>> and before the next getline() when ever you switch between using the extraction operator>> and getline() you should place an ignore() call to remove the end of line character.

Quote

Also, I tried using the variables locally and had the same results.

The same results as what? Because of how your current program is structured you won't see any difference, but by using local variables you won't be using what is considered a bad programming habit. You should strive to rarely if ever use global variables. Their use will make creating larger programs much harder and you should learn to properly pass variables to and from your functions.

Quote

I'm not quite sure what you mean by "passing the structure by reference." Could you define?

Did you study the links provided in my signature as I suggested? The function tutorials in my signature should help explain how to pass variables by reference in a C++ program.

Jim
Was This Post Helpful? 0
  • +
  • -

#6 HareCoder  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 05-September 12

Re: Movie data program, pointers, and loops...oh my!

Posted 05 September 2012 - 06:44 PM

Quote

It needs to be placed after the extraction operator>> and before the next getline() when ever you switch between using the extraction operator>> and getline() you should place an ignore() call to remove the end of line character.


I got that to work, however after I enter in the "Title" and press Enter, it waits for me to press Enter again before continuing. This does NOT happen during the second movie enter sequence, oddly.

Quote

The same results as what?


The same results as if I were to initialize them globally. Local or global, the same result happens. However, removing the colon after the if statement fixed the loop issue for the year, and having the cin.ignore(INT_MAX, '\n') has allowed the user to input a second movie title now.

Here is the code snippet updated. Is the cin.ignore in the proper place?

for (index = 0; index < NUM_MOVIES; index++)
	{
		//Get the title of the movie
		cout << "Title of movie #";
		cout << (index + 1) << ": ";
		getline(cin, movies[index].title);
		cin.ignore(INT_MAX, '\n');

		cout << "Director of movie #";
		cout << (index + 1) << ": ";
		getline(cin, movies[index].director);
		
		cout << "Year released: ";
		cin >> movies[index].yearReleased;
		if (movies[index].yearReleased >= 2013 || movies[index].yearReleased <= 1887) 
		{
			do 
			{
				cout << "You have entered an invalid year. Try again: ";
				cin >> movies[index].yearReleased;
			} while (movies[index].yearReleased >= 2013 || movies[index].yearReleased <= 1887);
		}

		cout << "Running time (in minutes): ";
		cin >> movies[index].runningTime;
		cout << endl;
	}



The links you provided were somewhat helpful, but they didn't explain how to call upon structure data, only arrays. I'm still at a loss as to how to call upon the information stored within the structure in the main function using the printData function I have defined.
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is online

  • member icon


Reputation: 4068
  • View blog
  • Posts: 12,549
  • Joined: 25-December 09

Re: Movie data program, pointers, and loops...oh my!

Posted 05 September 2012 - 06:53 PM

First you need to think of a structure as a User Defined Data Type. Which means that you pass the structure exactly as you would a regular variable. For instance to pass a structure named MyStruct to a function by reference you would define the function as:

struct MyStruct
{
   int number;
};

void myFunction(MyStruct &test)
{
   test.number = 10;  
}

// and in main call it like:
MyStruct values;
myFunction(values);


Note the ampersand in the function definition and the lack of the ampersand in main when calling the function. And also note that inside the function you would access the structure members the same way you would in the function where you defined the instance of this structure.

Jim
Was This Post Helpful? 1
  • +
  • -

#8 HareCoder  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 05-September 12

Re: Movie data program, pointers, and loops...oh my!

Posted 05 September 2012 - 08:58 PM

Woohoo! That worked Jim! I understand it now. I'm so glad you explained it that way; the tutorial didn't make it 'click' for me. Now the only pesky thing left is I can't get cin.ignore() to behave correctly. Here's what I have.

//Get the title of the movie
		cout << "Title of movie #";
		cout << (index + 1) << ": ";
		getline(cin, movies[index].title);
		cin.ignore();


When I press enter, it moves down to a blank line and awaits another press of Enter instead of simply moving forward to the next line, which would be the director. Is there a better way to clear the buffer? I may have asked that already, sorry, but I'm not having any luck with this tonight.
Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is online

  • member icon


Reputation: 4068
  • View blog
  • Posts: 12,549
  • Joined: 25-December 09

Re: Movie data program, pointers, and loops...oh my!

Posted 05 September 2012 - 09:12 PM

You will only need the ignore() when you switch between cin >> and getline(), for example at the end of your loop:

cout << "Running time (in minutes): ";
cin >> movies[index].runningTime;
/// Put the cin.ignore() here.
cout << endl;


In this program this is the only place that after the cin >> you will have a getline(), the getline() is at the beginning of the loop.

Jim
Was This Post Helpful? 1
  • +
  • -

#10 HareCoder  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 05-September 12

Re: Movie data program, pointers, and loops...oh my!

Posted 06 September 2012 - 06:19 AM

Thank you so much for your help on this program. I successfully got the program to run exactly the way I needed it to. I also added in some functionality to compute the first year's revenue and if it was a profit or loss.

Once again, thanks for putting up with some of my n00b questions Jim, and thanks to everyone else for pointing me in the right direction.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1