In my function, I am using .find and I need to be using .at, because the way I have it now doesn't account for extra whitespace etc., but I have no idea how to implement this change. I looked through the internet and I can't find a good example of the .at in work. I have an input of names:
firstname lastname and I need it to be
lastname, firstname .
My program "works", except it's bad style and I need to be using .at
Any suggestions or a good resource I can check for an example?

string parseInputString (string oneline)
       {
       string newName;  
       string firstName, lastName;   
       int index;
       
       index = oneline.find (' ');
       firstName = oneline.substr(0, index);
       oneline = oneline.substr(index + 1, oneline.length() - 1);
       lastName = oneline;
       newName = lastName + ", " + firstName + ".";
       return newName;
       }
#include<iostream>
using namespace std;
      string parseInputString (string oneline)
      {
      string newName;
      string firstName, lastName;
      int index;
      index = oneline.find(' ');
      int ret = oneline.at(index);
      firstName = oneline.substr(0, index);
      oneline = oneline.substr(index + 1, oneline.length() - 1);
      lastName = oneline;
      newName = lastName + ", " + firstName + ".";
      return newName;
      }
int main(){
string name = "FirstName LastName";
string res= parseInputString(name);
cout<<"Result: "<<res<<endl;
return 0;
}

>> My program "works", except it's bad style and I need to be using .at

I don't know what the style problem would be nor how using "at" would help.


>> I need to be using .at, because the way I have it now doesn't account for extra whitespace etc.

Again, how would using "at" help? Perhaps use find_first_of and find_last_of instead to handle more than one character of white space.

@tkud, wow. Thanks for that help! I will definitely see if I can implement that instead of what I have to see if that pleases my professor more. Hahaha. I tried something along those lines and my program crashed HARD. It said "program is being asked to execute in an unexpected way...shutting down..." or someting along those lines, so using .at had me scared! I was like WTH???!!! I'm such a noob, I still panic at small stuff.
@ VernonDozier, yeah, "bad style" is my professor's terminology and it wasn't exactly along the lines of what he had in mind. I guess it's true your suggestion of using find_first_of, find_last_of, but when he emailed me his "thoughts" on my program, he definitely said use .at, and what I had was "bad style" because of the white space issue, so I tried switching over and realized that I had no clue what ".at" even really meant! I'm a disaster. I know it.
Since y'all seem to be pretty proficient, can you reccomend a good resource to brush up on this stuff, beyond what my crappy textbook teaches? Thanks!

string parseInputString( const string& oneline ) // prefer passing by const referance
{
    // when possible, initialize objects at the point of their definition
    string::size_type index = oneline.find (' ') ;
    string firstName = oneline.substr( 0, index ) ;

    // find the next non-space character
    index = oneline.find_first_not_of( ' ', index ) ;

    string lastName ;
    if( index != string::npos ) // if there is something after the spaces
        lastName = oneline.substr( index ) ; // lastName is everything from there on

    return lastName + ", " + firstName + "." ;

}

>> I tried something along those lines and my program crashed HARD. It said "program is being asked to execute in an unexpected way...shutting down..." or someting along those lines, so using .at had me scared! I was like WTH???!!! I'm such a noob, I still panic at small stuff.


Nothing to be scared about. It just means you had a bug that crashed the program. It almost certainly meant that you you were trying to access an array element that didn't exist. Probably -1, which is npos. Note that the code from vijayan121 checks for npos.

That's the whole point of "at". It helps you avoid that problem. But you still have to handle that out_of_range exception somehow.

http://www.cplusplus.com/reference/string/string/at/

This member function behaves as operator[] , except that at also performs a range check, throwing an exception of type out_of_range in case that pos is not an actual position in the string.

As for resources, here's your online resource for everything C++. You can find all the standard functions like "at" here.

http://www.cplusplus.com/reference/

Thanks Vernon! I know about cplusplus.com, but I guess it just seemed overwhelming since it has SO MUCH info, but literally everyone recommends it, so I'm just going to make time everyday and start going through the site. (I have a total deskjob, so that helps...ha!)It's the only way I'm going to really learn, I guess. It seems like everyone in my class has been programming their whole life and they taught themselves! I think that's crazy. Wow.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.