C++ Character Histogram

Feb 3, 2010 at 2:22am
I've written a code to make a character histogram of a sentence that is entered. However, i'm trying to make it so I can select the character to show the histogram.

Enter String Upto 20 Characters
"Very Nice Day"
Enter letter to show histogram for
e

e 2

My problem is that the program keeps repeating the alphabet for the length of the string. For example. 7 characters.. 7 repeats. I don't know how to fix my loop to correct this.

Also anywhere i try to implement the character to show histogram for code. It repeats too.

Please help

Code:



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
//Histogram For Selected Character


#include <iostream>
#include <fstream>
 
using namespace std;
 
int main()
{//start main 1s
    char string;
    int i;
    int newstar[26]={0};
    int sum[26]={0};
    int star;
	char letter;

	cout << "Enter String Upto 20 Characters\n";

    do

    {//start do 2s

start:
        {
        string=cin.get();
        string=tolower(string);
 
            if(isalpha(string))
            {//4s
                for (i=97;i<123;i++)
                {//5s
                    if (string==i)
                    {//6s
                        sum[string-97]++;  
                        newstar[i-97]=sum[i-97];
					}//6e
						
                        cout<<(char)i<<" "<<sum[i-97];
                        for (star=0; star<newstar[i-97]; star++)
                        {
                        cout<<"";
                        }
                        cout<<endl;
 
                }//5e
 
            }//4e
    }//end do 2e
    }
    while (string != EOF);
 
}//end main 1e 
Topic archived. No new replies allowed.