• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

A small issue with a switch menu in c++

I suspect this might be the problem:
"cin >> infixString" combined with "cin.getline(buffer, MAX)" later on.

You choose option 1 at the menu and enter your expression. "cin >> infixString" gets the expression, but not the newline character, which remains in the input buffer.

Later, you use getline to get the next input (the menu choice); getline, by default, reads until it encounters a newline (which is still the first character in the input buffer), so your buffer remains empty.

You can get past this by using getline to get infixString, or placing a cin.ignore() after cin >> infixString (not very robust, though).
 
You were right!! I used this fix:

int getValidNonNegInt(char * prompt)
{
const int MAX = 80;
char buffer[MAX];
int i;
cout << prompt;
fflush(stdin); //The one small change.
cin.getline(buffer, MAX);
.......
}

Thanks for helping out!
 
Back
Top