0

I have a text file containing names and numbers. The name or the string contains 15 chars and I need to put it to the string. I'm working with structures.

struct grybautojas{
    string vardas;
    int barav, raudon, lep, diena;
}gryb[100];

After that, there are simple calculations which is done right, the problem is, it only reads everything once. After taking a first "box" of information, it just stops. Everything else in the result file is either blank as a string or 0 as an integer.

Here's my input function:

void ivedimas(){
    char eil[16];
    int b,r,l;

    inFile >> n;
    inFile.ignore();

    for(int i=0;i<n;i++){
        inFile.get(eil,15);
        gryb[i].vardas=eil;
        inFile >> gryb[i].diena;

        gryb[i].barav=0, gryb[i].raudon=0, gryb[i].lep=0;

        for(int m=0;m<gryb[i].diena;m++){
            inFile >> b >> r >> l;
            gryb[i].barav+=b, gryb[i].raudon+=r, gryb[i].lep+=l;
        }

        inFile.ignore();
    }

    inFile.close();
}

And here's the file containing data:

4
Petras        3
5 13 8
4 0  5 
16 1 0 
Algis         1 
9 6 13 
Jurgis        4 
4 14 2 
4 4  15 
16 15 251 
1  2  3 
Rita          2 
6 65 4 
4 4  13

What's the problem?

2 Answers 2

2
    inFile.get(eil,15);

    Rita          2 
    Petras        3
    00000000011111
    12345678901234

I don't count 15, I count 14. Also, some of your lines seem to have a space at the end. You should rewrite your input logic to be much more robust. Read lines and parse them.

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

1 Comment

Yea, should have calculated the spaces, just copied and pasted data information without a clear look before.
0

This line:

inFile.get(eil,15);

is reading the number that follows the name.

When you try to read the number (3 in your example), you get the next one (5) instead.

Comments

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.