I thought I did perfectly but I want the histogram to like the graph:

#include <iostream>
#include <cstdio>
#include <ctype.h>
#include <cstdlib>
using namespace std;

int main(int argc, char** argv)
{
   int i, cols ;
   cols = 50 ;
   int line = 60 ;
   int c; int max = 0 ;
   static int count [26];

   for (int i =0; i<26; i++)
		count [i] = 0 ;

   c=cin.get();
     while (!cin.eof())
   {
	     if (isalpha(c))
	     {
	         c = tolower(c);
	         count[ c- 'a'] ++;

	     }
        c=cin.get(); 
    }

    for (int i = 0; i < 26; i++)
           if (max < count [i])
           max = count [i];
           for (int i = 0; i < 26; i++)
                printf( "count [%c] : %7d/n", i + 'a', count[i]);

		   for (int i = 0; i < 26; i++)
		   {
			    printf( "%7d %c", count[i], 'a' + 1);
	            int limit = ((count[i]*cols )/ max);
 
	            for (int x = 0; x< limit; x++ )   
			        cout.put ('*');
			        cout.put ('\n');
		   
	       } //ends loop
   int LINES = 48;
   for (line=LINES; line>0;line--)
   {
        int threshold = (line*max)/LINES;
        for (int i = 0; i < 26; i++)
     {	
	   if (count[i]>threshold)
		   cout.put('#');
	   else
	   cout.put(' ');
     }
   cout.put('\n');
	
   }
}

Recommended Answers

All 4 Replies

Are you sure you want this line as your while loop control test:

while (!cin.eof())

I've never used cin.eof() but I can't get this condition to fail regardless of what I type, so I end up in an infinite loop.

Exactly!! So, What you sugggest I use? Or what is the problem with the code?

Exactly!! So, What you sugggest I use? Or what is the problem with the code?

Maybe the user types some character to declare he/she is through typing, perhaps the return key? So change that line to something like this:

const char NO_MORE_INPUT = '\n';
while (c != NO_MORE_INPUT)

That at least gets you out of the infinite input loop.

while (!cin.eof())

I've never used cin.eof() but I can't get this condition to fail regardless of what I type, so I end up in an infinite loop.

The loop breaks when you enter Ctrl+Z.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.