function within switch statement skipping over all cin imputs?

Hello,

I'm trying to create a program where the menu uses a switch statement and each case has an associated function. The switch statement works correctly, but all of the functions that are called within the switch statements skip over the cin fields so the user isn't able to enter any data. The singular exception is for case c, with function testing123();

What am I doing wrong?

Thanks!

#include <iostream>
#include <fstream>
#include <ctime>

using namespace std;

ifstream inputFile;
ofstream outputFile;
const int MAX_SIZE = 100;
string responseList[MAX_SIZE];
string categoryList[MAX_SIZE];
string response;
string category;
string userQuestion;
char menuChoice;
int ct=0;
int i;
int size;
string newUserResponse;
string vacant;

void testing123();
void playMagic();
void importFile();
void addResponse();

int main() {

do {
cout << "\n";
cout << "Please choose one of the menu options below." << "\n\n";

cout << "A. Read responses from a file" << endl;
cout << "B. Play Magic Eight Ball" << endl;
cout << "C. Sort by responses" << endl;
cout << "D. Sort by category" << endl;
cout << "E. Write responses to a file" << endl;
cout << "F. Delete response" << endl;
cout << "G. Add response" << endl;
cout << "H. Exit\n" << endl;

cout << "Menu choice?" << endl;
cin >> menuChoice;

switch (menuChoice) {


case 'a':
case 'A':

importFile();

break;

case 'b':
case 'B':

playMagic();

break;

case 'c':
case 'C':
testing123();

break;

case 'd':
case 'D':
break;

case 'e':
case 'E':
break;

case 'f':
case 'F':

importFile();

break;

case 'g':
case 'G':

addResponse();

break;

case 'h':
case 'H':
cout << "Exiting program, goodbye." << endl;
exit(0);
break;

default:
cout << "Invalid choice, please try again.\n\n";

//break;
}


} while (menuChoice != 'h');

return 0;
}

void playMagic(){

cout << "Let's play Magic Eight Ball. Please enter a question: " << endl;
getline(cin,userQuestion);

cout << "The Answer to your questions:" << endl;
cout << userQuestion << endl;

int ranNUm;
ranNUm = rand() % ct;
cout << responseList[ranNUm] << endl;// << "\n" << categoryList[ranNUm];

}

void importFile(){

//ifstream inputFile;
inputFile.open("info.txt");

while (getline(inputFile,response)) {

//this doesn't seem to make any difference????
// when working, cuts of letter of category
//inputFile.ignore();


getline(inputFile,category);
//inputFile.ignore();


responseList[ct] = response;
categoryList[ct] = category;

ct++;

size = ct;

}

cout << "final ct = " << ct << endl;

for(i=0; i < ct; ++i) {
cout << "Response: " << responseList[i] << "\t\t" << "Category: " << categoryList[i] << endl;
}

inputFile.close();

}

void testing123(){

cout << "does this work???" << endl;
cin >> vacant;
cout << "did cin work???" << endl;

}

void addResponse(){

cout << size << endl;

if(size < MAX_SIZE){

cout << "this will work" << endl;

cout << "enter a new response " << endl;
getline(cin,newUserResponse);

responseList[ct] = newUserResponse;
categoryList[ct] = vacant;

ct++;

cout <<"new response " << responseList[ct] << endl;

}

else{
cout << "not enough room" << endl;
}

}
You are mixing formatted and unformatted input. A common error.


Whenever you ask a user to input something, the user will always press enter when done.

When you get an entire line (unformatted input), the Enter key is read and tossed for you:

1
2
3
4
5
  std::string s;
  {
    std::cout << "s? ";
    getline( std::cin, s );
  }


When you ask for just one thing, like an integer, the Enter key is not read and tossed for you. You must do it yourself.

1
2
3
4
5
6
  int n;
  {
    std::cout << "n? ";
    std::cin >> n;
    std::cin.ignore( std::numeric_limits <std::streamsize> ::max(), '\n' );
  }

Hope this helps.
Thank you, think I mostly understand the issue. So when replacing the getline, how would I still be able to collect the entire string. I tried this:

void playMagic(){

cout << "Let's play Magic Eight Ball. Please enter a question: " << endl;
//getline(cin,userQuestion);
cin >> userQuestion;

cout << "The Answer to your questions:" << endl;
cout << userQuestion << endl;

int ranNUm;
ranNUm = rand() % ct;
cout << responseList[ranNUm] << endl;// << "\n" << categoryList[ranNUm];

}

but I'm only collect the first word of the string, everything else gets stuck in the buffer and then the menu goes crazy for a few loops (until it get to 'h') before exiting.

Thanks for the help!
Why are you trying to replace getline()?
That the first cin that is skipped in the program? Sorry maybe I didn't understand your initial response. With just the cin >> the function works, but again, it only collects the first work of the string to the whitespace.
Ok, I think I have it now. For case B:

case 'b':
case 'B':
cin.ignore();

playMagic();

break;
Topic archived. No new replies allowed.