First let me start by saying that I hope I wrapped my code properly.

Next, my instructor sent us some code using a class and instructed us to add to it and make it so that we can display a checking account balance from a simple three line text file, then add and subtract from that balance.

I will get the adding and subtracting, but what is giving me problems is how do I pass a value from my "deposit" variable up to my money::addchecking function? I have watched a half dozen videos, and thought I followed them correctly but I am doing something wrong.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int checkingfunction();


class money
{
 double checking;
 double savings;
 double cash;
 
public:
 double deposit (0);
 money();
 ~money();
 double showcash();
 double showchecking();
 double addchecking();
 double subtractchecking();
};

money::money()
{
 char readline[100];
 int pennies;
 ifstream inputFile;

 inputFile.open("balance.txt");
 if (inputFile.good())
 {
  inputFile >> readline;
  pennies = atoi(readline);
  checking = pennies/100 + (pennies%100)*.01;
  inputFile >> readline;
  pennies = atoi(readline);
  savings = pennies/100 + (pennies%100)*.01;
  inputFile >> readline;
  pennies = atoi(readline);
  cash = pennies/100 + (pennies%100)*.01;
  inputFile.close();
 }else cout << "file not found" << endl;
}
money::~money()
{
 int pennies; 
 char writeline[100];
 
 ofstream outputFile;
 outputFile.open("balance.txt");
 pennies = checking*100;
 itoa(pennies, writeline, 10);
 outputFile << pennies << endl;
 pennies = savings*100;
 itoa(pennies, writeline, 10);
 outputFile << pennies << endl;
 pennies = cash*100;
 itoa(pennies, writeline, 10);
 outputFile << pennies << endl;
 outputFile.close();
}

double money::showcash()
{
 return cash;
 
}

 double money::showchecking ()
	{
		
return checking;
	}

double money::addchecking()
	{
	checking=checking+deposit;
	return checking;	
	}


int main (int argc, char* argv[])
{
            money roger_money;

            cout << "cash on hand is " << roger_money.showcash() << endl;
  

money checkdeposit;

	cout << "checking is " <<checkdeposit.showchecking() << endl; 
	double deposit;
	 cout << "How much would you like to deposit to checking?"<< endl;  //Ask for deposit amount
	 cin >> deposit;
	 cout << "deposit amount is $"<< deposit<< endl; //Show amount being Deposited
	 checkdeposit.addchecking(deposit)
           
//how do I pass "deposit" to the function double money::addchecking()?


 system("pause");
 return 0;
}

Thank you for using code tags. You used them correctly.

Regarding your question...

how do I pass a value from my "deposit" variable up to my money::addchecking function?

you DO NOT pass "deposit". See line 15. "deposit" is declared as a class variable. That means no passing is necessary it's already there. If "deposit" is not SUPPOSED to be a class variable, you'll need to delete line 15. Line 95 is incorrect or at least needs to be improved / changed / made consistent.

You declare a variable called "deposit" on line 93. Note that the variables called "deposit" on lines 15 and 93 are completely different variables. Now look at line 97.

checkdeposit.addchecking(deposit)

You are passing the addchecking function a parameter. Now look at lines 20 and 76.

double money::addchecking()

This function takes NO parameters. They need to match. They do not. You need to make a decision. Should "deposit" be a class variable or not? If it should be a class variable, delete line 93 and wherever you see "deposit" below that, replace it with this and get rid of the parameter so everything matches...

checkdeposit.deposit
money checkdeposit;
 
cout << "checking is " <<checkdeposit.showchecking() << endl;
cout << "How much would you like to deposit to checking?"<< endl; //Ask for deposit amount
cin >> checkdeposit.deposit;
cout << "deposit amount is $"<< checkdeposit.deposit<< endl; //Show amount being Deposited
checkdeposit.addchecking(); // remove the parameter here

That is one way. I think a BETTER way would be to NOT have "deposit" be a class variable. Delete line 15. Keep the code in main pretty much the same, but change the addchecking function so that it takes a parameter.

double money::addchecking(double deposit)
{
checking=checking+deposit;
return checking;
}

OK, i nearly worked out all the bugs in this code. However, now when I run the program, it loops twice through the ~destructor, and the second time through erases the file I have it write to. I have stepped through the code and I still can't tell what is making it run through twice or how to get it to stop the second run through. Is there a loop I am not seeing?

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int checkingfunction();


class money
{
 double checking;
 double savings;
 double cash;
 
public:
  money();
 ~money();
 double showcash();
 double showchecking();
 double addchecking(double deposit);
 double subtractchecking();
};

money::money()
{
 char readline[100];
 int pennies;
 ifstream inputFile;

 inputFile.open("balance.txt");
 if (inputFile.good())
 {
  inputFile >> readline;
  pennies = atoi(readline);
  checking = pennies/100 + (pennies%100)*.01;
  inputFile >> readline;
  pennies = atoi(readline);
  savings = pennies/100 + (pennies%100)*.01;
  inputFile >> readline;
  pennies = atoi(readline);
  cash = pennies/100 + (pennies%100)*.01;
  inputFile.close();
 }
 
 else cout << "file not found" << endl;
}





money::~money()
{
 int pennies;
 char writeline[100];
 
 ofstream outputFile;
 outputFile.open("balance.txt");
 pennies = checking*100;



 itoa(pennies, writeline, 10);
 outputFile << pennies << endl;
 pennies = savings*100;
 itoa(pennies, writeline, 10);
 outputFile << pennies << endl;
 pennies = cash*100;
 itoa(pennies, writeline, 10);
 outputFile << pennies << endl;
 outputFile.close();

 

 }






double money::showcash()
{
 return cash;
 
}

 double money::showchecking ()
    {
     
return checking;
    }

double money::addchecking(double deposit)
    {
    checking=checking+deposit;
    return checking; 
    }


int main (int argc, char* argv[])
{
  money roger_money;

  cout << "Cash on hand is  $" << roger_money.showcash() << endl;
 
 

    money checkdeposit;

 
    double deposit;
    cout << "Checking Balance $" <<checkdeposit.showchecking() << endl;
    //cout << "cash on hand is " << checkdeposit.showcash() << endl;                    //check to see what checkdeposit.showcash() is
    loop:
    cout <<"\n" << "How much would you like to deposit to checking?"<< endl;  //Ask for deposit amount
    cin >> deposit;
 
        if (deposit <= checkdeposit.showcash())
            {
                cout << "Deposit amount is $"<< deposit<< endl; //Show amount being Deposited
                cout << "New Balance is  $" << checkdeposit.addchecking(deposit) << endl;
            }

        else
            {
                cout << "That is more than the available balance Please try again " << endl;
                goto loop;
            }


    system ("pause");
    return (0);

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.