9 Replies - 1313 Views - Last Post: 06 April 2013 - 01:04 PM Rate Topic: -----

#1 jsheehan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 14-February 13

bar graph for characters in c++?

Posted 29 March 2013 - 07:58 AM

Hi, I am working on a project to count the number of words, sentences and question marks in a string. Part of the project is to count the amount of times that a character is included in the string, and to produce a bar graph to show this. The first part is working fine, but I am having trouble with the analysis part. The code is:

#include <iostream>
using namespace std;
int main()
    {
          char mytext[200];
          char ch = 'x';
          int i;
          int a;
          int words = 0;
          int sentences = 0;
          int questions = 0;
          int counts [256] = {0};
          int drawBar (int x);
          cout << "enter some text: ";
          gets(mytext);
          a = strlen(mytext);
          cout << "you typed: " << mytext << endl;
          cout << "length: " << a << endl;
          for (i=0;i<=a-1;i++)
          {
              ch = mytext[i];
              switch(ch)
              {
                        case ' ':
                             words++;
                             break;
                             case '.':
                                  sentences++;
                                  words++;
                                  break;
                              case '?':
                                   questions++;
                                     sentences++;
                                  words++;
               }                    
                                       }
           cout << "Words: " << words << endl;
          cout << "Sentences: " << sentences << endl;
          cout << "questions: " << questions << endl;        
         system("pause");
          return 0;
          }
        int drawBar (int x)
     {
         int i = 0;
         for (i=0;i<=x-1;i++)
         {
             cout << "*";
             }
             cout << endl;
             
             return 0;
             }  


The bar graph is not being produced. Do i need to move the for statement for it, or are my declerations wrong?

This post has been edited by JackOfAllTrades: 29 March 2013 - 08:09 AM
Reason for edit:: Removed question from code tags


Is This A Good Question/Topic? 0
  • +

Replies To: bar graph for characters in c++?

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4293
  • View blog
  • Posts: 13,459
  • Joined: 25-December 09

Re: bar graph for characters in c++?

Posted 29 March 2013 - 08:10 AM

Don't you have more than one item to graph? What does x actually signify?


Also don't do this:
for (i=0;i<=a-1;i++)

Just use the operator< instead of subtracting 1 from your variable. Less chance of forgetting to do the subtraction.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6111
  • View blog
  • Posts: 23,671
  • Joined: 23-August 08

Re: bar graph for characters in c++?

Posted 29 March 2013 - 08:10 AM

Where do you call the drawBar function? I don't see it.

Another person who needs to read my function tutorials (in my signature).
Was This Post Helpful? 0
  • +
  • -

#4 jsheehan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 14-February 13

Re: bar graph for characters in c++?

Posted 03 April 2013 - 01:29 PM

thanks for that. I've decided to try doing it another way, as follows:

#include <iostream>
using namespace std;
int main()

    {	
		  int counts[256] = {0};
          char mytext[200];
          int x;
          int i;
          char ch = 'x';
          int words = 0;
          int sentences = 0;
          int questions = 0;
          
          
          
          cout << "enter some text: ";
          gets(mytext);
          x = strlen(mytext);
          counts[x]++;
		 
          
		  cout << "you typed: " << mytext << endl;
          cout << "length: " << x << endl;
          
          for (i=65;i<=122;i++)
          
          {ch = i;
          cout << ch << " * " << counts[i] << endl;
		  }

          for (i=0;i<=x-1;i++)
          {
              ch = mytext[i];
              switch(ch)
              {
                        case ' ':
                             words++;
                             break;
                             case '.':
                                  sentences++;
                                  words++;
                                  break;
                              case '?':
                                   questions++;
                                     sentences++;
                                  words++;
                  
				  				  }
             
          }          
          cout << "Words: " << words << endl;
          cout << "Sentences: " << sentences << endl;
          cout << "questions: " << questions << endl;
		  
      while(ch != 'z')
           {
                   x = ch;
                   counts[x]++;
                   }
           
		  for (i=65;i<=122;i++)
          {ch = i;
           cout << " * " << counts[i] << endl;
		  }   
            
          system("pause");
          return 0;
		  		 }




The word, sentence and question mark segment is working fine, however on the character count and bar graph, the number of characters is shown as 0 and there is one star for every character. Do i need to move the while loop?
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3667
  • View blog
  • Posts: 11,499
  • Joined: 05-May 12

Re: bar graph for characters in c++?

Posted 03 April 2013 - 08:36 PM

Look at your lines 19 and 20. Can you explain what you are trying to do with those two lines? After giving us an explanation, can you show how those two lines of code is achieving that objective?
Was This Post Helpful? 1
  • +
  • -

#6 jsheehan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 14-February 13

Re: bar graph for characters in c++?

Posted 05 April 2013 - 01:24 PM

View PostSkydiver, on 03 April 2013 - 08:36 PM, said:

Look at your lines 19 and 20. Can you explain what you are trying to do with those two lines? After giving us an explanation, can you show how those two lines of code is achieving that objective?


Another part of the project is to show the overall length of the string. Lines 19 & 20 are to obtain the text in the string, check the length and to return this to the screen. It's working fine, as shown here.

Enter some text: Hello?.
You entered: Hello?.
Length: 7

I should have mentioned it earlier, aplogies
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3667
  • View blog
  • Posts: 11,499
  • Joined: 05-May 12

Re: bar graph for characters in c++?

Posted 05 April 2013 - 04:27 PM

That explains line 19, but why are you doing line 20?
Was This Post Helpful? 0
  • +
  • -

#8 jsheehan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 14-February 13

Re: bar graph for characters in c++?

Posted 06 April 2013 - 03:56 AM

View PostSkydiver, on 05 April 2013 - 04:27 PM, said:

That explains line 19, but why are you doing line 20?


I tought i needed to have this line in order to count the string length. I'll take it out and try it again.
Was This Post Helpful? 0
  • +
  • -

#9 jsheehan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 14-February 13

Re: bar graph for characters in c++?

Posted 06 April 2013 - 09:59 AM

I took out line 20, and the length of the string did not show. Had to put it back in,as it is mandatory for the project. Do i need to declare a variable for the star output?
Was This Post Helpful? 0
  • +
  • -

#10 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 841
  • View blog
  • Posts: 2,472
  • Joined: 20-March 10

Re: bar graph for characters in c++?

Posted 06 April 2013 - 01:04 PM

Hmm,

your first attempt was better than your second attempt.

first of all you have to get the first part of your program working accurately

in both your programs if I type hello
I get 0 words, not good.

you miss headers most notably cstdio and cstring.

Why are you declaring mytext as char[200] why not use string.

case ' ' is fine but what happens if you do this : I am a space before full stop .
you get an extra word before the full stop so you need to check for spaces at mytext[i-1]

same with case '?'

dont use system("pause"0);

try cin.ignore();
cin.get();
to hold the window open.

as for the bar drawing this should be done in a function like you tried in the first place

convert the whole sentence to uppercase I doubt the teacher or professor is asking for a bar graph
of both upper and lower case letters in a console.

then just count the letters and add 1 each time to a container array then print a star in a loop for the occurences of that letter.

fixed the first part for you

wishing you luck for bar drawing.

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

int main()
{
    string mytext;
    char ch = 'x';
    int i = 0;
    int a = 0;
    int words = 0;
    int sentences = 0;
    int questions = 0;


    cout << "enter some text: ";
    getline(cin,mytext);
    a = mytext.length();
    if (a !=0)
    {
        words ++;
        cout << "you typed: " << mytext << endl;
        cout << "length: " << a << endl;
        for (i=0; i<a; i++)
        {
            ch = mytext[i];
            switch(ch)
            {
            case ' ':
                words++;
                break;
            case '.':
                sentences++;
                if(mytext[i-1]== ' ')
                {words= words-1;}
                break;
            case '?':
                questions++;
                sentences++;
                if(mytext[i-1]== ' ')
                {words= words-1;}
                break;
            }
        }
    }
    cout << "Words: " << words << endl;
    cout << "Sentences: " << sentences << endl;
    cout << "questions: " << questions << endl;
    cin.ignore();
    cin.get();

    return 0;
}





Snoopy.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1