Thread: Help! What's wrong with my code? How do I read data from a file?

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    2

    Question Help! What's wrong with my code? How do I read data from a file?

    This file is like.

    John Doe 789-89-7897 7.25 45 F
    Jane Smith 456-56-4564 8.20 30 P
    Luke Warm 123-23-1231 5.26 48 F

    I need to read from this file and then output a report with columns and rows.

    This is my code so far, but somethings wrong, it keeps crashing.

    Code:
    # include <iostream>
    # include <fstream>
    # include <iomanip>
    # include <string>
    
    using namespace std;
    
    int main ()
    {
    
    	const int LENGTH=10;
    
    	int num_records=0;
    	char FName[LENGTH];
    	char LName[LENGTH];
    	char ssNum[LENGTH];
    	float hrWage[LENGTH];
    	float weekHrs[LENGTH];
    	char empStatus[LENGTH];
    
    	float timePay=0;
    	float overHrs=0;
    	float overPay=0;
    	float overTime=0;
    	string statDisplay;
    	
    	float netPay=0;
    	int unionFee=5;
    
    cout<<"First Name     Last Name     SSN           Wage    #Hrs  Reg.Pay  OT Pay  Status     NetPay     "<<endl;
    
    
    	//open file
    	ifstream data_file ("F:\\2011 Spring\\C++\\Data Files\\Ch11 Exercise 1\\DataFile.txt");
    
    	//check for an open file
    	if (data_file.is_open())
    	{
    		data_file>>FName[num_records]
    				>>LName [num_records]
    				>>ssNum [num_records]
    				>>hrWage [num_records]
    				>>weekHrs [num_records]
    				>>empStatus [num_records];
    
    		while(!data_file.eof())
    		{
    			num_records++;
    
    			data_file>>FName[num_records]
    				>>LName [num_records]
    				>>ssNum [num_records]
    				>>hrWage [num_records]
    				>>weekHrs [num_records]
    				>>empStatus [num_records];
    
    				timePay=hrWage[num_records]*weekHrs[num_records];
    
    				if (weekHrs[num_records]>40)
    				{
    					overHrs=weekHrs[num_records]-40;
    					overPay=hrWage[num_records]*1.5;
    					overTime=overHrs*overPay;
    				}
    				else
    				{
    					overTime=0;
    				}
    
    				if(empStatus[num_records]=='F')
    				{
    					statDisplay = "Fulltime" ;
    				}
    				else
    				{
    					statDisplay = "Parttime";
    				}
    
    				netPay=(timePay+overTime)-unionFee;
    
    				
    				cout<<left<<setw(15)<<FName;
    				cout<<left<<setw(14)<<LName;
    				cout<<left<<setw(16)<<ssNum;
    				cout<<left<<setw(9)<<hrWage;
    				cout<<left<<setw(6)<<weekHrs;
    				cout<<left<<setw(9)<<timePay;
    				cout<<left<<setw(8)<<overTime;
    				cout<<left<<setw(11)<<statDisplay;
    				cout<<left<<setw(11)<<netPay<<endl;
    
    		}
    		data_file.close();
    	}
    	else
    	{
    		cout<<"Error: File is unable to open."<<endl;
    	}
    	 return(0);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Use your debugger to help you determine the point of the crash. This gives you a reference as to where to start looking. (The actual mistake could have happened well before the crash, but at least you can rule out what comes after the crash for now.)

    One problem I see is that FName, LName and ssNum should be arrays of std::string objects, not char. Also, do not use eof() to control a loop like that. Rather, write:
    Code:
    while (data_file >> FName[num_records]
                     >> LName[num_records]
                     >> ssNum[num_records]
                     >> hrWage[num_records]
                     >> weekHrs[num_records]
                     >> empStatus[num_records])
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    2
    Thank you so much. That helped alot. there was a few other problems with it too, but I was able to figure it out once I did your suggestion.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 21
    Last Post: 11-03-2007, 02:56 PM
  2. how can i read data from file... plz help me
    By fnfn in forum C++ Programming
    Replies: 2
    Last Post: 05-22-2007, 06:39 AM
  3. Replies: 4
    Last Post: 12-07-2002, 04:24 PM
  4. how do i read data from a file?
    By ssjnamek in forum C++ Programming
    Replies: 19
    Last Post: 02-01-2002, 04:58 PM
  5. please read this source code and tell me what is wrong
    By goof in forum Windows Programming
    Replies: 7
    Last Post: 12-01-2001, 11:49 PM

Tags for this Thread