Thread: Program not reading in records

  1. #1
    Registered User
    Join Date
    Dec 2002

    Program not reading in records

    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; ("c:program7.dat", ios::in); 
    //verify that open was successful 
    if (! //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; 
    inFile >>item_code 
    //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') 
    if (item_type=='M') 
    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 
    //Output items 
    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<<"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<<" "; 
    }//end main 
    //function definition 
    void convert_type (char item_type) 
    else if (item_type=='E') 
    else if (item_type=='I') 
    else if (item_type=='L') 
    else if (item_type=='M') 
    float calc_cost_hour (float charge, float hours) 
    float cost_hour=(float)0.0; 
    if (hours!=(float)0.0) 
    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
    Registered User
    Join Date
    Nov 2002
    Try asking in the C++ question, since your code is C++.

    And next time please don't forget code tags.

  3. #3
    Casual Visitor
    Join Date
    Oct 2001
    That's a little too involved to work with. If memory serves me correctly you'd read in arrays using inFile.getline(array, size to read, deliminator)

    I'd work on the data too by putting it in a class or struct. I'm by no means an expert on the topic, but as a very basic example consider

    #include <iostream.h>
    #include <fstream.h>
    int main(void)
      struct data
        char code[20];
        char name[20];
        float labor;
      struct data mydata;
      ifstream inFile;
  "c:\\csource\\test.txt", ios::in);
        cout << "Unable to read file.";
        return -1;
      inFile.getline(mydata.code, 20, ' ');
      inFile.getline(, 20, ' ');
      inFile >> mydata.labor;
      cout << mydata.code << " "
           << << " "
           << mydata.labor << endl;
      return 0;
    The file

    Con-11-101 Architec 31899.35
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Visalia, CA, USA
    wtf? deja vu.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 12-19-2005, 02:01 PM
  2. ReadConsoleInput Reading Multiple Records
    By manofsteel972 in forum Windows Programming
    Replies: 1
    Last Post: 10-26-2004, 10:57 PM
  3. Getting the name of a file reading to program
    By DarkAbyss in forum C++ Programming
    Replies: 1
    Last Post: 03-22-2003, 09:14 AM
  4. Whats wrong with my file reading program?
    By Alphabird32 in forum C++ Programming
    Replies: 1
    Last Post: 09-08-2002, 03:19 AM
  5. Help on reading a disk from my program
    By civix in forum C++ Programming
    Replies: 2
    Last Post: 07-18-2002, 07:52 PM