Manipulate a string?

This is a discussion on Manipulate a string? within the C++ Programming forums, part of the General Programming Boards category; I am reading data from a file into a vector. The data is in a form of lastname, firstname. I ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    3

    Manipulate a string?

    I am reading data from a file into a vector. The data is in a form of lastname, firstname. I need to switch it to be firstname lastname. Any clues on how I could do this? I have the program doing everything it needs to do, but this. This is a minor part of the program, but it looks ugly in it's present form. Any help would be appreciated. Thank you!

  2. #2
    twm
    twm is offline
    root
    Join Date
    Sep 2003
    Posts
    232
    Difficult to say without knowing how your vector is used and how you read the strings from file.
    Code:
    fs>> lastname >> firstname;
    v.push_back(firstname);
    v.push_back(lastname);
    ...perhaps?

    >Thank you!
    No no, thank you.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    3
    the data is a list in a file. the data is a GPA then lastname.firstname.
    (example)

    3.57 Jones, Steve
    2.91 Kramer, Bill


    there are 2 vectors, one for the GPA's and the second is for the string. the string ends up being lastname, firstname.
    the final output needs to be in firstname lastname

    Code:
    void main ()
    {
    vector<double> gpa;
    vector<string> names;
    float f;
    string s;
    
    
                    ifstream fin("input.txt");                     
    
    	if (!fin)
    	cerr << "error" << endl;
    
    	while (!fin.eof())                       	{
    			
                    fin >> f;
    	gpa.push_back(f);
    
    	getline (fin, s);
    	names.push_back(s);
    
    	}
    	
    	ofstream fout("output.txt");				        
    	fout << "Original Inputted Information\n\n";   
         
    	for (int i = 0; i < gpa.size(); i++)
    	fout << gpa[i] << "   " << names[i] << endl;

  4. #4
    twm
    twm is offline
    root
    Join Date
    Sep 2003
    Posts
    232
    Code:
    string firstname;
    string lastname;
    
    while (fin>> f) {
      fin>> lastname >> firstname;
      gpa.push_back(f);
      names.push_back(firstname + " " + lastname);
    }
    Tada!
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    3
    Thank you. That is almost what I need. It points me in the right direction. The problem is that the output should be in the original form before the names are reversed. Thank you for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. string manipulate
    By thinice16 in forum C Programming
    Replies: 3
    Last Post: 08-01-2008, 07:30 PM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM

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