-2

Beginner programmer here, I am having issues with the string input structure of the program -- I am having trouble successfully storing the string user input for year, genre, and director. I tried not using cin >> with getline, and just use only get() and getline() -- but the error still persists with the string inputs not being stored properly. EX: If user enters choice 'B', and stores the year string input "2015" into string variable whatYear, it results in an error of not being stored properly. How to fix?

#include <iostream>
#include <cctype>
#include <string>

void printYear();
void printGenre();
void printDirector();

using namespace std;

int main(){

char choice;

The user will enter a choice: 'B' for year, 'C' for genre, and 'D' for director.

cout << "Hello movie'goer, please select one of the following choices:\n\n"
 << "B - Display movies in a specific year\n"
 << "C - Display movies in a specific genre\n"
 << "D - Display movies from a specific director\n\n"
 << "Enter your choice: ";

choice = cin.get();

//Input validation
while (toupper(choice) != 'B' && toupper(choice) != 'C' &&
   toupper(choice) != 'D') {

cout << "Choice is invalid, the choice must be one of the following:'B',   'C', or"
     << " 'D'. Re-enter your choice: ";
 choice = cin.get();
}

cout << endl << endl;

This is where it processes the choice. 'B' will print the string year input, 'C' will print the string genre input, and 'D' will print the string director's name input.

switch(choice)
{
case 'a':
case 'A': break;
case 'b':
case 'B': printYear(); break;
case 'c':
case 'C': printGenre(); break;
case 'd':
case 'D': printDirector(); break;
}

}

When choice 'B' is inputted for year, I am having trouble storing the year input into the string variable whatYear.

void printYear()
{
string whatYear;
cout << "Enter a specific year: " << endl;
getline(cin, whatYear);

I checked to see if the string input was stored successfully, but it was a failure.

cout << "The input stored in string whatYear is: " << whatYear;
}

I am having the same problem here as well with the string input not being stored successfully into the string variable whatGenre.

 void printGenre()
{
string whatGenre;
cout << "Enter a specific genre: " << endl;
getline(cin, whatGenre);

The cout statement doesn't display it correctly here.

cout << "The input stored in string whatGenre is: " << whatGenre;
}

The same issue with the other two functions.

void printDirector()
{
string whatDirector;
cout << "Enter a specific director " << endl;
getline(cin, whatDirector);

cout << endl << "The input stored in string whatGenre is: " << whatDirector;
}

3 Answers3

1

In addition to not using operator>> together with getline(), do not use get() with getline() either, for the same exact reasons. Just consistently use getline() to process interactive input, to avoid this kind of a mess.

When you used get() to read the menu choice, and entered "B" followed by , get() willingly consumed the "B" character, but the newline remained unread, which the subsequent getline() processed as an empty string, instead of processing the entered year.

Always, consistently, use getline() to read std::cin interactively.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
1

a possible explanation is that the cin.get() function reads only one character while you type two (the letter and enter). the first one gets stored in choice while the second one is saved in the buffer and issues an empty line to your getline() call.

try cleaning the buffer before issuing the getline command with fflush(stdin);

even worse: some operating systems (MS windows - I'm looking at you) are using two character when you click enter: \r and \n

akiva
  • 2,677
  • 3
  • 31
  • 40
0

Okay, using get() with getline() was the issue. My issue have been resolved by replacing everything with getline(), and converting the get() to store menu choice as a char array to use cin.getline(). Thanks for the help all for those who answered, @Sam Varshavchik @kiwi

  • Note that Stack Overflow isn't meant as a help desk for resolving your personal issues, but as a FAQ like resource for future research. The issue you had was asked before many times, and one of the most upvoted Q&A pairs already contains the answer. Hence I marked your question as duplicate. – πάντα ῥεῖ May 16 '16 at 03:14