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;
}