Find the answer to your Linux question:
Results 1 to 10 of 10
Like Tree2Likes
  • 1 Post By Roxoff
  • 1 Post By Roxoff
Hello guys and gals I am working on a school assignment and we are supposed to make a program that produces a graph sales that the user inputs. But I ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Newbie
    Join Date
    Sep 2012
    Posts
    148

    Sales Graph in C++


    Hello guys and gals I am working on a school assignment and we are supposed to make a program that produces a graph sales that the user inputs. But I am having trouble with it as it is only showing a graph for 3 out of the 5 as seen below
    programscreenshot.jpg
    and here is the code:

    Code:
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    int main(void)
    {
        int store1;
        int store2;
        int store3;
        int store4;
        int store5;
        int divStore1;
        int divStore2;
        int divStore3;
        int divStore4;
        int divStore5;
        int gstore1;
        int gstore2;
        int gstore3;
        int gstore4;
        int gstore5;
    
    
    
        cout << "Enter today's sales for store 1: ";
        cin >> store1;
    
        cout << "Enter today's sales for store 2: ";
        cin >> store2;
    
        cout << "Enter today's sales for store 3: ";
        cin >> store3;
    
        cout << "Enter today's sales for store 4: ";
        cin >> store4;
    
        cout << "Enter today's sales for store 5: ";
        cin >> store5;
    
        cout << "SALES CHART" << endl;
        cout << "(* = $100)" << endl;
        cout << "Store 1: ";
        divStore1 = store1 / 100;
        for(gstore1; gstore1 < divStore1; gstore1++)
        {
            cout << "*";
        }
        cout << endl;
        divStore2 = store2 / 100;
        cout << "Store 2: ";
        for(gstore2; gstore2 < divStore2; gstore2++)
        {
            cout << "*";
        }
        cout << endl;
        divStore3 = store3 / 100;
        cout << "Store 3: ";
        for(gstore3; gstore3 < divStore3; gstore3++)
        {
            cout << "*";
        }
        cout << endl;
        divStore4 = store4 / 100;
        cout << "Store 4: ";
        for(gstore4; gstore4 < divStore4; gstore4++)
        {
            cout << "*";
        }
        cout << endl;
        divStore5 = store5 / 100;
        cout << "Store 5: ";
        for(gstore5; gstore5 < divStore5; gstore5++)
        {
            cout << "*";
        }
    
    }
    I have no idea why its doing this all the lines are supposed to do the same thing but just those two don't want to output anything?

  2. #2
    Linux Enthusiast gruven's Avatar
    Join Date
    Dec 2004
    Location
    Arkansas
    Posts
    541
    The first thing I see wrong is that none of the gstore variables are initialized. Initialize them in the for loops to 0 and see if that helps (gstore1 = 0; and do that for 1 through 5). I will look into it more when not on my phone.

    Linux User #376741
    Code is Poetry

  3. #3
    Linux Newbie
    Join Date
    Sep 2012
    Posts
    148
    Hey what worked man thanks, I seem to forget tho initialize my variables alot. But thanks its working now.

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Newbie
    Join Date
    Sep 2012
    Posts
    148
    But I am curious, why does the variable not set to the number when I set it to gstore < divstore?

  6. #5
    Super Moderator Roxoff's Avatar
    Join Date
    Aug 2005
    Location
    Nottingham, England
    Posts
    3,933
    Quote Originally Posted by awesomepilot View Post
    But I am curious, why does the variable not set to the number when I set it to gstore < divstore?
    That is not setting the value, the statement:

    Code:
    gstore < divstore
    evaluates to a boolean value of true or false - it does not change the variable values.

    In C++ you should (as you have discovered) initialise your variables like this:

    Code:
    int gstore1(0);
    but the 'int' type is a built-in fundamental type, it is not a class - so no constructor is called, hence it has no default value.
    gruven likes this.
    Linux user #126863 - see http://linuxcounter.net/

  7. #6
    Linux Enthusiast gruven's Avatar
    Join Date
    Dec 2004
    Location
    Arkansas
    Posts
    541
    Quote Originally Posted by awesomepilot View Post
    But I am curious, why does the variable not set to the number when I set it to gstore < divstore?
    In simplest terms, < is not an assignment operator. Roxoff is correct in everything he said, but I would disagree with one thing. I was always taught to use
    Code:
    int gstore1 = 0;
    instead of
    Code:
    int gstore1(0);
    .
    They both do the same thing, but to a beginner, the assignment operator allows you to easily see that the variable is set to that value. When you use the other notation, it may confuse beginners with functions, and there is plenty of new stuff for them to get confused with that isn't this, lol.

    Anyway, to initialize a variable, you MUST set it one of these ways. Without explicit initialization, your variable will just be equal to whatever was in that spot in memory at the time (it could be random nonsense or a real number).

    Also, a for loop declaration looks for information in certain spots in the declaration. it is as so:
    Code:
    for (variable (such as int i = 0); conditional (such as i < foo); thing to be done (such as i++));
    If you are using variables just for for loops, don't declare them at the top. If they will be used for nothing else but the for loop, then declare them in the for loop when initializing like this:
    Code:
    for(int gstore2 = 0; gstore2 < divStore2; gstore2++)
        {
            cout << "*";
        }
    I have found that doing it this way makes it easier to remember to initialize your variable for the for loop so the count will be correct.

    Linux User #376741
    Code is Poetry

  8. #7
    Super Moderator Roxoff's Avatar
    Join Date
    Aug 2005
    Location
    Nottingham, England
    Posts
    3,933
    Quote Originally Posted by gruven View Post
    ...I was always taught to use
    Code:
    int gstore1 = 0;
    instead of
    Code:
    int gstore1(0);
    .
    They both do the same thing, but to a beginner, the assignment operator allows you to easily see that the variable is set to that value...
    While these have the same effect, they're different. The first one is the C type initialisation; the newest C++ spec. recommends the second way. As this is a built in type, the value is assigned regardless, but if this had been assigning a value to an instance of a class, you'd want to use the second way exclusively. For this reason, the second way should always be used - it may be slightly less intuitive, but it can be applied consistently.
    gruven likes this.
    Linux user #126863 - see http://linuxcounter.net/

  9. #8
    Linux Enthusiast gruven's Avatar
    Join Date
    Dec 2004
    Location
    Arkansas
    Posts
    541
    I didn't know that, thank you for clarifying.

    But, chances are his university is still using C++98 (as are most universities) and will that work in 98? Another variable to consider is what his instructor will do if he uses that.

    Most universities teach a basic structured programming class. While the language used is c++, it is taught as c with some c++ thrown in. I don't know what year he is in, but chances are he is going to need to use the assignment operator.

    Linux User #376741
    Code is Poetry

  10. #9
    Linux Enthusiast gruven's Avatar
    Join Date
    Dec 2004
    Location
    Arkansas
    Posts
    541
    After doing some research, Roxoff is completely correct. You should initialize your variables the way he says according to the c++ spec. It will work in all versions of c++, although it is not compatible with c. But we are not learning c.

    Thank you Roxoff, you have taught me something new, and I am always willing to learn.

    Linux User #376741
    Code is Poetry

  11. #10
    Super Moderator Roxoff's Avatar
    Join Date
    Aug 2005
    Location
    Nottingham, England
    Posts
    3,933
    Just to clarify this a little more for those learning this interesting language. Here's an example I pulled out my donkey:

    Code:
    class a { ... };
    a b = 49;
    The compiler should do the following things (this may be optimized, of course):

    * create an instance of a on the stack and pass it the value 49, if a suitable constructor can be found (else a compiler error is generated)
    * create 'b' by calling the copy constructor passing in the instance on the stack
    * the default copy constructor does the actual work, copying in member variable values

    This is long-winded, and actually quite dangerous if there are pointers involved. And, of course, the smart programmer will have inhibited the copy constructor if the class contains pointers (unless they're using smart pointers, boost::shared_ptr<...> or similar - but that's a template class and a whole new world of learning and potential pain...)

    Code:
    class a { ... };
    a b(49);
    * create instance b of class a by calling it's most appropriate constructor. If there isn't a suitable constructor a compiler error is generated.

    This is clearly a lot shorter and safer. I believe the general process is also applicable to other C++ like languages such as C#, Java, perhaps even PHP.
    Linux user #126863 - see http://linuxcounter.net/

Posting Permissions

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