Thread: Vectors and Structs

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    5

    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:
    
    
    Last edited by jamesnh; 12-14-2007 at 10:40 AM. Reason: new code

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    v.push_back(info.logon);
    info.logon is a std::string, but v is a std::vector<Student>

    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    5
    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?
    Last edited by jamesnh; 12-13-2007 at 10:52 PM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.

    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Probably. Try it and find out for yourself.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    #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.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    5
    the data is like

    Smith,John 012345678

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    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,' ')
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. vectors of structs
    By Zosden in forum C++ Programming
    Replies: 5
    Last Post: 10-05-2008, 01:37 AM
  2. help using vectors with structs
    By Swordsalot in forum C++ Programming
    Replies: 15
    Last Post: 04-09-2008, 11:14 AM
  3. allocating structs within STL vectors
    By aoiOnline in forum C++ Programming
    Replies: 20
    Last Post: 12-05-2007, 02:49 PM
  4. Variable scopes; Vectors of struct's
    By relyt_123 in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2007, 10:07 PM
  5. Vectors of structs
    By twomers in forum C++ Programming
    Replies: 7
    Last Post: 06-15-2006, 12:52 AM