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!

little function help please

Status
Not open for further replies.

mattw2319

Programmer
Oct 14, 2002
7
US
I am having a problem with this program reading in the first line of my infile. It is skipping automatically to read the second line and also adding a line in the end that I have no idea where it would have come from, its an exponential number with no relation at all to my infile. The program is supposed to read 4 change amounts from infile and convert to the fewest change amounts. Any help would be greatly appreciated. The first line that it is skipping is a value of 0.49 fron the infile. Thank you


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

using namespace std;

ifstream infile;
ofstream outfile;

int sumQuarter, sumDime, sumNickle, sumPenny, amount;
float dollar;

const int dollar2cent = 100;
const int quarter = 25;
const int dime = 10;
const int nickle = 5;
const int penny = 1;


int inQuarter(int);
int inDime(int);
int inNickle(int);
int inPenny(int);
float getData();
int printChange();


int main()
{
infile.open (&quot;money.dat&quot;);
outfile.open (&quot;money.out&quot;);

dollar = getData ();

while (infile)
{
dollar = getData ();
amount = dollar * dollar2cent;
sumQuarter = inQuarter(amount);
sumDime = inDime(amount);
sumNickle = inNickle(amount);
sumPenny = inPenny(amount);
printChange();


}


}

int inQuarter(int amount)
{
int num;
num = amount / quarter;
return num;
}

int inDime(int amount)
{
int num;
amount = amount % quarter;
num = amount / dime;
return num;
}


int inNickle(int amount)
{
int num;
amount = amount % quarter;
amount = amount % dime;
num = amount / nickle;
return num;
}


int inPenny(int amount)
{
int num;
amount = amount % quarter;
amount = amount % dime;
amount = amount % nickle;
num = amount;
return num;
}

float getData()
{
float dollar;
infile >> dollar;
return dollar;
}


int printChange()
{
outfile << &quot;$&quot; << dollar << &quot;is changed to&quot; << sumQuarter << &quot;quarter(s)&quot; <<
sumDime << &quot;dime(s)&quot; << sumNickle << &quot;nickle(s)&quot; << sumPenny << &quot;penny(s)&quot; << endl;
return 0;
}

 
I see your first problem. Your code logic is skipping the first value read in your code. The second problem is you are checking to break the loop only after the file is read past the end of file. Your Main() should read as follows
(I haven't tested it, but you can see the logic change):

int main()
{
infile.open (&quot;money.dat&quot;);
outfile.open (&quot;money.out&quot;);

dollar = getData ();

while (infile)
{
amount = dollar * dollar2cent;
sumQuarter = inQuarter(amount);
sumDime = inDime(amount);
sumNickle = inNickle(amount);
sumPenny = inPenny(amount);
printChange();

dollar = getData ();
}
}
 
Thank you very much, don't believe I didn't see that. Appreciate it
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top