Thread: Program prints garbage

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    36

    Program prints garbage

    Code:
    #include <iostream>
    using namespace std;
    #include <string>
    class IceCreamCone
    {
        private:
            string flavor;
            int numOfScoops;
            string typeOfCone;
            float price;
        public:
            IceCreamCone(string = "Vanilla",  int = 1 , string = "Sugar");
            void setPrice();
            float getPrice();
    };
    void IceCreamCone::setPrice()
    {
        price = 0;
    }
    float IceCreamCone::getPrice()
    {
        return price;
    }
    IceCreamCone::IceCreamCone(string flvr, int numScoops , string typeCone )
    {
        setPrice();
        price = 0.75 * (float)numOfScoops;
        if( typeCone == "waffle")
            price += 0.40;
    
    
    }
    int main(void)
    {
        IceCreamCone iceCream;
        IceCreamCone iceCreamTwo("chocolate", 3 , "waffle" );
        
        cout << iceCream.getPrice() << endl;
        cout << iceCreamTwo.getPrice() << endl; // prints "garbage"
        
        return 0;
    }
    In the main function after calling function getPrice() (line 39) for the object iceCreamTwo it ends up printing garbage,
    Code:
    cout << iceCreamTwo.getPrice() << endl;
    and I am not quite sure why it happens, since program prints good number for iceCream object.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Actually both of your output lines are printing garbage, just different garbage.

    You need to check to insure that your member variables are properly initialized in your constructor. By the way I don't recommend using default values for constructor parameters.

    Jim

  3. #3
    Registered User
    Join Date
    Mar 2016
    Posts
    36
    Quote Originally Posted by jimblumberg View Post
    Actually both of your output lines are printing garbage, just different garbage.

    You need to check to insure that your member variables are properly initialized in your constructor. By the way I don't recommend using default values for constructor parameters.

    Jim
    I don't see how member variables in the constructor are improperly initialized , could you just give me some hint , I have just started to learn about C++.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I don't see how member variables in the constructor are improperly initialized
    Show me where you think you have initialized all of the member variables in your class.
    Code:
        private:
            string flavor;
            int numOfScoops;
            string typeOfCone;
            float price;
    ...
    IceCreamCone::IceCreamCone(string flvr, int numScoops , string typeCone )
    {
        setPrice();
        price = 0.75 * (float)numOfScoops;
        if( typeCone == "waffle")
            price += 0.40;
     
     
    }
    Jim

  5. #5
    Registered User
    Join Date
    Mar 2016
    Posts
    36
    Quote Originally Posted by jimblumberg View Post
    Show me where you think you have initialized all of the member variables in your class.
    Code:
        private:
            string flavor;
            int numOfScoops;
            string typeOfCone;
            float price;
    ...
    IceCreamCone::IceCreamCone(string flvr, int numScoops , string typeCone )
    {
        setPrice();
        price = 0.75 * (float)numOfScoops;
        if( typeCone == "waffle")
            price += 0.40;
     
     
    }
    Jim
    Sorry, I didn't initialize all of the member variables ( I kinda mix terms initialization and assignment , since I am not native english speaker) and , I am kinda confused right now , please could you tell me why would I need to initialize all the member variables.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Because if you don't initialize (assign) values to your variables they have undefined values (garbage).

    I kinda mix terms initialization and assignment
    The only real difference between initialization and assignment is that initialization happens once, when you define the variable. After the variable is defined you assign values to the variable.

    Code:
    // Definition, no initialization.
    int n; 
    
    // Definition with initialization.
    int m = 10;
    int i = m;
    
    // Assignment.
    n = 100;
    m = n;
    In your constructor the only variable you initialized was price (to zero). All the other variables have undefined values.

    Jim

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Just a comment on your class composition.

    I find setPrice() a bit useless the way it is in your code. In fact, since the price is entirely dependent on other factors, maybe it is best to calculate it every time you need it.
    Code:
    float IceCreamCone::getPrice() const
    {
       float price = 0.75 * numOfScoops;
       if (typeOfCone == "waffle")
          price += 0.40;
       return price;
    }
    If you do it like this, then there is no reason to store price as member data. If you think about it, you should see how editing other attributes of the cone will keep the price correct.
    Last edited by whiteflags; 08-14-2016 at 01:07 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 02-10-2015, 12:06 PM
  2. Replies: 3
    Last Post: 01-28-2015, 10:53 AM
  3. Replies: 2
    Last Post: 09-06-2011, 01:08 AM
  4. Why the program prints line twice?
    By red463 in forum C Programming
    Replies: 15
    Last Post: 04-30-2010, 05:20 AM
  5. program that prints itself
    By modec in forum C Programming
    Replies: 1
    Last Post: 10-10-2003, 02:14 PM

Tags for this Thread