program not reading in records from file

This is a discussion on program not reading in records from file within the C++ Programming forums, part of the General Programming Boards category; Please help, I working on a program that is suppose to read data from a file. For some reason the ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    5

    program not reading in records from file

    Please help, I working on a program that is suppose to read data from a file. For some reason the program isn't working. When I run it, I don't receive an error message, nor do I see the information that its suppose to read in. Can someone please tell me what I'm doing wrong. Thanks in advance.

    Here is a copy of my program:

    #include <iostream.h>
    #include <fstream.h>
    #include <iomanip.h>
    #include <string.h>
    #include <stdlib.h>

    //function prototype

    void convert_type(char);
    float calc_cost_hour(float, float);

    void main()

    {
    char item_code[10]="";
    char item_name[20]="";
    char item_type=' ';
    float hours=(float)0.0;
    float charge=(float)0.0;
    int days=0;
    float total_chrg=(float)0.0;
    float total_hours=(float)0.0;
    int counter=0;
    char type_charge[7]="";
    float cost_hour=(float)0.0;
    float labor=(float)0.0;
    float materials=(float)0.0;
    float highest_charge=(float)0.0;
    char itemCode[11]="";
    int number_day=0;


    //open input file

    ifstream inFile;
    inFile.open ("crogram7.dat", ios::in);


    //verify that open was successful

    if (!inFile.fail()) //if open did not fail
    {
    //read data from prg7.dat file

    cout<<"#"<<setw(6)<<"Type"<<setw(15)<<"Contr.Code" <<setw(13)<<"Item Name"<<setw(14)<<"Cost/Hour"<<setw(12)<<"Item Cost"<<endl;
    cout<<endl;

    cout<<setiosflags(ios::fixed);
    cout<<setiosflags(ios::showpoint);
    cout<<setprecision(2);


    inFile >>item_code
    >>item_name
    >>item_type
    >>hours
    >>charge
    >>days;

    //test for the end of the file

    while (!inFile.eof())
    {


    //function call

    convert_type (item_type);
    cost_hour=calc_cost_hour (charge, hours);


    //update counter and accumulator

    counter = counter + 1;
    total_chrg = total_chrg + charge;
    total_hours = total_hours + hours;

    if (item_type=='L')
    labor=labor+charge;

    if (item_type=='M')
    materials=materials+charge;


    cout<< counter
    <<setw(5) <<item_code
    <<setw(4) <<item_name
    <<setw(5) <<cost_hour
    <<setw(9) <<charge<<endl;

    if (charge > highest_charge)
    {
    strcpy(itemCode, item_code);
    number_day = days;
    highest_charge = charge;;
    }







    }//end while loop

    //close file
    inFile.close();

    system("pause");
    system("cls");

    //Output items

    cout<<"-----------------------------------------------------------------"<<endl;
    cout<<"The number of records in the database is: "<<counter<<endl;
    cout<<"Cost of all materials: "<<setw(15)<<materials<<endl;
    cout<<"Total manhours charged: "<<setw(12)<<hours<<endl;
    cout<<"Total labor cost: "<<setw(5)<<labor<<endl;
    cout<<"The total cost of all charges: "<<setw(23)<<total_chrg<<endl;
    cout<<endl;
    cout<<"The contractor code that had the greatest expense was "<<itemCode<<" "<<endl;
    cout<<"It was completed on day "<<number_day<<" of the job at a cost of $ "<<highest_charge<<" ";
    cout<<endl;
    cout<<endl;

    }


    }//end main

    //function definition

    void convert_type (char item_type)

    {
    if(item_type=='C')
    cout<<"CONTR"<<setw(2);

    else if (item_type=='E')
    cout<<"ELECT";

    else if (item_type=='I')
    cout<<"INSPC";

    else if (item_type=='L')
    cout<<"LABOR";

    else if (item_type=='M')
    cout<<"MATLS";
    }

    float calc_cost_hour (float charge, float hours)

    {
    float cost_hour=(float)0.0;

    if (hours!=(float)0.0)
    cost_hour=charge/hours;

    return 0;
    }

    Here is a copy of the data file:
    Con-11-101 Architec C 0 31899.35 1
    CEM-01-001 Concrete M 0 315090.00 3
    CEM-22-086 Cem-labor L 16800 802200.00 71
    CEM-01-111 Cem-inst I 0 11435.00 77
    ELC-17-488 Elc-wirg E 0 79995.00 5
    ELC-22-086 Elc-labor L 6256 447304.00 63
    ELI-01-001 Elc-inst I 0 7399.00 66
    PLB-03-225 Plb-pipe M 0 258000.00 18
    PLB-03-801 Plb-labor L 13536 554976.00 80
    PBI-01-001 Plb-inst I 0 6089.00 88
    CON-01-388 Transpor C 0 34517.50 277
    EXC-05-999 Excavate L 1080 28890.00 21
    LAN-18-189 Sod-field M 0 37895.00 101
    LAN-19-190 Sod-entr M 0 18000.00 137
    LAN-18-225 Sod-inst L 1188 17820.00 122
    CEM-79-220 Concrete M 0 175000.00 119
    Con-11-133 Seat-h/w M 0 299250.00 133
    LAN-19-257 Sod-inst L 360 7740.00 260
    Con-11-305 Seat-inst L 22500 781875.00 311
    CEM-87-233 Concrete M 0 88795.00 321
    Con-11-293 Booth-etc L 5760 506880.00 333

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I'm not going to lie and say i fixed this but I did smooth out some rough edges. You can do the rest:

    Code:
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <cstring>
    #include <cstdlib>
    
    //function prototype
    using namespace std;
    
    void convert_type(char);
    float calc_cost_hour(float, float);
    
    int main() {
    	char item_code[10]="";
    	char item_name[20]="";
    	char item_type=' ';
    	float hours=(float)0.0;
    	float charge=(float)0.0;
    	int days=0;
    	float total_chrg=(float)0.0;
    	float total_hours=(float)0.0;
    	int counter=0;
    	char type_charge[7]="";
    	float cost_hour=(float)0.0;
    	float labor=(float)0.0;
    	float materials=(float)0.0;
    	float highest_charge=(float)0.0;
    	char itemCode[11]="";
    	int number_day=0;
    
    
    	//open input file
    
    	ifstream inFile;
    	inFile.open ("c:/program7.dat", ios::in);
    
    
    	//verify that open was successful
    
    	if (!inFile.fail()) //if open did not fail
    	{
    		//read data from prg7.dat file
    
    		cout<<"#"<<setw(6)<<"Type"<<setw(15)<<"Contr.Code"<<setw(13)<<"Item Name"<<setw(14)<<"Cost/Hour"<<setw(12)<<"Item Cost"<<endl;
    		cout<<endl;
    
    		cout<<setiosflags(ios::fixed);
    		cout<<setiosflags(ios::showpoint);
    		cout<<setprecision(2);
    
    
    		inFile >>item_code
    		>>item_name
    		>>item_type
    		>>hours
    		>>charge
    		>>days;
    
    		//test for the end of the file
    
    		while (!inFile.eof()) {
    			//function call
    
    			convert_type (item_type);
    			cost_hour=calc_cost_hour (charge, hours);
    
    
    			//update counter and accumulator
    
    			counter = counter + 1;
    			total_chrg = total_chrg + charge;
    			total_hours = total_hours + hours;
    
    			if (item_type=='L')
    			labor=labor+charge;
    
    			if (item_type=='M')
    			materials=materials+charge;
    
    
    			cout<< counter
    			<<setw(5) <<item_code
    			<<setw(4) <<item_name
    			<<setw(5) <<cost_hour
    			<<setw(9) <<charge<<endl;
    
    			if (charge > highest_charge) {
    				strcpy(itemCode, item_code);
    				number_day = days;
    				highest_charge = charge;
    			}
    
    
    		}//end while loop
    
    		//close file
    		inFile.close();
    
    		system("pause");
    		system("cls");
    
    		//Output items
    
    		cout<<"-----------------------------------------------------------------"<<endl;
    		cout<<"The number of records in the database is: "<<counter<<endl;
    		cout<<"Cost of all materials: "<<setw(15)<<materials<<endl;
    		cout<<"Total manhours charged: "<<setw(12)<<hours<<endl;
    		cout<<"Total labor cost: "<<setw(5)<<labor<<endl;
    		cout<<"The total cost of all charges: "<<setw(23)<<total_chrg<<endl;
    		cout<<endl;
    		cout<<"The contractor code that had the greatest expense was "<<itemCode<<" "<<endl;
    		cout<<"It was completed on day "<<number_day<<" of the job at a cost of $ "<<highest_charge<<" ";
    		cout<<endl;
    		cout<<endl;
    
    	}
    
    
    }//end main
    
    //function definition
    void convert_type (char item_type) {
    	if(item_type=='C')
    	cout<<"CONTR"<<setw(2);
    
    	else if (item_type=='E')
    	cout<<"ELECT";
    
    	else if (item_type=='I')
    	cout<<"INSPC";
    
    	else if (item_type=='L')
    	cout<<"LABOR";
    
    	else if (item_type=='M')
    	cout<<"MATLS";
    }
    
    float calc_cost_hour (float charge, float hours) {
    	float cost_hour=(float)0.0;
    
    	if (hours!=(float)0.0)
    	cost_hour=charge/hours;
    
    	return 0;
    }
    One big one was void main. Don't do that anymore.
    Last edited by master5001; 12-13-2002 at 01:41 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help reading a file (isbn program)
    By bigmac(rexdale) in forum C Programming
    Replies: 2
    Last Post: 05-16-2008, 10:56 PM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21