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?
Can anyone please advise – I’d be most greatful?
Many Thanks
Cbo