Program Enters Infinite Loop if User Enters a String With Spaces

closed account (N1q4LyTq)
For my assignment that I created the other day, I had received a 100%. We had to create a series of menu options and each case did different things. The first case allows a user to input a password and the code encrypts it, the second makes an input string lowercase, the third makes an input string uppercase, and the fourth allows the user to enter a new string. Although it works when a user enters a string without spaces, cases 1-4 will not allow any spaces in the string without creating an infinite loop. I had previously tried the getline() function and although it works for the first string that the user enters, it does not work for any of the cases within the switch statement. I tried the getline() function in different locations, and my professor did not have the opportunity to answer my question. I was wondering if anyone of you had an idea on how to figure this out. Thank you :)

#include <iostream>
#include <string>
using namespace std;

int main()
{
int menuOption;
string userInput;
string password;
string lowerString;
string upperString;
string newString;
cout << "Please enter a string: ";
getline (cin, userInput, '\n');
do{
cout << "\n\t1. Encrypt\n";
cout << "\t2. Convert to all lowercase\n";
cout << "\t3. Convert to all uppercase\n";
cout << "\t4. Enter new string\n";
cout << "\t5. Quit Program\n";
cout << "\t6. Option: ";
cin >> menuOption;
switch (menuOption)
{
case 1: cout << "\nPlease enter the password to be encrypted: ";
cin >> password;
for (int p = 0; p < password.length(); p++)
{
password.at(p) += 2;
}
cout << "\nEncrypted Password: " << password << endl;
break;

case 2: cout << "Please enter a string to convert to lowercase: ";
cin >> lowerString;
//getline (cin, lowerString);
for (int s1 = 0; s1 < lowerString.length(); s1++)
{
lowerString[s1] = tolower(lowerString[s1]);
}
cout << "Lowercase: " << lowerString << endl;
break;

case 3: cout << "\nPlease enter a string to convert to uppercase: ";
cin >> upperString;
//getline (cin, upperString);
for (int s2 = 0; s2 < upperString.length(); s2++)
{
upperString[s2] = toupper(upperString[s2] );
}
cout << "\nUppercase: " << upperString << endl;
break;

case 4: cout << "\nPlease enter a new string: ";
cin >> newString;
//getline (cin, newString);
break;

case 5: cout << "\nThe program will now terminate! Have a great day!\n";
break;
default: cout << "\nYour entry is invalid.\n";
}
} while (menuOption != 5);
return 0;
}

The formatting looks a little weird because I copy and pasted, but know on my code all the cases are on the same line :), and indented properly.
Last edited on
cin >> password;
use getline() for std::string throughout, std::cin breaks on whitespace
closed account (N1q4LyTq)
I tried utilizing the getline() function, but then it would not run properly.
One problem I see is that you use cin in your switch case which will leave a dangling '\n' in the input buffer so the second time it will skip cin >> menuOption; thus creating an endless loop. You should use getline for all you string inputs.
what do you mean by : 'it would not run properly'? and are you sure it's the getline() that's causing it?
We're going to need a bit more information than "would not run properly".
Topic archived. No new replies allowed.