Program calculation problems?

This is a discussion on Program calculation problems? within the C++ Programming forums, part of the General Programming Boards category; Hi Guys, Wondering if anyone can help. Trying to create a simple program which: - Inputs the price of presents ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    25

    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?

    Can anyone please advise – I’d be most greatful?

    Many Thanks

    Cbo

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    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.
    Last edited by SlyMaelstrom; 11-28-2005 at 02:57 AM.
    Sent from my iPad®

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    1) By convention, class names are capitalized.
    Code:
    class christmas_presents
    2) Always initialize your variables:
    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.
    Last edited by 7stud; 11-28-2005 at 05:27 AM.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    25
    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

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    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.

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    25
    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)

    Any ideas please?

    Ta

    Cbo

  7. #7
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    cout <<"You have spent: " << fixed << setprecision(2) << total_present_price << " pounds" <<endl;
    Kurt

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    You only have to call setprecision() once. It will remain that precision until you change it. Unlike setw() which must be called before each use.
    Sent from my iPad®

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with my c letter count program
    By cram55 in forum C Programming
    Replies: 10
    Last Post: 05-30-2007, 04:10 AM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 10:17 PM
  3. GPA Program Problems
    By Clint in forum C Programming
    Replies: 3
    Last Post: 04-28-2005, 10:45 AM
  4. Trying to create a payroll calculation program
    By nadeni0119 in forum C++ Programming
    Replies: 1
    Last Post: 04-01-2003, 03:14 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21