# Program calculation problems?

• 11-28-2005
rebel
Program calculation problems?
Hi Guys,

Wondering if anyone can help. Trying to create a simple program which:

- Inputs the price of presents bought

Now when the user has finished entering the price of all the presents bought (they press 0) it:

- Outputs a count of the presents bought
- Outputs a count of the total price of all the presents added up
- Outputs out the average price of the presents bought

Now I have got the program to run, but it does not output the correct information as shown below – can anyone please advise?

http://www.thisisslough.com/c_plus.jpg

Here is my code:

presents.h

Code:

```// PRESENTS.H // Program for calculating Christmas Presents bought #include <iostream> using namespace std; #include<iomanip> #include <string> class christmas_presents         {   public:             void calculate_average_price() ;             void display_presents_bought() ;             void display_presents_spent() ;             void display_average_price();   private:   int present_price;   int number_of_presents;   int total_present_price;   int average_price;   }; void christmas_presents:: calculate_average_price() { char terminator; cout << "Please input the first price of the present (or 0):"; cin >> present_price; int number_of_presents = 0; int total_present_price = 0; int average_price = 0; while (present_price !=0)   {   total_present_price =+ present_price;   ++ number_of_presents;   cout << "Please input the next price of the present (or 0):";   cin >> present_price;   } cin.get (terminator); if (number_of_presents > 0)   average_price = total_present_price / number_of_presents; else   average_price = 0; } void christmas_presents:: display_presents_bought()   {   cout <<"You have bought: " << setprecision(2) << number_of_presents << " presents" <<endl; } void christmas_presents:: display_presents_spent()   {   cout <<"You have spent: " << setprecision(2) << total_present_price << " pounds" <<endl; } void christmas_presents:: display_average_price()   {   cout <<"On average you have spent: " << setprecision(2) << average_price <<endl; }```
Main program to get it to run

Code:

```// ASSIGN78.CPP // A Program to display christmas presents #include "presents.h" #include <cstdlib> int main ()         {     christmas_presents price;     price.calculate_average_price ();     price.display_presents_bought ();     price.display_presents_spent ();     price.display_average_price ();     system ("pause");         }```
- Also at the moment it only seems to let you input whole numbers?

Many Thanks

Cbo
• 11-28-2005
SlyMaelstrom
Code:

```            void display_presents_bought() ;             void display_presents_spent() ;             void display_average_price();```
These don't work too well without getting the information you expect them to output. This may be the problem, but I'm not too proficient in classes, I'll change the code a bit see if it works, but it's clear that at the time of output, your code isn't filling the variables and outputting garbage. Also, you should probably initialize these variables in the contructor.

And lastly, =+ just sets the value equal to a positive version of your variable. You want +=.

[EDIT] Yeah this was the problem. You have to pass the variables to the functions.
• 11-28-2005
7stud
1) By convention, class names are capitalized.
Code:

`class christmas_presents`
Code:

`char terminator;`
In that regard, you also need to initialize your member variables. Initialization of member variables is done in a constructor.

3) There is no reason for present_price to be a member variable: it's sole purpose is to get input from the user, and it has no meaning after your calculate_average_price() function ends.

4) These declarations:
Code:

```int number_of_presents = 0; int total_present_price = 0; int average_price = 0;```
create local variables with those names in your function. Those local variable names mask your member variable names and prevent you from accessing the member variables.

When execution enters a normal function(one that is not part of a class) the variables that are in scope(i.e. the variable names you are able to use) are the function parameter names and any variables that are declared within the function. However, the member functions of a class can "see" outside themselves, and from within a member function all the other members of the class are visible, so if you want to access a member variable, you just use it's name. For instance,
Code:

`number_of_presents = 10;`
Similarly, if you want to execute one of the other member functions, you just call it.

5) You probably want to make your average price a double type; otherwise you will only get whole number averages. Try entering the prices: 10 and 5 and see what the output is.
• 11-28-2005
rebel
Thanks guys,

Got it working now apart from entering decimal places.

Say I enter a whole number it works fine, but if I enter 3.50 for example, it throws a wobbily!

I set double average_price;, but it still does let me put in decimal places?

Thanks

Cbo
• 11-28-2005
7stud
Quote:

I set double average_price;, but it still does let me put in decimal places?
Every variable in your program that is assigned a value with decimal places has to be a double or else the decimal part will get chopped off.
• 11-28-2005
rebel
Aggh,

I see thanks for your help!

Just one last thing. When it comes to outputting what has been spent it does not output it to 2 decimal places, even though I have made total_present_price and average_price doubles and setprecision(2) on my output as shown below ?

Code:

```  cout <<"You have bought: " << setprecision(2) << number_of_presents << " presents" <<endl;   cout <<"You have spent: " << setprecision(2) << total_present_price << " pounds" <<endl;   cout <<"On average you have spent: " << setprecision(2) << average_price <<endl;```
Say I input 13.00 then 12.00 and press 0 it just says:

You have spent: 25 pounds (Not 25.00, which is what I want it to do)

On average you have spent: 13 pounds (Not 13.00, which is what I want it to do)

`cout <<"You have spent: " << fixed << setprecision(2) << total_present_price << " pounds" <<endl;`