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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C++ help (beginner)

Status
Not open for further replies.

killa3d

Technical User
Apr 1, 2002
1
US
a program needs to calculate and display the total of the weekly payroll amounts stored in the data file

the data file has the following info :

25000.89
35600.50
45678.99
67000.89

now the only problem is that when I run the program it does not give me the correct answer which is 173280.94, what am I doing wrong??
and here is the program that I have so far:

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
//declare variables
float total = 0.0;
int counter = 0;

//open file
ifstream inFile;
inFile.open (&quot;T13ConE07.dat&quot;, ios::in);

//determine if the file was opened
if (inFile.is_open())
{
//declare varables
float payroll = 0;

//read and display records
while (!inFile.eof())
{
inFile >> payroll;
inFile.ignore();
counter = counter + 1;

//display records
cout << fixed;
cout.precision(2);
cout << payroll <<endl;

}//end while
//close file
//calculate total
total = payroll;
inFile.close();
cout << &quot;Total is: &quot; << total <<endl;
}
else
cout << &quot;File could not be opened. &quot; <<endl;
//end if

return 0;
}//end of main function
 
Hi
I don't know if you just forgot to copy it in your message, or maybe I am just missing it (it's still early on this side of the world) but a line like &quot;total+=payroll&quot; is missing in the while-loop. Each time you read payroll from the file you have to add it to the total.

good luck

Branko
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top