9 Replies - 3807 Views - Last Post: 20 March 2012 - 02:36 PM Rate Topic: -----

#1 oAllElseFailo  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 23-September 11

How to split a string from user input?

Posted 20 March 2012 - 01:11 AM

I can't think of how to do this. The user needs to input their first and last name

    string userName;
    cout << "Please enter your name: ";
    getline(cin, userName);



Then I need to somehow split that to store their first and last name in different string variables.

Would i need to do something like .find(" ") that would find the first white space or am I on the wrong track here? I think that would just return a number of an array though.. ARGH!

And just so others know, I've googled it and checked other people's answers but they all use this split.h or #include <iostream> and we haven't gone over any of that.

Help ><

This post has been edited by oAllElseFailo: 20 March 2012 - 01:32 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How to split a string from user input?

#2 raspinudo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 61
  • View blog
  • Posts: 232
  • Joined: 19-September 11

Re: How to split a string from user input?

Posted 20 March 2012 - 01:36 AM

I'll give you the 'c-style' way of doing this.

#include <iostream>
#include <string>
using namespace std;

int main(){
    string input;
    getline(cin, input);
    string temp1;
    string temp2;

    for(int i = 0; i < input.length(); i++){
        if(input[i] == ' ') { 
            temp1.append(input.substr(0,i));
            temp2.append(input.substr(i+1,input.length()-1));
            break;
        }
    }
    
    cout << temp1 << endl;
    cout << temp2 << endl;
}



there are shorter ways to do this, but this should give you a better idea of how char arrays are dissected.

Once you understand this, a good thing to look into would be the sstream header in c++
http://www.cplusplus...m/stringstream/

This will allow you to token-ize your input quickly and efficiently
gl

This post has been edited by raspinudo: 20 March 2012 - 01:42 AM

Was This Post Helpful? 1
  • +
  • -

#3 oAllElseFailo  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 23-September 11

Re: How to split a string from user input?

Posted 20 March 2012 - 01:52 AM

Can you do that same code without using append?

I could follow that up to that point then it lost me.
Was This Post Helpful? 0
  • +
  • -

#4 raspinudo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 61
  • View blog
  • Posts: 232
  • Joined: 19-September 11

Re: How to split a string from user input?

Posted 20 March 2012 - 01:57 AM

All append does is add to the end of a string.
You could also just say:

temp1 = input.substr(0,i);
temp2 = input.substr(i+1,input.length()-1);




note: if you found my help useful, be sure to +1 me :)

also, be sure to look into the libraries for string on the link I left above, there you fill find all types of helpful functions such as append(), erase(), insert(), find().

happy coding

This post has been edited by raspinudo: 20 March 2012 - 02:02 AM

Was This Post Helpful? 1
  • +
  • -

#5 oAllElseFailo  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 23-September 11

Re: How to split a string from user input?

Posted 20 March 2012 - 02:07 AM

Oh i see, cool i'll try that out! We went over all that, erase, insert, find, at, etc. but never went over append.. Thanks for your help!
Was This Post Helpful? 0
  • +
  • -

#6 raspinudo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 61
  • View blog
  • Posts: 232
  • Joined: 19-September 11

Re: How to split a string from user input?

Posted 20 March 2012 - 02:12 AM

Always happy to help out a fellow coder.

Feel free to shoot a pm' my way if you get caught up on anymore small issues such as this.
Was This Post Helpful? 1
  • +
  • -

#7 raspinudo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 61
  • View blog
  • Posts: 232
  • Joined: 19-September 11

Re: How to split a string from user input?

Posted 20 March 2012 - 03:24 AM

I'm going to repost a pm response I sent in regards to this convo in case it can help anyone else:

Quote

String is a class, just like any other. It has one private data member, which is a char array(char[]). This is the same in java, they just hide it from you. If it makes it more readable, you can use the .at() function when accessing chars in a C++ string. The reason I use the [] notation is just because I'm used to it. In C++ their is a concept called "operator overloading" which allows you to rewrite an operators functionality in regards to your particular class. That is exactly what STL does with the string class, they override the brackets so you can use them to directly access chars. What I'm trying to get at is, all strings are simply char arrays at their core, they are simply wrapped in a class, and given a bunch of functions to operate on them.

Let's say we have a string test = "foo"
What this looks like is really a char array -> [f][o][o]
so when I say string temp += test[0], I am really saying add the first block of the char array to temp, which will now look like this:
temp = [f]
when its printed it says "f"
It's just that the string class hides most of this from you to simplify things.

I hope this clears things up a bit.


I hope this can help

This post has been edited by raspinudo: 20 March 2012 - 03:24 AM

Was This Post Helpful? 1
  • +
  • -

#8 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 651
  • View blog
  • Posts: 2,240
  • Joined: 31-December 10

Re: How to split a string from user input?

Posted 20 March 2012 - 10:09 AM

If the strings are separated by whitespace, instead of calling getline, just use the extraction operator(>>) on cin:
string firstname, lastname;
cout << "Enter first and last name: ";
cin >> firstname >> lastname;
cout << "Hello " << firstname << ' ' << lastname << endl;


Was This Post Helpful? 1
  • +
  • -

#9 raspinudo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 61
  • View blog
  • Posts: 232
  • Joined: 19-September 11

Re: How to split a string from user input?

Posted 20 March 2012 - 01:49 PM

I agree completely, but the OP was a bit confused on how strings are really char arrays, so I thought I could illustrate that better with the loop.
Was This Post Helpful? 1
  • +
  • -

#10 oAllElseFailo  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 23-September 11

Re: How to split a string from user input?

Posted 20 March 2012 - 02:36 PM

I actually didn't know you could do it that way vivid. So thanks. I went ahead and did it raspinudo's way since that's what we covered with getline and such so again, thanks to you as well rasp, you were a big help
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1