Hi, my code prints out the number of times a letter is typed. I can get it to print out the correct info, but when it deals with a white space, it will print out the list for every white space. how can I deal with this?

#include <iostream>
#include <fstream>

using namespace std;

int main()
{//start main 1s
    char c;
    int i;
    int newstar[26]={0};
    int sum[26]={0};
    
    do
    {//start do 2s
start:
        c=cin.get();
        {//3s
            if(isalpha(c))
            c=tolower(c);
            {//4s
                for (i=97;i<123;i++)
                {//5s
                    if (c==(char)i)
                    {//6s
                        sum[c-97]++;
                        goto start;
                    }//6e
                }//5e
            }//4e
            for(int x=97;x<123;x++)
            {//7s
                newstar[x-97]=sum[x-97]*80/389;
                cout<<(char)x<<" "<<sum[x-97];
                for (int star=0; star<newstar[x-97]; star++)
                {//8s
                    cout<<"*";
                }//8e
                cout<<endl;
            }//7e
        }//3e
    }//end do 2e
    while (c != EOF);
}//end main 1e

Recommended Answers

All 7 Replies

have you try

if(!isspace)

between line 18 & 19?

have you try changing line 19 for line 20?

I still get the code repeating for every white space.

sorry I edit my comment while you reply
and sorry for saying that i was i bit sleepy

you were right to point out that for the isalpha function to work right, you need the next line to be what you want it to do. I am playing around with my code. I combined the for loops into each other. The problem is it prints out step by step for every char. any thoughts?

thank you

#include <iostream>
#include <fstream>

using namespace std;

int main()
{//start main 1s
    char c;
    int i;
    int newstar[26]={0};
    int sum[26]={0};
    int star;
    do
    {//start do 2s
start:
        c=cin.get();
        {
        c=tolower(c);
       
            if(isalpha(c))
            {//4s
                //cout<<"c is alpha"<<endl;
                for (i=97;i<100;i++)
                {//5s
                    if (c==i)
                    {//6s
                        sum[c-97]++;  
                        newstar[i-97]=sum[i-97]*80/389;
					}//6e
                        cout<<(char)i<<" "<<sum[i-97];
                        for (star=0; star<newstar[i-97]; star++)
                        {
                        cout<<"*";
                        }
                        cout<<endl;
                    
                }//5e
               
            }//4e
    }//end do 2e
    }
    while (c != EOF);

}//end main 1e
#include <iostream>
#include <fstream> 
using namespace std; 
int main()
{
    char c;    
    int i;    
    int newstar[26]={0};    
    int sum[26]={0};     
    do    
    {
          c=cin.get();
          if(if(!isspace))
          {
                      if(isalpha(c))            
                      c=tolower(c);            
                      {
                                               for (i=97;i<123;i++)
                                               {
                                                   if (c==(char)i)
                                                   {
                                                                  sum[c-97]++;
                                                                  goto start;
                                                   }              
                                               }
                      }
                      for(int x=97;x<123;x++)            
                      {
                              newstar[x-97]=sum[x-97]*80/389;                
                              cout<<(char)x<<" "<<sum[x-97];                
                              for (int star=0; star<newstar[x-97]; star++)                
                              {
                                  cout<<"*";                
                              }//8e                
                              cout<<endl;            
                              }//7e        
                      }//3e    
          }//end do 2e    
          while (c != EOF);
    }

Humm...I coudnt compile gastoncis code. But try this T-Doog.

#include <iostream>

#include <fstream>

 

using namespace std;

 

int main()

	 {//start main 1s

		char c;

		int i;

		int newstar[26]={0};

		int sum[26]={0};

 

			

				do{//start do 2s
					start:
	
					c=cin.get();
					if(c == ' ')
					  {
						continue;
					  }
					 if(isalpha(c))

							c=tolower(c);

							 

								for (i=97;i<123;i++)

									{//5s

										if (c==(char)i)

										  {//6s

										    sum[c-97]++;

										    goto start;

										  }//6e

								        }//5e

					   		

						 for(int x=97;x<123;x++)
      				     		           {//7s

								newstar[x-97]=sum[x-97]*80/389;

							        cout<<(char)x<<" "<<sum[x-97];

								  for (int star=0; star<newstar[x-97]; star++)

								       {//8s

										cout<<"*";

								       }//8e

										cout<<endl;

							    }//7e
						}while(c != EOF);

					
				return 0;

	   }

thank you works great. I didn't know about "continue".

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.