I have to write a program for school that has user input ten movie titles and their corresponding ratings. This program must use parrallel arrays. There is more to the program but not needed for the issue I am currently having. I cannot seem to get the string for the movie title to get into the array correctly. When running this piece of the program, I input the first movie title, then the rating, then it continues printing, but skipping the input for the movie title and going directly to the rating for ten instances of the question. I have played with different ways to get this to work correctly, but no luck. Here is the code snippet for this part of the program.

// Program to prompt user to enter ten movie rental choices
// and rate them 1-5.  Convert ratings to number of *'s and
// sort using parallel arrays.

#include <iostream>
#include <string>
#include <cstring>

using namespace std;


int main()
{

	
	cout << "This is a program that will let you enter ten movie rental " << endl;
	cout << "titles and rate them." << endl;
	cout << "Ratings are entered 1 through 5, with 5 being the highest rating. " << endl;
	cout << endl;
	cout << endl;
	
	char movieTitle[10][50];
	int rating[10];
	int j = 0;
	int i = 0;
	
	for (j; j < 10; j++)
	{
	cout << "Enter ten movie rental movie titles and thier corresponding ratings." << endl;
	cin.get(movieTitle[j], 50);
	cout << "Enter a rating for " << movieTitle[j] << "." << endl;
	cin >> rating[i];
	i++;
	}

	

	return 0;
}
Re: Input problem using arrays 80 80

> cin.get(movieTitle[j], 50);
This stops when a newline is found.

> cin >> rating;
This leaves a newline on the input stream, thus screwing up your get() call the next time around.

Look into using say cin.ignore().
Mixing input methods almost always leads to problems such as "missing or skipping input".

A better way would be to read EVERYTHING into a std::string to begin with, then extract what you need from that.

Re: Input problem using arrays 80 80

To read everything into a string to begin with I would have to have ten variables of string then input them into the array movieTitle? I don't think I understand what you are trying to tell me with that statement.

Re: Input problem using arrays 80 80

What he means is don't use cin >> myvar; alongside cin.get( mystring ); The problem comes because doing so breaks the user's input model. When you ask for something, the user types an answer and presses ENTER. The get() method returns the string entered and tosses the ENTER away. The >> method only gets the first thing the user typed and doesn't toss anything away.

You can fix the problem one of two ways:

// fix #1
cin >> rating[ i ];  // try to read an integer
cin.ignore( 10000 );  // ignore everything else and toss the ENTER
// fix #2
#include <sstream>
#include <exception>
...
string s;
...
cin.get( s );  // read everything
if (!(stringstream( s ) >> rating[ i ]))  // then convert to an integer
  throw runtime_error( "Not an integer!" );  // complain if not an integer

Both methods throw an exception if you try to enter something other than an integer. The second method is a tad easier to look at if you want to deal with the error...

Re: Input problem using arrays 80 80

>Both methods throw an exception if you try to enter something other than an integer.
How do you figure that? Only the second will actually throw an exception. The first will simply set cin to an error state (in which case the ignore will effectively be a no-op).

Re: Input problem using arrays 80 80

Oop! You're right. The first doesn't throw an exception...

He'd have to test the first the same way: if (!(cin >> rating[ i ])) fooey(); The second only throws an exception because I threw it after testing the stringstream for the error state the same way...

Re: Input problem using arrays 80 80

I tried to use fix #1 that was suggested, but now it gives me one question and one rating, and seems stuck. After entering the rating for the first movie title, the program goes to the next line, does not repeat in the loop like it should.

The second fix seems more difficult for me because I have not learned about the sstream and exception libraries in class yet. I am still just a lowly beginner.

Here is the snippet of code as I have it now as suggested.

char movieTitle[10][50];
	int rating[10];
	int j = 0;
	int i = 0;
	

	for (j; j < 10; j++)
	{
	cout << "Enter ten movie rental movie titles and thier corresponding ratings." << endl;
	cin.get(movieTitle[j], 50);
	cout << "Enter a rating for " << movieTitle[j] << "." << endl;
	cin >> rating[i];
	cin.ignore(10000);
	i++;
	}
Re: Input problem using arrays 80 80

I'm sorry. I goofed twice. It should read: cin.ignore( 10000, '\n' ); That'll fix it.

Re: Input problem using arrays 80 80

When I insert the code line

cin.ignore( 10000, '\n' );

I get the following error from my compiler.

MovieRentalList.cpp:34: warning: multi-character character constant

Re: Input problem using arrays 80 80

My guess is you got the / instead of the \, as in
cin.ignore( 10000, '/n' );

Are you always pasting the code you're compiling / running, because simply posting guesses doesn't work.

Re: Input problem using arrays 80 80

Thank you, that fixed the issue with the multi character error. It was a / instead of a \. No I don't make it a habit to just paste code because I like to type it all to help me learn exactly how it should be entered.

Re: Input problem using arrays 80 80

You should always copy/paste exactly what is in your editor at that time, and your exact error messages.

Correcting your mistake doesn't do any good, since we're just going to say "it looks fine to me", and you're left in the dark.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.