infinate loop problem

I have an infinite loop. I have the program reading the input as an ascii value and i need it to keep track of upper and lower cases and also numeric characters.

int main(){

char ascii;

int numbercount, uppercasecount, lowercasecount;

numbercount = 0, uppercasecount = 0, lowercasecount = 0;

cout<< "enter # to quit!"<< endl;

cout << "Give character: ";

cin >> ascii;

do

cout << "Its ascii value is: " << (int) ascii << endl;

while (ascii!=35);

if (ascii>=65 && ascii<=90)
uppercasecount++;
else if (ascii>=48 && ascii<=57)
numbercount++;
else if (ascii>=97 && ascii<=122)
lowercasecount++;
cout << "number of lowercases: "<<lowercasecount;
Do you need to convert to an int for this asignment?

If not you can simply do something like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

do
    cout << "its ascii value is: " << (int) ascii << endl;
while( ascii != '#'  );

if( ascii >= 'A' && ascii <= 'Z' )
    ++uppercasecount;
if( ascii >= 'a' && ascii <= 'z' )
    ++lowercasecount;
if( ascii >= '0' && ascii <= '9' )
    ++numbercount;

cout << "Number of lowercases: " << lowercasecount << endl;
cout << "Number of uppercases: " << uppercasecount << endl;
cout << "Number of numbers: " << numbercount << endl;


wait a sec after looking at your code why do you have a do/while loop there??What is the purpose of infinitely outputting the ascii value.

Maybe you meant to put the do on line 5 then a open curly brace then on the last line put a closing curly brace and your while statement
Last edited on
Change this:
1
2
3
4
5
6
7
cin >> ascii;

do

cout << "Its ascii value is: " << (int) ascii << endl;

while (ascii!=35);


to this:
1
2
3
4
5
do
{
    cin >> ascii;
    cout << "Its ascii value is: " << (int) ascii << endl;
} while (ascii!=35);
ok its not looping anymore but i need it repeatedly ask the user to enter characters and keep count until they enter #.
Like I said earlier

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

int main(){

char ascii;

int numbercount, uppercasecount, lowercasecount;

numbercount = 0, uppercasecount = 0, lowercasecount = 0; 
do
{
cout<< "enter # to quit!"<< endl;

cout << "Give character: ";

cin >> ascii;

do

cout << "Its ascii value is: " << (int) ascii << endl;

while (ascii!=35);

if (ascii>=65 && ascii<=90)
uppercasecount++;
else if (ascii>=48 && ascii<=57)
numbercount++;
else if (ascii>=97 && ascii<=122)
lowercasecount++;
} while( ascii != 35 );
cout << "number of lowercases: "<<lowercasecount;


*edit on a side note you might want to use a while statement instead of a do/while actually because do whiles check at the end. Otherwise just throw an if statement in and end it if they put '#';
Last edited on
1
2
3
4
5
6
7
8
9
10
11
    while ((std::cout << "Please enter a character (# to quit): ") && //while we can print this prompt
           (std::cin >> input) && //AND we make a successful read of user input
           (input != '#'))  //AND input isn't '#'
    {
        if( input >= 'A' && input <= 'Z' )
            ++uppercasecount;
        if( input >= 'a' && input <= 'z' )
            ++lowercasecount;
        if( input >= '0' && input <= '9' )
            ++numbercount;
    }


assuming, of course, that you declare input as a char, and the count variables as integers before this loop.
Last edited on
Thank guys!!
Topic archived. No new replies allowed.