Thread: reading in from files

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    20

    reading in from files

    im reading in from a txt file that looks like this:
    4 6.75 120 4444
    1 1200.00 1111
    3 10000.00 3333
    2 12.50 50 2222
    -1

    and bassically im trying to just read in one number at a time however im getting an infinite loop . im using a switch statement that is suppose to stop not at the end of file but when it comes to a -1
    anyways heres the code any hep would be much apreicated
    Code:
    #include <iostream>
    #include "pay.h"
    #include <iomanip>
    #include <string>
    #include <fstream>
    using namespace std;
    
    int main()
    {  //start declarations
    	pay p;
    	string empnum;
    	int week=0;
    	int hour=0;
    	int pie=0;
    	char paycode;
    	double hoursal;
    	double comtotal;
    	double pricepie;
    	string inloc;
    	string outloc;
    	ofstream output;
    	ifstream input;
    	// end declarations
    
    	output<<fixed<<showpoint<<setprecision(2); //sets everything to two decimal points
    	cout<<"Where is the input file"<<endl; // takes in the inputfile location
    	getline(cin,inloc);
    	input.open(inloc.c_str(), ios::out);
    	cout<<"Where would you like the output file to be saved"<<endl;// asks where to save the output file
    	getline(cin,outloc);
    	output.open(outloc.c_str(), ios::out);
    	output<<setw(10)<<"WEEKLY PAY REPORT FOR THE WIDGET COMPANY"<<endl; //the heading for the ouput file starts here
    	output<<endl;
    	output<<"EMPLOYEE"<<setw(5)<<"WEEKLY"<<setw(5)<<"HOURLY"<<setw(5)<<"HOURS"<<setw(5)<<
    		"GROSS"<<setw(5)<<"PRICE"<<setw(5)<<"NUMBER OF"<<endl;
    	output<<"NUMBER"<<setw(5)<<"PAY"<<setw(5)<<"SALARY"<<setw(5)<<"WORKED"<<setw(5)<<
    		"SALES"<<setw(5)<<"PER PIECE"<<setw(5)<<"PIECES"<<endl; //the heading for the output file ends here
    	do
    	{
    		
    		input>>paycode; // takes in the paycode from the file
    		switch(paycode) //all the computations done here calculating total payroll and the payment for each employee
    		{
    			case '1':input>>week;input>>empnum;p.weeklyPay(week);
    				output<<empnum<<setw(5)<<week;break;
    			case '2':input>>hoursal;input>>hour;input>>empnum;p.hourlyPay(hour,hoursal);
    				output<<empnum<<setw(5)<<p.hourlyPay(hour,hoursal)<<setw(5)<<hoursal<<setw(5)<<hour;break;
    			case '3':input>>comtotal;input>>empnum;p.commissionPay(comtotal);
    				output<<empnum<<setw(5)<<p.commissionPay(comtotal)<<setw(10)<<comtotal;break;
    			case '4':input>>pricepie;input>>pie;input>>empnum;p.piecePay(pricepie,pie);
    				output<<empnum<<setw(5)<<p.piecePay(pricepie,pie)<<setw(20)<<pricepie<<setw(5)<<pie;break;
    			case '-1':input.close();break; // close the input file when its -1
    		}
    	//	p.getPayRollAmount(); //running total of the payroll updates every round the loop runs
    	}while(paycode !='-1'); //condition for the loop to keep running
    
    	output<<endl; //the rest of the output file starts here
    	//output<<"Total payroll: $"<<p.getPayRollAmount()<<endl;
    	output<<"Total number of managers paid: "<<p.getWeekly()<<endl;
    	output<<"Total number of hourly workers paid: "<<p.getHourly()<<endl;
    	output<<"Total number of commission works paid: "<<p.getCommission()<<endl;
    	output<<"Total number of piece workers paid: "<<p.getPiece()<<endl;
    	output<<endl;
    	output<<"Programmer: Johnny Gaffey"; // the last of the outputfile ends here
    	output.close(); //close the output file
    	cout<<endl<<"Processing complete";
    	return 0;
    }//main
    this is what the output is suppose to look like:

    WEEKY PAY REPORT FOR THE WIDGET COMPANY

    EMPLOYEE WEEKLY HOURLY HOURS GROSS PRICE # OF
    NUMBER PAY SALARY WORKED SALES PER PIECE PIECES
    4444 810.00 6.75 120
    1111 1200.00
    3333 820.00 10000.00
    2222 687.50 12.50 50


    total payroll: $3517.50
    total number of managers paid: 1
    total number of hourly workers paid: 1
    total number of commission workers paid: 1
    total number of piece workers paid: 1

  2. #2
    Cat Lover
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109
    1. Well, first of all. When reading in from a file it's generally better practice to do something like while(in_file.good()), or while(s<<in_file), rather than looking for your own sentinel value.

    2. It's easier to read when you spread things out a bit more.

    3. A char can only hold a single character, so something like "-1" needs either a character array or string.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    char paycode;
    
    do
    {
        input>>paycode;
        switch(paycode)
        {
        case '1':
            input>>week;
            input>>empnum;
            p.weeklyPay(week);
            output<<empnum<<setw(5)<<week;
            break;
        case '2':
            input>>hoursal;
            input>>hour;
            input>>empnum;
            p.hourlyPay(hour,hoursal);
            output << empnum << setw(5) << p.hourlyPay(hour,hoursal)                                               
                   << setw(5) << hoursal << setw(5) << hour;
            break;
        case '3':
            input>>comtotal;
            input>>empnum;
            p.commissionPay(comtotal);
    	output << empnum << setw(5) << p.commissionPay(comtotal) 
                   << setw(10) << comtotal;
            break;
        case '4':
            input >> pricepie;
            input>>pie;
            input>>empnum;
            p.piecePay(pricepie,pie);
            output << empnum << setw(5) << p.piecePay(pricepie,pie) << setw(20)
                   << pricepie << setw(5) << pie;
            break;
        case '-1':
            input.close();
            break; // close the input file when its -1
    }
    //	p.getPayRollAmount(); //running total of the payroll updates every round the loop runs
    }while(paycode !='-1'); //condition for the loop to keep running
    You want to be reading numbers (i.e. int) and not char because paycode can't ever be equal to '-1' since '-1' is not a character. So, make paycode an int and get rid of all those single quotes in your switch statement.

    I also have no idea why you are calling some of those functions (in blue) twice with the same arguments.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    20
    thanks for the quick responses and as for the functions being called twice... the function returns a number andim calling it the 2nd time just to print that number

    also after trying that advice i changed paycode to an int and took off the ' ' but now this is as far as it gets:

    WEEKLY PAY REPORT FOR THE WIDGET COMPANY

    EMPLOYEEWEEKLYHOURLYHOURSGROSSPRICENUMBER OF
    NUMBER PAYSALARYWORKEDSALESPER PIECEPIECES

    also doesnt seem like the setw(5) is working any ideas?
    updated code:
    Code:
    #include <iostream>
    #include "pay.h"
    #include <iomanip>
    #include <string>
    #include <fstream>
    using namespace std;
    
    int main()
    {  //start declarations
    	pay p;
    	string empnum;
    	int week=0;
    	int hour=0;
    	int pie=0;
    	int paycode;
    	double hoursal;
    	double comtotal;
    	double pricepie;
    	string inloc;
    	string outloc;
    	ofstream output;
    	ifstream input;
    	// end declarations
    
    	output<<fixed<<showpoint<<setprecision(2); //sets everything to two decimal points
    	cout<<"Where is the input file"<<endl; // takes in the inputfile location
    	getline(cin,inloc);
    	input.open(inloc.c_str(), ios::out);
    	cout<<"Where would you like the output file to be saved"<<endl;// asks where to save the output file
    	getline(cin,outloc);
    	output.open(outloc.c_str(), ios::out);
    	output<<setw(10)<<"WEEKLY PAY REPORT FOR THE WIDGET COMPANY"<<endl; //the heading for the ouput file starts here
    	output<<endl;
    	output<<"EMPLOYEE"<<setw(5)<<"WEEKLY"<<setw(5)<<"HOURLY"<<setw(5)<<"HOURS"<<setw(5)<<
    		"GROSS"<<setw(5)<<"PRICE"<<setw(5)<<"NUMBER OF"<<endl;
    	output<<"NUMBER"<<setw(5)<<"PAY"<<setw(5)<<"SALARY"<<setw(5)<<"WORKED"<<setw(5)<<
    		"SALES"<<setw(5)<<"PER PIECE"<<setw(5)<<"PIECES"<<endl; //the heading for the output file ends here
    	do
    	{
    		
    		input>>paycode; // takes in the paycode from the file
    		switch(paycode) //all the computations done here calculating total payroll and the payment for each employee
    		{
    			case 1:input>>week;input>>empnum;p.weeklyPay(week);
    				output<<empnum<<setw(5)<<week;break;
    			case 2:input>>hoursal;input>>hour;input>>empnum;p.hourlyPay(hour,hoursal);
    				output<<empnum<<setw(5)<<p.hourlyPay(hour,hoursal)<<setw(5)<<hoursal<<setw(5)<<hour;break;
    			case 3:input>>comtotal;input>>empnum;p.commissionPay(comtotal);
    				output<<empnum<<setw(5)<<p.commissionPay(comtotal)<<setw(10)<<comtotal;break;
    			case 4:input>>pricepie;input>>pie;input>>empnum;p.piecePay(pricepie,pie);
    				output<<empnum<<setw(5)<<p.piecePay(pricepie,pie)<<setw(20)<<pricepie<<setw(5)<<pie;break;
    			case -1:input.close();break; // close the input file when its -1
    		}
    	//	p.getPayRollAmount(); //running total of the payroll updates every round the loop runs
    	}while(paycode !='-1'); //condition for the loop to keep running
    
    	output<<endl; //the rest of the output file starts here
    	//output<<"Total payroll: $"<<p.getPayRollAmount()<<endl;
    	output<<"Total number of managers paid: "<<p.getWeekly()<<endl;
    	output<<"Total number of hourly workers paid: "<<p.getHourly()<<endl;
    	output<<"Total number of commission works paid: "<<p.getCommission()<<endl;
    	output<<"Total number of piece workers paid: "<<p.getPiece()<<endl;
    	output<<endl;
    	output<<"Programmer: Johnny Gaffey"; // the last of the outputfile ends here
    	output.close(); //close the output file
    	cout<<endl<<"Processing complete";
    	return 0;
    }//main
    Last edited by johnnyg; 02-19-2006 at 12:01 AM.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    as for the functions being called twice... the function returns a number andim calling it the 2nd time just to print that number
    The question is: why are you calling the function the 1st time?

    also doesnt seem like the setw(5) is working any ideas?
    If you have a heading like "HOWLONGDOYOUTHINKTHISHEADINGIS", and you use setwidth(5), what are you trying to accomplish?

    Code:
    input.open(inloc.c_str(), ios::out);
    Do you know what ios::out means?

    You need to learn how to format a switch statement. Look in your book or look up a tutorial. Your code is a mess.

    Since you haven't posted your class, as far as anyone knows, no data is saved in your member variables.
    Last edited by 7stud; 02-19-2006 at 12:46 AM.

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    20
    calling the function the first time updates the total number of employees of that kind and calculates the weekly salary

    the setw(5) was suppose to creat a space inbetween the text

    yea i know what iosut meansi was copy and pasting code from my last project and forgot to take out that part from the in part.

    as for the switch sorry its so messy i put some spacing in to make it more readable

    would it help if i post my class?

    update: the output isnt reading the employee number 1111 for some reason and no spacing in the output... setw() doesnt seem to be working for me maybe i did something wrong

    and at the bottem where it says how many of each kind of employee i have managers is 1 like it should be but the others are:
    Total number of managers paid: 1
    Total number of hourly workers paid: 4244878
    Total number of commission works paid: -479997245
    Total number of piece workers paid: 3359306

    here is the updated code:
    Code:
    #include <iostream>
    #include "pay.h"
    #include <iomanip>
    #include <string>
    #include <fstream>
    using namespace std;
    
    int main()
    {  //start declarations
    	pay p;
    	string empnum;
    	int week=0;
    	int hour=0;
    	int pie=0;
    	int paycode;
    	double hoursal;
    	double comtotal;
    	double pricepie;
    	string inloc;
    	string outloc;
    	ofstream output;
    	ifstream input;
    	// end declarations
    
    	output<<fixed<<showpoint<<setprecision(2); //sets everything to two decimal points
    	cout<<"Where is the input file"<<endl; // takes in the inputfile location
    	getline(cin,inloc);
    	input.open(inloc.c_str());
    	cout<<"Where would you like the output file to be saved"<<endl;// asks where to save the output file
    	getline(cin,outloc);
    	output.open(outloc.c_str(), ios::out);
    	//the heading for the ouput file starts here
    	output<<"                     WEEKLY PAY REPORT FOR THE WIDGET COMPANY"<<endl; 
    	output<<"EMPLOYEE     WEEKLY     HOURLY     HOURS   "<<
    		"  GROSS     PRICE          NUMBER OF"<<endl;
    	output<<"NUMBER       PAY        SALARY     WORKED"<<
    		"    SALES     PER PIECE      PIECES"<<endl; //the heading for the output file ends here
    	do
    	{
    		
    		input>>paycode; // takes in the paycode from the file
    		switch(paycode) //all the computations done here calculating total payroll and the payment for each employee
    		{
    			case 1 :input>>week;input>>empnum;p.weeklyPay(week);
    				output<<empnum<<setw(5)<<week<<endl;;break;
    
    			case 2 :input>>hoursal;input>>hour;input>>empnum;p.hourlyPay(hour,hoursal);
    				output<<empnum<<setw(5)<<p.hourlyPay(hour,hoursal)<<setw(5)<<hoursal<<setw(5)<<hour<<endl;;break;
    
    			case 3 :input>>comtotal;input>>empnum;p.commissionPay(comtotal);
    				output<<empnum<<setw(5)<<p.commissionPay(comtotal)<<setw(10)<<comtotal<<endl;break;
    
    			case 4 :input>>pricepie;input>>pie;input>>empnum;p.piecePay(pricepie,pie);
    				output<<empnum<<setw(5)<<p.piecePay(pricepie,pie)<<setw(20)<<pricepie<<setw(5)<<pie<<endl;break;
    
    			case -1 :input.close();break; // close the input file when its -1
    		} 
    	//	p.getPayRollAmount(); //running total of the payroll updates every round the loop runs
    	}while(paycode !=-1); //condition for the loop to keep running
    
    	output<<endl; //the rest of the output file starts here
    	//output<<"Total payroll: $"<<p.getPayRollAmount()<<endl;
    	output<<"Total number of managers paid: "<<p.getWeekly()<<endl;
    	output<<"Total number of hourly workers paid: "<<p.getHourly()<<endl;
    	output<<"Total number of commission works paid: "<<p.getCommission()<<endl;
    	output<<"Total number of piece workers paid: "<<p.getPiece()<<endl;
    	output<<endl;
    	output<<"Programmer: Johnny Gaffey"; // the last of the outputfile ends here
    	output.close(); //close the output file
    	cout<<endl<<"Processing complete"<<endl;
    	return 0;
    }//main

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    20
    project is finished thanks for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading User Defined Files
    By Necrofear in forum C++ Programming
    Replies: 17
    Last Post: 06-30-2006, 12:55 AM
  2. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  3. reading files
    By hiya in forum C++ Programming
    Replies: 7
    Last Post: 05-21-2005, 11:40 AM
  4. A little help reading from files...
    By Invincible in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2002, 10:43 AM
  5. Need Advice in reading files
    By jon in forum C Programming
    Replies: 4
    Last Post: 10-07-2001, 07:27 AM