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;
}