#include <iostream>
#include <cctype>
usingnamespace std;
//Variable Declarations
char alpha; //Currently most alphabetical
char letter; //Holds character to be checked by the alphabetical function
int counter; //Counter used in for loop
//Creates alphabetical function for lowercase characters
char alphabetical (char x, char y)
{
char mostalpha; //Declares variable largest.
if ( x <= y ) //Compares ASCII value of x and y determining which is first in alphabet.
mostalpha = x; //X is first in alphabet.
else
mostalpha = y; //If X is not first then Y must be first.
return mostalpha; //Returns result.
}
int main()
{ //Prompts user to enter the 10 letters
cout << "This programs determines the first letter in an alphabetical series of 10 letters." <<endl;
cout << "Please enter the lower case letters one at a time." <<endl;
cout << "Please enter 10 lowercase letters: " <<endl;
cin >> alpha; //Reads first letter, which is the most alphabetical by default
while (islower(alpha)==0) //Loops while alpha is not a lower case character
{
cout << "Invalid input. Enter a new lower case letter:" <<flush;
cin.clear(); //Clears error on istream
cin.ignore(99999, '\n');//Ignores 99999 characters until new line is received
cin >> alpha; //Reads in first letter to alpha
cout << "Continue to enter the lower case letters" <<endl;
}
if (islower(alpha)!=0)
for ( int counter = 1; counter <= 9; ++counter ) //Counter repeats 9 times.
//Because first letter is already
//entered above.
{
cin >> letter; //Reads second letter entered.
while (islower(letter)==0) //Loops while letter variable is not a letter.
{
cout << "Invalid input. Enter a new lower case letter:";
cin.clear(); //Clears error on istream
cin.ignore(99999, '\n');//Ignores 99999 characters until new line is received
cin >> letter; //Reads in new letter to letter variable
cout << "Continue to enter the lower case letters" <<endl;
}
alpha = alphabetical (letter, alpha); //Uses alphabetical function and sets result as 'smallest' variable
}
cout << "The first letter in the alphabetical series is: " << alpha <<endl; //Final declaration statement.
return 0;
Everything works as expected except if the user enters two or more lower case letters. Then the program counts reads both letters into the series. I want to program to only accept one character at a time.