Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Needing a little help with simple functions

Status
Not open for further replies.

mattw2319

Programmer
Oct 14, 2002
7
US
I am really at my wits end with this program. I have been tinkering with it for days. This program is a bank account program that processes checks deposits and withdrawls. It reads the data from an infile. The problem that I cannot figure out is that I am not getting any output at all except for 0's across the board. Im a beginner.. obviously after you read my code you can tell. We havent discussed classes yet. The account cant drop below $500 or theres a $5 surcharge, and if its below $50 at anypoint , it gives a warning.
Any advice/help with this would be greatly appreciated. I am a bundle of stress because of this. Thank you





float main ()
{
infile.open("MWhite4.dat");
outfile.open("MWhite4.out");




outfile<<setw(15)<<&quot;Transaction&quot;<<setw(15)<<&quot;amount&quot;<<setw(15)<<&quot;Date&quot;<<setw(17)<<&quot;Balance&quot;<<setw(10)<&quot;Notice&quot;<<endl;

inData(q,o,i,z);
while (infile)
{
if ( transaction == 'D')
deposit(A);

else if ( transaction == 'W')
withdrawl(A);

else if (transaction == 'C')
check (A);

if ((month == 1) && (balance < 500.00))
{
balance = balance - bService;
if (balance < 50.00)
outfile << &quot;Your monthly balance is lower than $50.00.&quot; << endl;
}

else if ((month == 2) && (balance < 500.00))
{
balance = balance - bService;
if (balance < 50.00)
outfile << &quot;Your monthly balance is lower than $50.00.&quot; << endl;
}

else if ((month == 3) && (balance < 500.00))
{
balance = balance - bService;
if (balance < 50.00)
outfile << &quot;Your monthly balance is lower than $50.00.&quot; << endl;
}

else if ((month == 4) && (balance < 500.00))
{
balance = balance - bService;
if (balance < 50.00)
outfile << &quot;Your monthly balance is lower than $50.00.&quot; << endl;
}
else if ((month == 5) && (balance < 500.00))
{
balance = balance - bService;
if (balance < 50.00)
outfile << &quot;Your monthly balance is lower than $50.00.&quot; << endl;
}

else if ((month == 6) && (balance < 500.00))
{
balance = balance - bService;
if (balance < 50.00)
outfile << &quot;Your monthly balance is lower than $50.00.&quot; << endl;
}

else if ((month == 7) && (balance < 500.00))
{
balance = balance - bService;
if (balance < 50.00)
outfile << &quot;Your monthly balance is lower than $50.00.&quot; << endl;
}

else if ((month == 8) && (balance < 500.00))
{
balance = balance - bService;
if (balance < 50.00)
outfile << &quot;Your monthly balance is lower than $50.00.&quot; << endl;
}

else if ((month == 9) && (balance < 500.00))
{
balance = balance - bService;
if (balance < 50.00)
outfile << &quot;Your monthly balance is lower than $50.00.&quot; << endl;
}

else if ((month == 10) && (balance < 500.00))
{
balance = balance - bService;
if (balance < 50.00)
outfile << &quot;Your monthly balance is lower than $50.00.&quot; << endl;
}

else if ((month == 11) && (balance < 500.00))
{
balance = balance - bService;
if (balance < 50.00)
outfile << &quot;Your monthly balance is lower than $50.00.&quot; << endl;
}

else if ((month == 12) && (balance < 500.00))
{
balance = balance - bService;
if (balance < 50.00)
outfile << &quot;Your monthly balance is lower than $50.00.&quot; << endl;
}

printData();
inData(q,o,i,z);
}



return 0;
}

void inData(char transaction, float amount, float month, float day)
{
infile >> transaction >> amount >> month >> day;

}
float deposit(float& balance)
{
balance = balance + amount - dService;
return balance;
}
float withdrawl(float& balance)
{
balance = balance - amount - wService;
return balance;
}
float check(float& balance)
{
balance = balance - amount - cService;
return balance;
}
void printData()
{
outfile <<setw(15)<<transaction<<setw(15)<<amount<<setw(10)<<month<<setw(5)<<day<<setw(17)<<setw(10)<<balance<<setw(10)<< endl;
}
 
2 questions -- where is infile declared (and outfile for that matter) and why is main a float?

dude, I am sleepy, so I cant read others code without getting a headache -- but, here are some general tips:

Make a smaller program one that just reads the input of your input file and prints it to the screen to test your understanding of just that.

...does it work or is that your problem? (smaller is easier to debug)

Do the same thing with the other basic concepts in your code.

Another thing, your code is really complicated so this isnt a huge problem, but if you comment sections of you code with outlines of their general goals it's easier for people to help you debug it.



 
I left out the declarations, and yes the infile and out file are declared. Also i can get the input file to echo to the output file, but only when I do the echoing inside the inData function. I know the codes a mess but its the only way i know as of now. I have float main() because I forgot to change it back to void while I was messing around to find anysort of fix for myself.
 
Hi!

I dont know what exactly your problem is, but did you ever recognize having the same code in each else if statement? why not writing this like:

if ( (month >= 1 || month <= 12) && balance < 500 ) {
balance = balance - bService;
if (balance < 50.00)
outfile << &quot;Your monthly balance is lower than $50.00.&quot; << endl;
}

or if you later want to have different code following:

if ( balance < 500 ) {
switch ( month ) {
// month1 processed separate
case 1:
// your code
break;
// month2 processed separate
case 2:
// your code
break;
// month3-4 processed together
case 3:
case 4:
case 5:
// your code
break;
}
}

Greetings
Remo
 
Either float or void are wrong for main(). Even if void is accepted, the only real correct return type is int. By the way, where are month and balance defined? And when do they get theyr values?

Greetings
Remo
 
Ok, I thank you for giving me a couple of pointers, this is my code now as it stands, and hopefully its a little easier to read. I am still not able to get any of the infile to print to the outfile other than zeros. From what I can tell my functions aren't even being used.


#include <iomanip>
#include <iostream>
#include <fstream>

using namespace std;
ifstream infile;
ofstream outfile;
float balance, amount, transaction;

float service;
const float nService = 10.00;
const float bService = 5.00;
int month, day, i, z;
float A, o;
char C, D, W, q;
void deposit(float&);
void withdrawl(float&);
void check(float&);
void inData(char, float, int, int);
void printData();

int main ()
{
infile.open(&quot;myfile.dat&quot;);
outfile.open(&quot;myfile.out&quot;);
balance = 0;

// make a headline for the outfile

outfile<<setw(15)<<&quot;Transaction&quot;<<setw(15)<<&quot;amount&quot;<<setw(15)<<&quot;Date&quot;<<setw(17)<<&quot;Service Charge&quot;<<setw(10)<<&quot;Balance&quot;<<setw(10)<<&quot;Notice&quot;<<endl;

// call the infile function to get the data

inData(q,o,i,z);

// use E.o.F. to read and calculate whole infile

while (infile)
{
if (transaction == 'D')
{ deposit(A);}
else if (transaction == 'W')
{ withdrawl(A);}
else if (transaction == 'C')
{ check (A);}

printData();

// create a deduction from balance if it dips below a certain amt of $

if ( (month >= 1 || month <= 12) && balance < 500 )
{
balance = balance - bService;
if (balance < 50.00)
outfile << &quot;Your monthly balance is lower than $50.00.&quot; << endl;
if (balance < 0)
balance = balance - nService;
}


// reread the infile to set up for next calculation
inData(q,o,i,z);
}



return 0;
}
// function to read infile and set names

void inData(char transaction, float amount, int month, int day)
{
infile >> transaction >> amount >> month >> day;

}
// function to add amount in second line of infile to the balance and subtract the service charge

void deposit(float& balance)
{
service = .1;
balance = balance + amount - service;

}

// function to subtract amount in second line of infile from balance and subtract the service charge

void withdrawl(float& balance)
{ service = .75;
balance = balance - amount - service;

}
// function to subtract amount in second line of infile from balance and subtract the service charge

void check(float& balance)
{ service = .15;
balance = balance - amount - service;

}
// function to print out parts from the infile, the new balance created from the functions and service charge

void printData()
{
outfile <<setw(15)<<transaction<<setw(15)<<amount<<setw(10)<<month<<setw(5)<<day<<setw(17)<<service<<setw(10)<<balance<<setw(10)<< endl;
}
 
The first thing I notice is that you get data using inData with variables (q, o, i, z).


Since you have declared inData like this:

void inData(char transaction, float amount, int month, int day)
{
infile >> transaction >> amount >> month >> day;
}

it has its own overidden versions of variables transaction, amount, month, and day (they are not the global versions). I think you're assuming that the global variables will be changed.

Additionally, because you are passing variables &quot;by value&quot;, what you are passing is not changed in function main, rather a copy of each variable is made in function inData, and that is what is being manipulated.

To correct these problems, without totally changing the style of your code (which would be good, but you'll learn about aesthetics later), I recommend you start by doing this:

declare inData's variables of this differently, and have variables passed by reference:

void inData(char &trans, float &amnt, int &mnth, int &dDay)
{
infile >> trans >> amnt >> mnth >> dDay;
}

Then, when you call inData, pass transaction, amount, month, and dDay directly, because those are the values your other functions work with. You can get rid of q, o, i, and z.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top