Thread: Private Variable assigning

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    73

    Private Variable assigning

    I'm working on a program that will take user input to display how much change you have, a friend has, and you have together. The code is below.

    My problem is in my output. The output file shows the following:
    You have $-8.58993e+008.
    Your friend has $-8.58993e+008.
    Together you have $-1.71799e+009.

    Can you tell me what is causing it?

    Here is my class.
    Code:
    #pragma once
    class change
    {
    private:
    	int pennies;
    	int nickels;
    	int dimes;
    	int quarters;
    public:
    	change(void);
    	change(int p, int n, int d, int q);
    	~change(void);
    	void SetPennies(int p);
    	void SetNickels(int n);
    	void SetDimes(int d);
    	void SetQuarters(int q);
    	int GetPennies();
    	int GetNickels();
    	int GetDimes();
    	int GetQuarters();
    	double GetValue(change c);
    	change AddCoins(change c);
    };
    Here is my program.
    Code:
    #include <iostream>
    #include <fstream>
    #include "change.h"
    using namespace std;
    
    
    void header(ofstream &outfile);
    void ChangeInput(change c1, change c2);
    void PrintChange(ofstream &outfile, change c1, change c2, change c3);
    
    
    int main()
    {
    	ofstream outfile;
    	outfile.open("output.txt");
    
    
    	change c1, c2, c3;
    
    
    	ChangeInput(c1, c2);
    	c3 = c1.AddCoins(c2);
    
    
    	header(outfile);
    	PrintChange(outfile, c1, c2, c3);
    
    
    	system("pause");
    	return 0;
    }
    
    
    void header(ofstream &outfile)
    {
    	outfile << "Trey Brumley" << endl;
    	outfile << "CMPS 1063-201" << endl;
    	outfile << "Dr. Tina Johnson" << endl;
    	outfile << "April 1, 2013" << endl;
    	outfile << "Program 3 - Classes Extended" << endl;
    	outfile << "This program will further demonstrate the use of classes by using a class designed to convert monetary change into cents values." << endl;
    	outfile << "================================================================================================================================" << endl << endl;
    
    
    	return;
    }
    
    
    void ChangeInput(change c1, change c2)
    {
    	int p, n, d, q;
    	cout << "Please enter the number of coins you have (pennies, nickels, dimes, quarters):" << endl;
    	cin >> p >> n >> d >> q;
    	c1.SetPennies(p);
    	c1.SetNickels(n);
    	c1.SetDimes(d);
    	c1.SetQuarters(q);
    
    
    	cout << endl << "Please enter the number of coins your friend has (pennies, nickels, dimes, quarters):" << endl;
    	cin >> p >> n >> d >> q;
    	c2.SetPennies(p);
    	c2.SetNickels(n);
    	c2.SetDimes(d);
    	c2.SetQuarters(q);
    
    
    	return;
    }
    
    
    void PrintChange(ofstream &outfile, change c1, change c2, change c3)
    {
    	outfile << "You have $" << c1.GetValue(c1) << "." << endl;
    	outfile << "Your friend has $" << c2.GetValue(c2) << "." << endl;
    	outfile << "Together you have $" << c3.GetValue(c3) << "." << endl;
    
    
    	return;
    }
    
    
    change::change(void)
    {
    }
    
    
    
    
    change::~change(void)
    {
    }
    
    
    change::change(int p, int n, int d, int q)
    {
    	pennies = p;
    	nickels = n;
    	dimes = d;
    	quarters = q;
    }
    
    
    void change::SetPennies(int p)
    {
    	pennies = p;
    }
    
    
    void change::SetNickels(int n)
    {
    	nickels = n;
    }
    
    
    void change::SetDimes(int d)
    {
    	dimes = d;
    }
    
    
    void change::SetQuarters(int q)
    {
    	quarters = q;
    }
    
    
    int change::GetPennies()
    {
    	return pennies;
    }
    
    
    int change::GetNickels()
    {
    	return nickels;
    }
    
    
    int change::GetDimes()
    {
    	return dimes;
    }
    
    
    int change::GetQuarters()
    {
    	return quarters;
    }
    
    
    double change::GetValue(change c)
    {
    	double total;
    
    
    	total = c.pennies+(5*c.nickels)+(10*c.dimes)+(25*c.quarters)/100.0;
    
    
    	return total;
    }
    
    
    change change::AddCoins(change c)
    {
    	change ch;
    	ch.pennies = pennies + c.pennies;
    	ch.nickels = nickels + c.nickels;
    	ch.dimes = dimes + c.dimes;
    	ch.quarters = quarters + c.quarters;
    
    
    	return ch;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    One of the problems is that you are passing variables by value when you want to change the values. Remember passing by value "copies" the variable so any changes to this copy are lost when the function returns. For example the following snippet:
    Code:
    void ChangeInput(change c1, change c2)
    {
        int p, n, d, q;
        cout << "Please enter the number of coins you have (pennies, nickels, dimes, quarters):" << endl;
        cin >> p >> n >> d >> q;
        c1.SetPennies(p);
        c1.SetNickels(n);
        c1.SetDimes(d);
        c1.SetQuarters(q);
     
     
        cout << endl << "Please enter the number of coins your friend has (pennies, nickels, dimes, quarters):" << endl;
        cin >> p >> n >> d >> q;
        c2.SetPennies(p);
        c2.SetNickels(n);
        c2.SetDimes(d);
        c2.SetQuarters(q);
     
     
        return;
    }
    Any changes to the parameters are lost when the function returns. You probably want to pass these variables by reference.

    Jim

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > void ChangeInput(change c1, change c2)
    Look at passing these by reference.

    Your local changes are being lost when the function returns.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    73
    So do I just add the ampersands?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Try it and see.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Also, that class does not warrant having a destructor. Just delete it.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    73
    I have to include the destructor as part of the assignment.

    Also, everywhere I put the ampersands, I get an error of some kind.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Show the modified code and the complete error message.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assigning a pointer to a variable
    By rlesko in forum C Programming
    Replies: 7
    Last Post: 11-15-2010, 12:45 PM
  2. Assigning a name to a variable
    By PAragonxd in forum C++ Programming
    Replies: 14
    Last Post: 12-13-2007, 05:35 AM
  3. Assigning Binary Value to a Variable
    By JAYMAN in forum C Programming
    Replies: 2
    Last Post: 11-06-2005, 01:24 PM
  4. Assigning Variable to arry
    By Vicked in forum Windows Programming
    Replies: 3
    Last Post: 04-13-2003, 08:09 PM
  5. can't get it to reassign the private variable...
    By talz13 in forum C++ Programming
    Replies: 4
    Last Post: 11-13-2002, 01:34 PM