counting input chars -- help request

Hi,

below program count input letters and numbers, but few things are not going as i planned:

1. when input like: 33 - it display twice comment instead last count
1
2
3
4
5
6
7
33
Chars: 
Letters: 0
Numbers: 5
Chars: 
Letters: 0
Numbers: 6

2. i would like to stop it when last char is repeated 3 times.

can you help please?

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
  cout<<"Input letters or numbers.\n";
	char zn;

	int count_l=0, count_c=0;

	while(cin>>zn)
	{
		//cin>>znak;
		if((zn<=122 && zn>=97) || (zn<=90 && zn>=65))
		{
			++count_l;

			if(count_l=='3')
			{
				cout<<"Chars: \nLetters: "<<count_l<<endl;
			}
		}
		if(zn<=57 && zn>=48)
		{
			++count_c;
		}
		cout<<"Chars: \nLetters: "<<count_l<<"\nNumbers: "<<count_c<<endl;
	}



return 0;
}
when I compile your code it works, but if you what results to be shown at the end, put them out of the while
1
2
3
4
5
6
while() 
  {
       // counting
  }
  
   // results 



rich1 - thanks for reply,

unfortunately, when I put comment from line 22

cout<<"Chars: \nLetters: "<<count_l<<"\nNumbers: "<<count_c<<endl;

further to line 24 does not print anything and you can input characters as much as you want without any prompt.

unless I did not understand where exactly to put it.
I think the error is on line 13 you are comparing count_l (int) to a char ('3') which is 51 is that what you meant to do? if( count_l == 51) because that seems very strange to me.

Honestly I think you should remove lines 13-16

Also you can put in your if statements letters like 'a' - 'z' and 'A' - 'Z' instead of 122 - 97 and 90-65 same with the numbers.


After reading your first post again it looks like you are trying to stop it from displaying the letters/numbers twice for each digit. For this just simply move the output statement on line 22 to line 24 or anything after the while statement really

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  cout<<"Input letters or numbers.\n";
	char zn;

	int count_l=0, count_c=0;

	while(cin>>zn)
	{
		if((zn<=122 && zn>=97) || (zn<=90 && zn>=65))
		{
			++count_l;
		}
		if(zn<=57 && zn>=48)
		{
			++count_c;
		}
	}
		cout<<"Chars: \nLetters: "<<count_l<<"\nNumbers: "<<count_c<<endl;
return 0;
}
giblit - thank you for reply

Yes, you were right about lines 13-16 this part of code does not working at all, i do not want to compare with 51 (ASCI code for 3) but if characted was input 3 times.
1
2
3
4
5
if(count_l=='3')
			{
				cout<<"Chars: \nLetters: "<<count_l<<endl;
			}


it will stops and display that character was inputted 3 times, if it is wrong how can i make it working?

With moving comment as you sugested - does not display at all now.

1
2
3
		}
	}
		cout<<"Chars: \nLetters: "<<count_l<<"\nNumbers: "<<count_c<<endl;


What am I missing, how to make working for this part:


Sorry I didn't realize you wanted it to stop when the last character was inputted 3 times in a row for this simply put a counter for the last character.


Basically like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
char input , last;
int letters = 0 , numbers = 0 , characters = 0 , repeats = 0;
while( cin >> input && repeats != 3 )
{
    ++characters;
    if( letter ) ++letters; 
    if( number )  ++numbers;
    if( characters == 1 ) //First time so you have to set initial value for last char
        last = input;
    if( last == input )
        ++repeats;
    else
        repeats = 1;
}


http://ideone.com/1ie26T
Topic archived. No new replies allowed.