limiting char input to only process single characters

I am writing a program that accepts 10 lower case letters one at a time and then returns the letter that would be first in an alphabetical series.

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
#include <iostream>
#include <cctype>
using namespace 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.
This is working for me:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <limits>

int main()
{
	const int NUM = 10 ;
	char userChar, tempChar = 'z' ;

	std::cout << "Enter " << NUM << " lowercase letters: \n" ;
	for ( int i = 0; i < NUM; ++i )
	{
		std::cin >> userChar ;
		std::cin.clear() ;
		std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' ) ;
		if ( userChar < tempChar )
		{
			tempChar = userChar ;
		}
	}
	std::cout << tempChar << std::endl ;

  return 0;
}
Last edited on
Topic archived. No new replies allowed.