0

I found a way to read the data into my array structs but i cannot control how many it reads. It will read all 14 entries in the input file but after it has read the 14 it continues for a couple lines then stops. However, if i place the code to read data inside any loop whether while or for, it starts printing huge numbers instead of what is contained in the file. Does anyone know why this happens? Is there a way around it

Input file looks as follows( exclude the dot): There are 14 sets of data in the file. So, as shown below, set 1 would include georgia 76.4 10, then north carolina 95.5 20, and so on till 14 sets.

Data inside input file looks like this:

georgia
76.4 10
north carolina
95.5 20
and so on.

Need to print data to the screen like:

georgia 76.4 10
north carolina 95.5 20
etc.

The problem lies in my code were i attempt and horribly fail to read into the input file.

#include <iostream>
#include <fstream>
#include <cstdlib>

struct ATdata  
{
   string state;
   double miles;
   int shelters;
};

int readData( ifstream& input, struct ATdata data[] );

int main()
{
    ifstream input;
    char filename[256];
    ATdata data[14];
    int i;

    cout << "Enter input file name: ";
    cin >> filename;

    input.open( filename );

    if ( input.fail() )
    {
        cout << "Input file does not exist." << endl;
        exit(1);
    }

    readData( input, data );

    cout << i << endl;
    return(0);

}

int readData( ifstream& input, struct ATdata data[] )
{
    int i, j;

    getline( input, data[i].state, '\t' );
    input.ignore( 14, '\n' );
    input >> data[i].miles;
    input.ignore( 1, ' ' );
    input >> data[i].shelters;

    for ( j = 0; j < 14; j++ )
    {
        cout << data[j].state << data[j].miles << data[j].shelters << endl;
    }
}

I apologize for all the info trying to be as clear as can be as to not look more idiotic. Thank you have a nice day.

1
  • when you input the file try cin>>filename; that should put the text you input into filename. Commented Apr 23, 2014 at 21:41

2 Answers 2

1

getline() takes an ostream& and a string. Since your types are not string, it gives you an error. I recommend reading in your input like this:

for (int i = 0; i < 14; i++ )
{
   input >> data[i].state >> data[i].miles >> data[i].shelters;
}

BTW, your state is a char, that's 1 character. Make it an std::string.

Sign up to request clarification or add additional context in comments.

2 Comments

That prints the state name or first name if two name( north carolina), followed by 13 lines of huge numbers...
input >> data[i].state is no good, since the state may contain spaces
0

The best way to get a whole line is to use std::getline. However, if you try to mix unformatted input with formatted input then it gets a bit tricky. You need lots of ignore calls and other guff. I think it is always clearer to read a whole line at a time, and then use a stringstream to parse that line.

Try this:

size_t readData( ifstream& input, struct ATdata data[], size_t max_items )
{
    size_t i;

    for ( i = 0; i < max_items; )
    {
        ATdata item;
        getline(input, item.state);

        string line_2;
        if ( ! getline(input, line_2) )
            break;

        istringstream iss(line_2);
        if ( ! (iss >> item.miles >> item.shelters) )
        {
        // skip malformed entry (or you could abort instead)
            continue;
        }

        data[i++] = item;
    }

    return i;
}

Call it with:

size_t num_items = readData(input, data, sizeof data / sizeof data[0]);

8 Comments

This is probably a dumb question but how would i declare the function outside of main. i keep getting "error: expected identifies before 'sizeof' " and also "error: expected ',' or '...' before 'sizeof' ". I declared it as size_t readData( ifstream& input, struct ATdata data[], sizeof data / sizeof data[0] ). I also tried declaring with type 'int' instead of 'size_t'.
see the first line of code in my post, copy it and put a semicolon after it. The bit where I said "Call it with" is how you call it, not how you declare it.
Got it to work and have tried manipulating the above code but cannot fix new problem. It reads and prints one set of data correctly but then prints a random number, then it prints the second set of data and then a random number and continues till 14 lines are read. It counts the random numbers as lines read.
The random numbers are not in my code. The data that is printed is done so in the same order that is written in the input file just printed with random numbers. Are they addresses? I printed data to the screen with a for loop while j was less than i.
You'll have to actually post the code you are having trouble with . There's no j in my suggested code.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.