Counting repeated alphabets.

Hi, Good day.
I had just learned C++ programming for 3 months.
I am making a program of a simple Jackpot. It's a school project actually.

I manage to get random alphabet output and I am to find the number of time the alphabets are repeated after they are generated.

I am facing a problem where the count for the repeated alphabet tends to continue to increase even after they are scanned and I can't manage to solve it.

Please guide me.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  RN_no=0;
			
    for (i=0; i<7; i++)
	{   		
	 for (k=i+1;k<7;k++)
	{ if (ran[i]==ran[i-1])   {break;} 
          //this is to make sure that the alphabet of 'i' is not equals to the        
            one before so that it won't double count

          else	{  if (ran[i]==ran[k])
	                 {			
		  	  RN_no=RN_no+1; //this is where the count is increase   
                                           if  there is a similar alphabet.
			 }
				 
		} 
	 }
					
     } 
	printf ("\nRepeated Alphabet = %d",RN_no);
 


My problem is that the alphabet will continue to be counted even after if it is counted, eg:

A A B B A G A

*supposingly there are only 4 repeatings, A 3 times and B 1 time
my code shows 5 times as the 'A' after the 'B' is still used to compared with
the last 'A'

Hope that someone can help me out with this issue please...
Thank you.
http://ideone.com/PoWSpX
I wrote some code that can count the number of repeat characters in a character array.
Your code is invalid. In this statement

{ if (ran[i]==ran[i-1]) {break;}

when i is equal to 0 you are trying to access the element with index i -1 that is equal to -1.


@kenvinkjt2000
Thank you very much for your time. Much appreciate it. Will take for reference. :)

@vlad from moscow
I had been thinking about that too, but the weird thing is that it works as in
ran[1]==ran[0] break;

but for ran[0], i think it don't quite make it to -1 don't know why.
But still thanks for your time :)
I manage to do it by adding a couple more of IFs statement.
Thank you everyone.
Topic archived. No new replies allowed.