Creating objects using data from text file.

Pages: 12
It makes no difference - it's better to get advice in public on the forum, than in private, for all the reasons I mentioned.
Last edited on
1st it is not crappy

That's really a matter of opinion.

the code was meant to solve his problem only I wrote it without even thinking

Precisely. Perhaps you should put more thought into the code you post.

you said it has many poor/bad practices, what are you talking about?

It does. From the horrible C-strings, to creating objects with deferred initialization, to using eof() to control a read loop, all poor if not outright bad practices when writing modern C++ code.

u just want my code to be like them,

Who is this "them"?

go suicide dude

Not likely, but perhaps you should seek out consoling for yourself.

c strings as parameters are used for speed reasons, eof()? rlly, you are just shit talking, like them i meant your crappy comments. the code is complete and perfect and rlly go suicide
Last edited on
@MikeyBoy Yeah right I agree
c strings as a parameter is used for speed reasons,

What makes you think using c-strings offer speed benefits?


eof()? rlly, you are just shit talking, the code is complete and perfect

Far from perfect. And using eof() for controlling a read loop is not considered a good practice, far from it. Using it, as you are, leads to erroneous data in many cases.

rlly go suicide

Grow up.
simply because the std::string type copy operation is slower then copying a const char*

eof() leads to errors maybe yes but not in my code

NO rlly go suicide
simply because the std::string type copy operation is slower then copying a const char*

And why are you invoking a copy operation? Your reasoning must be flawed, if you properly pass the string you avoid copying the string or perhaps you don't understand how to properly pass non-trivial classes like std::string.

eof() leads to errors maybe yes but not in my code


What, your code is a prime example of how not to use eof() to control a read loop.

Last edited on
So, fewdie's "the code is complete and perfect" not so much attempt.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

#if 0
With an 'int' for the number, >> just barfs on numeric overflow and never gets
past the first record.
Read firstName=Jason=
Read lastName=Matthews=
Read birthYear=1997=
Read number=2147483647=
Read username==
Read password==
#endif

struct UserData {
    UserData() :birthYear(-1), number(-1) {}

    string firstName;
    string lastName;
    int birthYear;
    long int number;    //!! int is too short
    string username;
    string password;
};

UserData GetData(string username, const char* file)
{
    ifstream infile;
    UserData tmp;
    infile.open(file);
    if (infile.fail())
        return UserData();    // failed to open file return an 'invalid' user

    while (!infile.eof())
    {
        infile >> tmp.firstName
                >> tmp.lastName
                >> tmp.birthYear
                >> tmp.number
                >> tmp.username
                >> tmp.password;
        cout << "Read firstName=" << tmp.firstName << "=" << endl;
        cout << "Read lastName=" << tmp.lastName << "=" << endl;
        cout << "Read birthYear=" << tmp.birthYear << "=" << endl;
        cout << "Read number=" << tmp.number << "=" << endl;
        cout << "Read username=" << tmp.username << "=" << endl;
        cout << "Read password=" << tmp.password << "=" << endl;
        if (tmp.username == username)
            return tmp;     // user found
    }

    infile.close();
    return UserData();    // user not found return an 'invalid' user
}

int main()
{
    UserData user = GetData("nobody", "foo.txt");
}


With this input

$ cat foo.txt 
Jason Matthews    1997    6782349567    user1997    pass1997
Kelsey James    1980    2484560234     user1980    pass1980
John Doe    1990    4567890231    user1990    pass1990


Wait for it.....
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
$ ./a.out 
Read firstName=Jason=
Read lastName=Matthews=
Read birthYear=1997=
Read number=6782349567=
Read username=user1997=
Read password=pass1997=
Read firstName=Kelsey=
Read lastName=James=
Read birthYear=1980=
Read number=2484560234=
Read username=user1980=
Read password=pass1980=
Read firstName=John=
Read lastName=Doe=
Read birthYear=1990=
Read number=4567890231=
Read username=user1990=
Read password=pass1990=
Read firstName=John=
Read lastName=Doe=
Read birthYear=1990=
Read number=4567890231=
Read username=user1990=
Read password=pass1990=

Yep, that's right.
Failing to check for end of file properly, and failing to check the result of the >> operators means effectively processing the last record twice.

Nothing was actually read on the 4th pass, it's just whatever happened to be lying around in memory at that time. The eof() wasn't going to stop the 4th pass because eof() was still false. Had the code being doing what it should have, and checking the actual status of >>, it would have known there was nothing to do on the 4th pass.

What's in the variables?
Maybe it's the last data, maybe it's a completely new object, maybe it's a pile of garbage.

Regardless, it was there and the code tried to do something with it.
If you're lucky, the garbage doesn't result in a crash or a false match.

Try copying say a BMP file with a poorly written eof() loop and then wonder why all your graphics programs spit it back as a malformed image.

> eof() leads to errors maybe yes but not in my code
Today's half-assed hack is tomorrow's obscure bug.
@jlb I hope your mom gets cancer and dies a slow painful death and that your dad left you as a baby
@salem c
yeah and that's an ez to fix things just read the 1st user before the loop then inside the loop check that user then read the next one, that should fix the problem, other than that: I hope your mom gets cancer and dies a slow painful death and that your dad left you as a baby you terrorist
Last edited on
Topic archived. No new replies allowed.
Pages: 12