Ask coding questions

← Back to all posts
inputting only lowercase letters; otherwise error message and loop again
tuhmaytow (0)

Hello, I am starting out with c++ and I am trying to do something where the user can only input lowercase letters and they would get prompted with a congratulatory message saying that their input is valid. But if they input anything other than lowercase letters, and or symbols, they will get an error message and will be prompted to input another character/letter. All this using a while loop. thank you! Here is my code:

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

int main()
{
char letter;
string userInput;

cout << "Enter a character:";
cin >> letter;

while(letter >= 'A' && letter <= 'Z') {
	cout << "Not a valid entry. \n";
	cout << "The character " << letter << " is invalid. " << "Try again. \n";
	cout << "Enter another character: \n";
	cin >> letter;

}
cout << "Congratulations valid entry! \n";
cout << "The character you entered is: " << letter << endl;
return 0;

}

Comments
hotnewtop
ConnorONeal (0)

I might be a bit late but if you change the captial A and Z to lowercase a and z it will take upercase letters.

while(letter >= 'a' && letter <= 'z')

ash15khng (712)

So do you need help, or are you sharing this with us? If asking for help, please make your question more clear. If sharing, you can go to the Share section of repl talk.