# Vectors and Structs

• 12-13-2007
jamesnh
Vectors and Structs
I was wondering if you guys mind looking over my code and find out why its not compiling. I dont need you to do it but some hints would be helpful.

What im looking to do is input data from a text file. Then put that into a vector object of strut type. Then take the last name which comes first before a comma and take 6 characters of that and last two digits from there social and make that there logon name and use there social as a password. I need to output this list to a file and output duplicates to the screen.

Sorry i havent put any pre or post comments on yet.

Code:

• 12-13-2007
laserlight
Quote:

v.push_back(info.logon);
info.logon is a std::string, but v is a std::vector<Student>

Quote:

if (info.logon(i) == info.logon(i+1))
I believe you actually mean:
Code:

`if (info.logon[i] == info.logon[i+1])`
The same goes for the two lines after it. Oh, and you might want to indent your code properly. Since you did not use braces when you should have:
Code:

```for (int i =0; i< ct; i++)     if (info.logon(i) == info.logon(i+1))     cout << info.name(i) <<" has a duplicate logon " << info.logon(i) << endl;     cout << info.name(i+1) << " has a duplicate logon " info.logon(i+1) << endl;```
is actually:
Code:

```for (int i =0; i< ct; i++)     if (info.logon(i) == info.logon(i+1))         cout << info.name(i) <<" has a duplicate logon " << info.logon(i) << endl; cout << info.name(i+1) << " has a duplicate logon " info.logon(i+1) << endl;```
What you really want is:
Code:

```for (int i = 0; i < ct; i++) {     if (info.logon[i] == info.logon[i+1])     {         cout << info.name[i] <<" has a duplicate logon " << info.logon[i] << endl;         cout << info.name[i+1] << " has a duplicate logon " << info.logon[i+1] << endl;     } }```
Note that I added a "<<" between " has a duplicate logon " and "info.logon[i+1]".

Incidentally, I think ct was never initialised.
• 12-13-2007
jamesnh
thanks missed the ct.

thought you could use () because of v.at() was the same as v.at[]

if i wanted to use the vector pushback and add the string to the end of the struct what would the format or syntax be?
• 12-13-2007
laserlight
Quote:

thought you could use () because of v.at() was the same as v.at[]
Nope, v.at(i) is the same as v[i], except that at() throws a std::out_of_range exception if i is not a valid index.

Quote:

if i wanted to use the vector pushback and add the string to the end of the struct what would the format or syntax be?
First assign the string to the struct's member, then push_back the struct.
• 12-13-2007
laserlight
Probably. Try it and find out for yourself.
• 12-14-2007
hk_mp5kpdw
#1
Code:

```int comma; ... comma = info.name.find (",");  // Search for the comma if (comma == -1)     cout << "Invalid Data"; else     logon = info.name.substr(0, comma);```
Should be:
Code:

```string::size_type comma; ... comma = info.name.find (",");  // Search for the comma if (comma == string::npos)     cout << "Invalid Data"; else     logon = info.name.substr(0, comma);```

#2
Code:

```while (!fin.fail ()) {     getline (fin, info.name);     getline (fin, info.soc);     ... }```
You may need to rethink that loop. The fail will only return true after a read attempt has failed. At the last successful read of data from the file, when there is no more left to read, this will still return true when it's called the very next time. Though there is no more to read from the file, the loop will execute one more time trying to get data that isn't there. The getlines will then set the appropriate error/fail bit for the ifstream object but by that time it's already too late since you're in the loop and processing bad data. The info.name and info.soc variables will likely still contain the results from the previous successful read. Usually, the fix for this is to put the getline call in the while loop's condition test, i.e.:
Code:

```while ( getline (fin, info.name) ) {     getline (fin, info.soc);     ...```
#3
Code:

```int ct; while (...) {     ...     ct++; } for (int i =0; i< ct; i++)```
Why have ct at all? The vector has a size member function that returns how many elements are stored within it. Just use that. There is no need for a separate variable to keep track of this.

#4
Quote:

Originally Posted by jamesnh
i appreciate the help. I found a problem with my getline statements(wasn't stopping at the space).

i was wondering if anyone could give me some insight why this inst working out so well?

Code:

`comma = info.name.find (',');  // Search for the comma`

Don't know, what's the content of the input file look like? Just for grins, what compiler/IDE are you using? MSVC6 has some problems with the getline function.
• 12-14-2007
jamesnh
the data is like

Smith,John 012345678
• 12-14-2007
hk_mp5kpdw
Put a couple print statements in the loop so you write out the name and soc parts right after you've read them in. See if you get what you expect in each iteration of the loop.

Quote:

Originally Posted by jamesnh
I found a problem with my getline statements(wasn't stopping at the space).

If you haven't fixed that issue, then the first getline call should likely be:
Code:

`getline(fin,info.name,' ')`