push_back problem

This is a discussion on push_back problem within the C++ Programming forums, part of the General Programming Boards category; I have a file that contains names and grades. It looks like this Mike 60 Joe 25 Steve 80 ... ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Location
    Kansas City
    Posts
    76

    push_back problem

    I have a file that contains names and grades. It looks like this

    Mike 60
    Joe 25
    Steve 80
    ...

    This file is opened inside a function. Here is the function:

    Code:
    void Bag_of_Grades::input_file ()
      {
        ifstream grades_stream ;
        grades_stream.open ( "D://C++//notes//streams_data1.dat" ) ;
    
          string name;  
          int score;    
    
     while ( grades_stream >> name >> score)
                  {
                    grades.push_back ( name  );
    
                  }
    
        }
    This function works, but in only passes the name into the vector (grades)

    I tried alot of stuff including:
    Code:
    grades.push_back ( name , score );
    
    or
    
    grades.push_back ( name  );
    grades.push_back ( score  );
    
    or
    
    grades.push_back ( name  >> score);
    But I can't get this right

    How can I pass the name and the score to the vector ?

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Aye, you can't push an int into a vector of strings. Sounds like you want a vector of structs, each struct containing a name and a score. Alternately, if you knew that each name was unique, you could use a map of strings to ints.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    im no whizz but ive just never seen this, what does it do, and is it correct?
    Code:
    while ( grades_stream >> name >> score)
    how did you define your grades, maybe it can only hold strings the way you declared it.
    thats my input

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,436
    grades_stream is an input stream object (much like cin, only on this case an ifstream). It is simply reading the value of the stream after an attempt to retrieve a string(name), followed by a space, followed by an int (score).

    If that particular format is not present on the next reading iteration (for instance it would contain "nadroj user", where "user" is obviously not an int), the the stream will return false and the reading loop will end.
    The programmerís wife tells him: ďRun to the store and pick up a loaf of bread. If they have eggs, get a dozen.Ē
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> what does it do, and is it correct?
    It is correct, and usually the best alternative to using while (!eof()).

  6. #6
    Registered User
    Join Date
    Sep 2006
    Location
    Kansas City
    Posts
    76
    It is correct and it "transfers" the elements from the grade_stream into name and score.

    for example "Mike" goes into name and 60 goes into "score"

    PS. I made it a vector of struct and it works, thanks for the tip pianorain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21