> cin.get(movieTitle[j], 50);
This stops when a newline is found.
> cin >> rating[i];
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.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
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. Theget() 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...
Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
>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).
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
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...
Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
I'm sorry. I goofed twice. It should read:
cin.ignore( 10000, '\n' );
That'll fix it.
Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
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.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
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.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953