0

I am trying to write an infix calculator and I want to start the program off asking the user if he/she needs help or not. I have written the code that will offer instructions if the user inputs 'y' or 'n', however, in both cases, the program ends without allowing the user to enter an infix expression and running the rest of the program. It seems as though the program is breaking in main right after cout << "Expression?"; It does not give the option for the user to input anything after that.

2
  • You're never calling isValidResponse, you're just testing whether its address is not null. Commented Mar 20, 2014 at 2:10
  • Ahhh you're right! in provideHelpIfNecessary(void) I changed it to: while(true) { if (isValidResponse(help) && (isYesResponse(help)) but it is still breaking out of the program early. Commented Mar 20, 2014 at 2:14

1 Answer 1

1

When you use cin >> help, you're only reading one character, the y or n. The newline after that is left in the input buffer.

Then when the main function uses getline, it reads up to the next newline, which is the one that was left in the buffer by provideHelpIfNecessary. So it just reads a zero-length line, and that causes the while loop to break.

Use getline in provideHelpIfNecessary instad of reading just one character.

Sign up to request clarification or add additional context in comments.

2 Comments

would it be as simple as saying getline(cin, help) in provideHelpIfNecessary? Because when I do that I receive a nasty error stating: error: no matching function for call to 'getline' getline(cin, help);
You need to change help from a char to a std::string.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.