Need to repeat from an input file

I'm very new to programming and this is my first programming course in college. Here's what I have:

#include <iostream>
using namespace std;


int main()
{

string employeeid;
int hoursworked;
float hourlyrate,grosspay,taxamount,netpay;
float TAXRATE = 0.10;
int counter = 0;
int n;

cout<<"Please enter the number of employees:";
cin>>n;


while(counter < n)
{
cout<<"ENTER THE EMPLOYEE ID:";
cin>>employeeid;
cout<<"ENTER THE HOURS WORKED:";
cin>>hoursworked;
cout<<"ENTER THE HOURLY RATE:";
cin>>hourlyrate;
grosspay=hoursworked*hourlyrate;
taxamount=grosspay*TAXRATE;
netpay=grosspay-taxamount;
cout<<"THE HOURS WORKED ARE "<<hoursworked<<"\n";
cout<<"THE GROSSPAY IS "<<grosspay<<"\n";
cout<<"THE TAX AMOUNT IS "<<taxamount<<"\n";
cout<<"THE NETPAY IS "<<netpay<<"\n";
++counter;
}//WHILE
cin.ignore();
cin.get();
return 0;
}//MAIN

How do I expand this so it repeats from an input file? I read the chapters in the book and still a little confused. The hints I have is this:

3B.) Expand Payroll Program so that repeats for 5 employees from an input file.
Data typed and saved under employee.in
#include<fstream.h> instead of <iostream.h>
Use ifstream fin("employee.in");
Use the same while loop
Last edited on
Umm, first: Please use code tags
like this
second: your question is quite general, this isn't a homework service, a little specificity is always best here
And third: an ifstream is a file you can read input from, it works EXACTLY like cin, so if you use the hints carefully (open the file with the hint they gave you, and then use what I just told you), you should be fine!
Does this help?
You've got pretty much everything you should need within these hints!
Surround the program in a for loop to get your 5 repeats (or a do/while loop with separate variable) and use your file-stream the same as console-stream.

1
2
#include <fstream>
#include <string> 

remember to include above headers!
1
2
3
string str;
ifstream fin("employee.in");
getline(fin, str);

String str now equals the first line of file emplyee.in, if you need to get whole file or get all lines separate there are ways but i'll leave you to figure out how.
Last edited on
Ok as I said I've read the chapters but taking an online course I really don't get any in class examples so I'm on my own but I'm gonna give it a shot thank you
Figured it out from the advice you guys gave me. Thank you
Great, now you have your problem sorted i'll give you a little advice for reading text document.
Firstly go read the tutorials on this website for Fstreaming, the one thing i didn't quite use to my advantage on multiple occasions is that files can be read in a similar way to strings.

You're able to get the length using the seekg() function and using the end() function as the argument. Then you can use tellg(int) to retrieve the position of the pointer in the file.
Also because the program points to the contents of the file to read it, so when you use getline() this gets the information but leaves the pointer at the end of the line, simply use getline() again for the next line (you may need to add 1 to position, not sure)

Anyways, just a couple of handy tricks that might hopefully help anyone in the future! Goodluck with C++
Thank you very much
Topic archived. No new replies allowed.