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() )
        {
            num_records++;

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

        data_file.close();

    }

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

    return num_records;

}

Dani AI

Generated

Your program is opening the file but the reader is never actually getting usable tokens into the right variables — and you also never print the results. As noted, output is missing. As pointed out, the input types and loop logic are mismatched for the file format (names contain spaces, SSNs have dashes, wages have decimals). Also avoid testing eof() before reading and stop incrementing the record counter before a successful read — that causes off‑by‑one and possible out‑of‑bounds writes. is right: add explicit output while you debug.

A robust approach: read each line with std::getline, split into whitespace tokens, then parse tokens from the end so the variable-length name is rebuilt from the leading tokens. This handles names with spaces and keeps SSN and numeric fields in their proper string/number types.

#include <string>
#include <sstream>
#include <vector>

// inside your loop:
std::string line;
while (std::getline(data_file, line)) {
    if (line.empty()) continue;
    std::istringstream iss(line);
    std::vector<std::string> toks;
    std::string t;
    while (iss >> t) toks.push_back(t);
    if (toks.size() < 5) continue; // malformed line
    char status = toks.back()[0];
    std::string hours_str = toks[toks.size()-2];
    std::string wage_str  = toks[toks.size()-3];
    std::string ssn       = toks[toks.size()-4];
    std::string name;
    for (size_t i = 0; i + 4 < toks.size(); ++i) {
        if (!name.empty()) name += ' ';
        name += toks[i];
    }
    // convert wage/hours with stod/stoi (check exceptions) and then store/print
}

Quick checklist while fixing: 1) Use 0‑based indexing and increment after a successful read (or use std::vector). 2) Keep SSN as a string, wage as double/float, hours as int (or double if needed). 3) Print each record immediately while debugging to confirm parsing. 4) If the file still looks empty, verify the process working directory and file encoding (BOM can hide the first token). Following those steps will make the input stable and the missing output obvious.

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;
    count++;
}

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.

Ideas?

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.