I'm supposed to just read a file and display certain information. I wrote the file in notepad and have it in the correct folder for my project, but when it opens (it DOES open), it doesn't display anything. Ideas?

#include <iostream>
#include <fstream>

using std::cout;
using std::cin;
using std::ifstream;

int ReadData ( int ssn[], int wage[], int hours[], char status[] );

int main ()
    int ssn[11];
    int wage[11];
    int hours[11];
    char status[11];

    ReadData ( ssn, wage, hours, status );

    return 0;

int ReadData ( int ssn[], int wage[], int hours[], char status[] )
    int num_records = 0;

    //open file
    ifstream data_file( "PE11_1.txt" );

    //check to see if file is open
    if ( data_file.is_open() )
        //read until end of file
        while ( !data_file.eof() )

            data_file >> ssn [num_records]
                      >> wage [num_records]
                      >> hours [num_records]
                      >> status [num_records];



        cout << "Error. Unable to open data file." <<'\n';

    return num_records;


Recommended Answers

All 4 Replies

Show your data file please. Also, you are reading the data, but never outputting it.

The following is what is contained in my file:

John Smith 123-09-8765 9.00 46 F
Molly Brown 432-89-7654 9.50 40 F
Tim Wheeler 239-34-3458 11.25 83 F
Keil Wader 762-84-6543 6.50 35 P
Trish Dish 798-65-9844 7.52 40 P
Anthony Lei 934-43-9844 9.50 56 F
Kevin Ashes 765-94-7343 4.50 30 P
Cheryl Prince 983-54-9000 4.65 45 F
Kim Cares 343-11-2222 10.00 52 F
Dave Cockroach 356-98-1236 5.75 48 F
Will Kusick 232-45-2322 15.00 45 P

First, you're not reading in the name, you go straight to SSN. So input will hang up right there, seeing characters that don't match int type.

Second, once you get past the name, SSN with dashes won't go into the int field, either. Nor will the hours, which have a decimal in them, indicating that array should be type double.

As to your reading loop, that one is going to give you poor control. Testing eof( ) before ever attempting to read is meaningless. You don't know if there's anything in the file or not, and you won't know till an attempt to read has occurred. Consider if the data file existed but was empty. eof( ) returns false, so you go into the loop body, increment the record count, try to read into the arrays (getting nothing). The next test of eof( ) now gives false, ending the loop. Your record count is 1, but there really wasn't any data.

A better model (you'll need to adapt this to your data format) is something like:

while( data_file >> var1 )
    data_file >> other_var2;
    data_file >> other_var3;

Read the first element of a line in the loop statement. If it successfully reads something, there's a logical true value there, otherwise false.


As Rubberman says, if you want it to output something, you'll have to write some code for that. It won't do it by magic. I see you already know how to use cout; that'll do fine.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.