Infile.open

I am new as you can tell , and lost..We are doing input text file and read the file

For example:(Bobfile.txt)
Joe Will 255-68-9842 45 10.70
Output
9842 J.Will 45 10.70 508.25 50.82 457.42

Can somebody point me in the right direction?
Right now I am just trying to get a output

cout<<ssn.substr(7)<<"\t"<<fname.substr(0,1)<< endl;


string fname="",lname="";
double ssn=0.0,hourw=0.0,rate=0.0;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <fstream>
using namespace std;
int main()
{
	cout<<fixed<<setprecision(2);
	ifstream infile;
	infile.open("Bobfile.txt");


   // Declare Varibles
	int x = 0,count=0,sum=0;
	string fname="";
	string lname="";
	string	 ssn="";
	string	name="";
	double  hours=0.0,rate=0.0,gross=0.0,Deductions=0.0,Netpay=0.0,overtime=0.0,rateo=1.5;
    char ans           ='y';
	string revfullname =" "; 	
				
		infile>>fname; lname; ssn; hours; rate;
        cout <<"SSN\tName\t\tHours\tRate\tGross\tDeductions\tNetpay";
		cout <<"\n____\t_______\t\t_____\t____\t_____\t__________\t_______"; 
        
		while (infile.eof()== false)		
		 
		if (hours>40)
				overtime = hours-40;
		       hours = 40;
				gross =(hours*rate)+(overtime*(rate*rateo));    
       

		cout<<ssn.substr(7)<<"\t"<<fname.substr(0,1)<< endl;//<<"."<<lname<<"\t"<<Hours<<"\t"<<Rate<<"\t"<<Gross<<"\t"<<Deductions<<"\t"<<Netpay;
		  
	  cout<<"\n\nWould you like to try another set of phone and ssn?:";//just a code for later use
		cin>> ans;
	while (ans == 'y'||ans == 'Y');
		
   cout<<"\n\t\t\tT H A N K  Y O U "<<endl;
	  system("pause");
	  return 0;
	 }     // end of main function
	  
If I understand you correctly, I think you want line 24 to be this instead:

infile>>fname >> lname >> ssn >> hours >> rate;

Let me know how it works out.
Thank you Bool maybe -- Will that solve my output issue
Lets see if we can line these babies up...
Almost there , how do you get to read the other 19 lines
Gross , deduct , and netpay how get them to read out ?
This code seems to work fine for me:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <fstream>
using namespace std;
int main()
{
	cout<<fixed<<setprecision(2);
	ifstream infile;
	infile.open("Bobfile.txt");


   // Declare Varibles
	int x = 0,count=0,sum=0;
	string fname="";
	string lname="";
	string	 ssn="";
	string	name="";
	double  hours=0.0,rate=0.0,gross=0.0,Deductions=0.0,Netpay=0.0,overtime=0.0,rateo=1.5;
    char ans           ='y';
	string revfullname =" "; 	
				
		infile>>fname >> lname >> ssn >> hours >> rate;
        cout <<"SSN\tName\t\tHours\tRate\tGross\tDeductions\tNetpay";
		cout <<"\n____\t_______\t\t_____\t____\t_____\t__________\t_______"; 
        
		while (infile.eof()== false)
		 
		if (hours>40)
				overtime = hours-40;
		       hours = 40;
				gross =(hours*rate)+(overtime*(rate*rateo));
       

		cout<<endl <<ssn.substr(7)<<"\t"<<fname.substr(0,1)<<"."<<lname<<"\t"<<hours<<"\t"<<rate<<"\t"<<gross<<"\t"<<Deductions<<"\t"<<Netpay;
		  
	  cout<<"\n\nWould you like to try another set of phone and ssn?:";//just a code for later use
		cin>> ans;
	while (ans == 'y'||ans == 'Y');
		
   cout<<"\n\t\t\tT H A N K  Y O U "<<endl;
	  system("pause");
	  return 0;
	 }     // end of main function 


some notes of interest:

*The variables in C++ are case sensitive so using "Hours" in line 40 like you had it originally would be incorrect -- the correct form is "hours." If you're lucky enough to be programming on MS Visual then Visual will put a red squiggly line under such variables to tell you that they're not defined.

*"Netpay" is not calculated. It's just initialized to zero which I'm pretty sure is not what you want

*line 32 isn't doing anything. I'm not sure what you wanted to do with that. in addition instead of using:

infile.eof()== false

to test that the file hasn't ended you might want to use

!infile.eof()

They will both do the same thing but the latter takes up less space. Likewise if you want to test:

infile.eof()== true

you should test:

infile.eof()

instead.
The numbers don't line up but that's a trivial detail.
I have almost there except two issues :
one Gross and dect Netpay are off
two How can I line them up,If the name short it goes out line
How do I count the records?

Thank so much for you help



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <fstream>
using namespace std;
int main()
{
	cout<<fixed<<setprecision(2);
	ifstream infile;
	infile.open("Bobfile.txt");


   // Declare Varibles
	int x = 0,count=0,sum=0,hours=0;
	string fname="";
	string lname="";
	string	 ssn="";
	string	name="";
	double  rate=0.0,gross=0.0,deductions=0.0,netpay=0.0,overtime=0.0,rateo=1.5;
    char ans           ='y';
	string revfullname =" "; 	
				
		infile>>fname>>lname>>ssn>>hours>>rate;
        cout <<"SSN\tName\t\tHours\tRate\tGross\tDeductions\tNetpay";
		cout <<"\n____\t_________\t_____\t____\t_____\t_________\t_______"; 
           while (infile.eof()== false)    
		   { if (hours>40)
				overtime = hours-40;
		        gross =(hours-overtime)*rate+(overtime*(rate*rateo));
				deductions = gross * .10;
				netpay = gross - deductions;
				
		cout<<"\n"<<ssn.substr(7)<<"\t"<<fname.substr(0,1)<<"."<<lname<<"\t"<<hours<<"\t"<<rate<<"\t"<<gross<<"\t"<<deductions<<"\t\t"<<netpay<<endl;
				
		
		infile>>fname>>lname>>ssn>>hours>>rate;
		   }

	  cout<<"\n\nWould you like to try another set of phone and ssn?:";
		cin>> ans;
	while (ans == 'y'||ans == 'Y');
		
   cout<<"\n\t\t\tT H A N K  Y O U "<<endl;
	  system("pause");
	  return 0;
	 }     // end of main function
	  
I think this is what you want but I didn't check to make sure that the outputs made sense or not:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <fstream>
using namespace std;
int main()
{
	cout<<fixed<<setprecision(2);
	ifstream infile;
	infile.open("Bobfile.txt");


   // Declare Varibles
	int x = 0,count=0,sum=0;
	string fname="";
	string lname="";
	string	 ssn="";
	string	name="";
	double  hours=0.0,rate=0.0,gross=0.0,Deductions=0.0,Netpay=0.0,overtime=0.0,rateo=1.5;
    char ans           ='y';
	string revfullname =" "; 	
				
		infile>>fname >> lname >> ssn >> hours >> rate;
        cout <<"SSN\tName\t\tHours\tRate\tGross\tDeductions\tNetpay";
		cout <<"\n____\t_______\t\t_____\t____\t_____\t__________\t_______"; 
        
		while (infile.eof()== false)
		 
		if (hours>40)
		{
				overtime = hours-40;
		       hours = 40;
		}
				gross =(hours*rate)+(overtime*(rate*rateo));
				Deductions = gross * .10;
				Netpay = gross - Deductions;
       

		cout<<endl <<ssn.substr(7)<<"\t"<<fname.substr(0,1)<<"."<<lname<<"\t"<<hours<<"\t"<<rate<<"\t"<<gross<<"\t"<<Deductions<<"\t"<<Netpay;
		  
	  cout<<"\n\nWould you like to try another set of phone and ssn?:";//just a code for later use
		cin>> ans;
	while (ans == 'y'||ans == 'Y');
		
   cout<<"\n\t\t\tT H A N K  Y O U "<<endl;
	  system("pause");
	  return 0;
	 }     // end of main function  


All the changes I did from the code I posted previously are in lines 34-41. I think your new expression for "gross" is wrong. I think you had it correct the first time.
overtime=0; inserted this at line 39 bring overtime back to zero
However still having issues with overtime an less than 40 hours

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <fstream>
using namespace std;
int main()
{
	cout<<fixed<<setprecision(2);
	ifstream infile;
	infile.open("Bobfile.txt");


   // Declare Varibles
	int x = 0,count=0,sum=0,hours=0;
	string fname="";
	string lname="";
	string	 ssn="";
	string	name="";
	double  rate=0.0,gross=0.0,deductions=0.0,netpay=0.0,overtime=0.0,rateo=1.5;
    char ans           ='y';
	string revfullname =" "; 	
				
		infile>>fname>>lname>>ssn>>hours>>rate;
        cout <<"SSN\tName\t\tHours\tRate\tGross\tDeductions\tNetpay";
		cout <<"\n____\t_________\t_____\t____\t_____\t_________\t_______"; 
           while (infile.eof()== false)
			   
		   { if (hours>40)
                overtime=0;
		        overtime = hours-40;
		        gross =(hours-overtime)*rate+(overtime*(rate*rateo));
				deductions = gross * .10;
				netpay = gross - deductions;
				
		cout<<"\n"<<ssn.substr(7)<<"\t"<<fname.substr(0,1)<<"."<<lname<<"\t"<<hours<<"\t"<<rate<<"\t"<<gross<<"\t"<<deductions<<"\t\t"<<netpay<<endl;
				
		
		infile>>fname>>lname>>ssn>>hours>>rate;
		   }

	  cout<<"\n\nWould you like to try another set of phone and ssn?:";
		cin>> ans;
	while (ans == 'y'||ans == 'Y');
		
   cout<<"\n\t\t\tT H A N K  Y O U "<<endl;
	  system("pause");
	  return 0;
	 }     // end of main function
	  
My gross keeps creeping up , 1st 5 lines are OK , then it starts going high
One how do will clear overtime or set it to 0 for next cal
Second How do I align the numbers up?
How do we count the records?
sample of textfile:
Dan Steffee 402-16-9923 45 10.70
Taylor Swift 444-13-8888 40 20.50
Chris Burrell 254-56-8999 48 17.50
Renee Lily 655-45-5555 46 18.00
Shaun Harvey 322-56-4566 50 21.00
Nicole Kidman 255-65-8898 25 12.00
Stacy Hensley 899-65-2264 38 15.00
Britney Spears 759-25-8977 28 14.50
Brad Pitt 765-45-8659 29 13.50
Jodie Foster 321-65-4565 45 13.50
Jim Jones 138-54-4684 25 12.54
Lovett Timothy 582-39-8559 37 12.50
Tom Hank 987-52-6544 50 13.54
Johnson McAdams 354-44-8888 46 21.38
Jim Weaver 123-45-6789 57 17.80
John Thomas 981-76-5432 56 12.50
Pete Gibson 897-46-5145 46 17.46
Hank Smith 546-87-2418 32 14.22
Sherry Jones 789-65-1243 28 13.75
Sharon Stone 795-82-4613 12 20.00



1
2
3
4
5
6
7
8
9
10
11
12
13
while (infile.eof()== false)
			   
		   {if (hours>40)
                 overtime = hours-40;
		        gross =(hours-overtime)*rate+(overtime*(rate*rateo));
				deductions = gross * .10;
				netpay = gross - deductions;
				
		cout<<"\n"<<ssn.substr(7)<<"\t"<<fname.substr(0,1)<<"."<<lname<<"\t"<<hours<<"\t"<<rate<<"\t"<<gross<<"\t"<<deductions<<"\t\t"<<netpay<<endl;
				
		
		infile>>fname >>lname >>ssn >>hours >>rate;
		   }
Topic archived. No new replies allowed.