Bar Charts in C++?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 22

Thread: Bar Charts in C++?

  1. #1
    Join Date
    Oct 2009
    Posts
    38

    Bar Charts in C++?

    Can somebody help me with making a bar chart in C++ ?

    I have to make a bar chart where each * represents 100. It should display like this after the user has inputted the cin for store sales.

    Store 1: *******
    Store 2: *******
    Store 3: *******
    Store 4: *******
    Store 5: *******
    Last edited by Sabensohn70; November 22nd, 2009 at 06:34 PM.

  2. #2
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Bar Charts in C++?

    Use a for-loop.
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  3. #3
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,317

    Re: Bar Charts in C++?

    Although you could directly use a loop, it may be simpler to just create (and immediately print) a string of asterisks with the desired length, since std::string has a suitable constructor.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  4. #4
    Join Date
    Oct 2009
    Posts
    38

    Re: Bar Charts in C++?

    how would i use the for loop for this?

  5. #5
    Join Date
    Jun 2008
    Posts
    592

    Re: Bar Charts in C++?

    consider
    Code:
     
    for( unsigned int I = 0; I < 10; ++I )
    {
        cout << "*";
    }
    http://msdn.microsoft.com/en-us/library/b80153d8.aspx

    this is very basic and could be easily found by google. please do some research
    0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
    0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000
    0000 0000 0000 0000

  6. #6
    Join Date
    Oct 2009
    Posts
    38

    Re: Bar Charts in C++?

    yeah so how would i make it so the users input each represent 100 in the chart?

  7. #7
    Join Date
    Jun 2008
    Posts
    592

    Re: Bar Charts in C++?

    i suppose you start with some math. maybe int points = total / 100; ?
    0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
    0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000
    0000 0000 0000 0000

  8. #8
    Join Date
    Oct 2009
    Posts
    38

    Re: Bar Charts in C++?

    can you please just help me out? I've never learned this material before. Im suppose to make a bar chart where store1=1000, store2= 1200, store3=1800, store4= 800, store5 =1900. and each asterisk will represent 100.

  9. #9
    Join Date
    Oct 2009
    Posts
    38

    Re: Bar Charts in C++?

    so far this is what i got..

    Code:
    #include<iomanip>
    using namespace std;
    
    int main()
    {
            int store1, store2, store3, store4, store5;
    
            cout << "Enter today's sales for store 1.\n";
            cout << "Enter a value between 0 and 4000:  ";
            cin >> store1;
            while ( store1 &#37; 100 != 0 || store1 < 0 || store1 > 4000 )
            {
                cout << "Invalid sales amount: " << store1 << endl;
                cout << "Sales must be multiple of 100 in range 0-4000, inclusive.\n";
                cout << "Enter today's sales for store 1.";
                cin >> store1;
            }
            cout << "Enter the sales for store 2.";
            cin >> store2;
            while ( store2 % 100 != 0 || store2 < 0 || store2 > 4000 )
            {
                cout << "Invalid sales amount: " << store2 << endl;
                cout << "Sales must be multiple of 100 in range 0-4000, inclusive.\n";
                cout << "Enter today's sales for store 2.";
                cin >> store2;
            }
            cout << "Enter the sales for store 3.";
            cin >> store3;
            while ( store3 % 100 != 0 || store3 < 0 || store3 > 4000 )
            {
                cout << "Invalid sales amount: " << store3 << endl;
                cout << "Sales must be multiple of 100 in range 0-4000, inclusive.\n";
                cout << "Enter today's sales for store 3.";
                cin >> store3;
            }
            cout << "Enter the sales for store 4.";
            cin >> store4;
            while ( store4 % 100 != 0 || store4 < 0 || store4 > 4000 )
            {
                cout << "Invalid sales amount: " << store4 << endl;
                cout << "Sales must be multiple of 100 in range 0-4000, inclusive.\n";
                cout << "Enter today's sales for store 4.";
                cin >> store4;
            }
            cout << "Enter the sales for store 5.";
            cin >> store5;
            while ( store5 % 100 != 0 || store5 < 0 || store5 > 4000 )
            {
                cout << "Invalid sales amount: " << store5 << endl;
                cout << "Sales must be multiple of 100 in range 0-4000, inclusive.\n";
                cout << "Enter today's sales for store 5.";
                cin >> store5;
            }
    i just dont know how to do the bar chart part..

  10. #10
    Join Date
    Jun 2008
    Posts
    592

    Re: Bar Charts in C++?

    can you please just help me out?
    i have. if i write too much more, i would be handing your assignment in too.

    Code:
     
    int points = store4 / 100;
    for( unsigned int I = 0; I < 10; ++I )
    {
        cout << "*";
    }
    I highlighted the 10... for a reason

    so far this is what i got..
    that code is redundant
    0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
    0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000
    0000 0000 0000 0000

  11. #11
    Join Date
    Oct 2009
    Posts
    38

    Re: Bar Charts in C++?

    im thinking for ( store1 = 0; store1 / 100; ++store1 ) ? this doesnt work

  12. #12
    Join Date
    Jun 2008
    Posts
    592

    Re: Bar Charts in C++?

    for ( store1 = 0; store1 / 100; ++store1 ) ?
    let's think here for a moment. did store1 hold the value for the store 1? if so, you effectively destroyed its value. store1 / 100 isn't right. the first test would equal false since 0 / 100 is 0. ++store1... well would count up if it ran, but when would it stop?

    loop up how to use the for statement.....................
    0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
    0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000
    0000 0000 0000 0000

  13. #13
    Join Date
    Oct 2009
    Posts
    38

    Re: Bar Charts in C++?

    ..dude i am reading my textbook and it doesnt say anything about making bar charts with the for loop. if you are not going to try to help please do not answer at all okay? believe it or not there are beginners in this world

  14. #14
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,317

    Re: Bar Charts in C++?

    Quote Originally Posted by Sabensohn70
    im thinking for ( store1 = 0; store1 / 100; ++store1 ) ? this doesnt work
    Yes, but that is because you used store1 as the loop counter. You should use another variable. Also, note that the truncation means that if the number is less than 100, no asterisk will be displayed. Is this intended?

    EDIT:
    Quote Originally Posted by Sabensohn70
    ..dude i am reading my textbook and it doesnt say anything about making bar charts with the for loop. if you are not going to try to help please do not answer at all okay? believe it or not there are beginners in this world
    Read Joeman's explanation of why your loop did not work. If you refuse to listen to anything other than a direct solution, you will not gain in understanding, and thus remain a beginner forever.
    Last edited by laserlight; November 24th, 2009 at 12:54 AM.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  15. #15
    Join Date
    Apr 1999
    Posts
    27,434

    Re: Bar Charts in C++?

    Quote Originally Posted by Sabensohn70 View Post
    ..dude i am reading my textbook and it doesnt say anything about making bar charts with the for loop.
    Programming books do not work that way, where you see a solution for all the problems. If books were written that way, a programming book would be millions of pages long. You must use your mind and apply what concepts you've learned in solving the problem.

    You have a number, and you must loop n times per 100. How many times will the loop perform? The answer is n/100 (you may need to add 1 if the number is < 100). So now you loop n/100 times. Inside that loop, you print 1 star. Lo and behold, you have your output.

    In your code, you took your hard-earned data, and destroyed it when you used it as a loop counter. Just create a brand new variable and use that to count the loop. All you need is a counter, it makes no sense destroying your data and using that as the counter.
    if you are not going to try to help please do not answer at all okay?
    You were being helped, you just didn't know it because the answer wasn't given to you. Learning to program doesn't work that way, where you are given the answer. At some point, you have to engage your mind in thinking in logical, discrete, well-defined steps. If you can't do that, then this field of study is not for you. And believe me, it has nothing to do with intelligence -- some people can think with a "programming" brain, while others can't, regardless of how intelligent they may be in other topics.
    believe it or not there are beginners in this world
    Yes, and many of them, probably sitting in your class now, could finish this assignment with no problems, or at the very least, understood immediately what Joeman and laserlight are talking about.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; November 24th, 2009 at 04:12 AM.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center