Alright, I've run into another little problem. When I run the program it prints "Enter B for book, R for recording", and for example if I type 'B' and press enter it prints "Enter book title", new line, and "Enter book author" without letting you input anything for the title. So it seems that it is taking the newline character after 'B' as the book title? I tried cin.ignore('\n'); in multiple places but it didn't fix anything.
Code:
cout << "Enter B for book, R for recording: ";
cin >> type;
switch(type){
case 'B' :
{
cout << "Enter book title: ";
csis << "Enter book title: ";
cin.getline(title, 99);
csis << title;
cout << endl << "Enter book author: ";
csis << endl << "Enter book author: ";
cin.getline(author, 99);
csis << author;
cout << endl << "Enter call number: ";
csis << endl << "Enter call number: ";
cin >> callNumber;
csis << callNumber;
Book* book = new Book(title,callNumber,author);
return book;
break;
}
case 'R' :
{
cout << "Enter recording title: ";
csis << "Enter recording title: ";
cin.getline(title, 99);
csis << title;
cout << endl << "Enter performer: ";
csis << endl << "Enter performer: ";
cin.getline(performer, 99);
csis << performer;
cout << endl << "Enter format: (M)P3, (W)AV, (A)IFF: ";
csis << endl << "Enter format: (M)P3, (W)AV, (A)IFF: ";
cin >> format;
csis << format;
cout << endl << "Enter call number: ";
csis << endl << "Enter call number: ";
cin >> callNumber;
csis << callNumber;
Recording* record = new Recording(title,callNumber,performer,format);
return record;
break;
}
default:
cout << "You entered an invalid type!" << endl;
return NULL;
}
Am I still doing something wrong here?
EDIT: putting cin.ignore('\n') after
Code:
csis << "Enter book title: ";
cin.ignore('\n');
works and fixes the problem for the book, but when I use it for the record like this:
Code:
csis << "Enter recording title: ";
cin.ignore('\n');
it lets me type in a title, then I press enter and it goes to a new blank line and I have to type something else and press enter again for the next line to print.
EDIT 2: I figure out for both of these, if I type in a one-word title the problem persists, however if I type in two or more words it works fine.