Ongoing Loop
May 14, 2015 at 4:27am UTC
On line 39, when I try to test the while loop by entering in only 1 character, the loop goes on forever, and spams all of my black box.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
#include <iostream>
#include <string>
using namespace std;
//prototype
void stateInstructions();
void getString(string&);
void convertToUpper(string);
void convertToLower(string);
void countingVowels(string);
void reverseString(string);
int main()
{
string userInput;
stateInstructions();
getString(userInput);
convertToUpper(userInput);
convertToLower(userInput);
countingVowels(userInput);
reverseString(userInput);
}
void stateInstructions()
{
string instructions;
instructions = "I will be manipulating your text input today.\n" ;
cout << instructions;
}
void getString(string &userInput)
{
string instructions;
instructions = "Please enter anything with at least 4 characters.\n" ;
cout << instructions;
//get userinput
getline(cin, userInput);
while (userInput.length() <= 4)
cout << "Please enter more than 4 characters." << endl;
cout << "You have entered this as your input: " << userInput << endl;
}
void convertToUpper(string userInput)
{
string response;
response = "This is your input in all capital letters: " ;
// string convertToUpper(userInput);
for (int i = 0; userInput[i]; i++)
userInput[i] = toupper(userInput[i]);
cout << response << userInput << endl;
}
void convertToLower(string userInput)
{
string response;
response = "This is your input in all lower case letters: " ;
for (int i = 0; userInput[i]; i++)
userInput[i] = tolower(userInput[i]);
cout << response << userInput << endl;
}
void countingVowels(string userInput)
{
string response;
// Counting Vowels
int numOfVowels = 0;
response = "The number of vowels in your input line: " ;
for (int i = 0; i < userInput.length(); i++)
{
if (userInput[i] == 'a' || userInput[i] == 'e' || userInput[i] == 'i' || userInput[i] == 'o'
|| userInput[i] == 'u' || userInput[i] == 'A' || userInput[i] == 'E' || userInput[i] == 'I'
|| userInput[i] == 'O' || userInput[i] == 'U' )
numOfVowels++;
}
cout << response << numOfVowels << endl;
//Counting Spaces
int numOfSpaces = 0;
for (int i = 0; i < userInput.length(); i++)
{
if (userInput[i] == ' ' )
numOfSpaces++;
}
// Counting Numbers
int numOfNumbers = 0;
for (int i = 0; i < userInput.length(); i++)
{
if (userInput[i] == '1' || userInput[i] == '2' || userInput[i] == '3' || userInput[i] == '4'
|| userInput[i] == '5' || userInput[i] == '6' || userInput[i] == '7' || userInput[i] == '8'
|| userInput[i] == '9' || userInput[i] == '0' )
numOfNumbers++;
}
// Counting Punctuation
int numOfPunc = 0;
for (int i = 0; i < userInput.length(); i++)
{
if (userInput[i] == ',' || userInput[i] == '?' || userInput[i] == '!' || userInput[i] == '/'
|| userInput[i] == '@' || userInput[i] == '#' || userInput[i] == '$' || userInput[i] == '%'
|| userInput[i] == '^' || userInput[i] == '&' || userInput[i] == '*' || userInput[i] == '('
|| userInput[i] == ')' || userInput[i] == '-' || userInput[i] == '=' || userInput[i] == '_'
|| userInput[i] == '+' || userInput[i] == '`' || userInput[i] == '~' || userInput[i] == '<'
|| userInput[i] == '>' || userInput[i] == '.' || userInput[i] == '"' || userInput[i] == '"' )
numOfPunc++;
}
// Counting Consonants
response = "There are these many consonants in your input line: " ;
int size = userInput.length() - numOfSpaces - numOfNumbers - numOfPunc;
int numOfCons = size - numOfVowels;
cout << response << numOfCons << endl;
}
void reverseString(string userInput)
{
string response;
// Text Manipulation - Reversing
response = "This would be your input line all reversed: " ;
string reverse(userInput);
userInput = string(userInput.rbegin(), userInput.rend());
cout << response << userInput << endl;
}
Last edited on May 14, 2015 at 5:13am UTC
May 14, 2015 at 5:16am UTC
You never try to get the user input again from within the while loop. You check if the user input is less than four, tell them that it is less than four, then check if it is less than four.
May 14, 2015 at 5:38am UTC
1 2 3 4 5 6
do
{
cout << "Please enter more than 4 characters."
"\nPlease try again." << endl;
getline(cin, userInput);
} while (userInput.length() <= 4);
I tried that, but now I can't enter more than like 7 characters
Last edited on May 14, 2015 at 5:39am UTC
May 14, 2015 at 5:40am UTC
If you enter only one character then
userInput.length()
will be
1
and since
1
is less than
4
, this statement
cout << "Please enter more than 4 characters." << endl;
will be executed without any limits (Infinite Loop)
As Ispil mentioned, you can do something like this:
1 2 3 4 5 6 7
getline(cin, userInput);
while (userInput.length() <= 4)
{
cout << "Please enter more than 4 characters." << endl;
getline(cin, userInput);
}
OR
1 2 3 4 5 6 7 8
do
{
getline(cin, userInput);
if (userInput.length() <= 4)
cout << "WARNING: Please enter more than 4 characters." << endl;
} while (userInput.length() <= 4);
May 14, 2015 at 5:48am UTC
I used the first one and it worked, but not the second one. Weird. Thanks everyone! :)
Topic archived. No new replies allowed.