0

Confused.. my teacher asked me to put this information into a string from an array.. here are the exact direction she had given me:

"Using the location of the space, use this and the appropriate string functions to store the first name in fname and the last name in lname (both string variables)."

The isspace function found the white space for me, and i put it in the variable 'y' as you will see in my code below. but I have no idea how to use the white space to put it the characters entered from the array into two separate string variables!

... any help would be greatly appreciated!

here is my code:

// Zachary Law Strings.cpp

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


int main()
{   int x, i,y;
char name[] = "Please enter your name: ";
char answer1 [80];
i=0;
y=0;

cout << name;
cin.getline(answer1, 79);
cout << endl;

x=strlen(answer1);

 for (int i = 0; i < x; i++){
    cout << answer1[i] << endl;
    if (isspace(answer1[i])) 
    {y=y+1;}}

 cout << endl << endl;
 cout << setw(80) << answer1;

 cout << y;




return 0;}
Zach Law
  • 23
  • 1
  • 8
  • There's a ton of options here. Hints: `std::string(char*,int)` will create a string from the specified `char*` of the specified length, or you could store the full answer in an `std::string` then use `std::string::substr()` to pull out the first and last name given the position of the space, or you could use `for` loops to copy one character at a time from `answer1` (from start to space, then from space to end) to somewhere else, etc. See also http://stackoverflow.com/questions/4637139/could-anyone-suggest-me-a-simple-way-of-splitting-names-in-c – Jason C Nov 26 '14 at 03:33
  • Thank you for your response, my issue is that I have to do this using whitespace – Zach Law Nov 26 '14 at 03:37
  • That's what all those solutions do... "whitespace" is a thing, not a tool you can use for something... you find the location of the whitespace in the string, then use one of the tools to split it in two at that location. You don't "use the whitespace to split the string", you "find the whitespace, and split the string at that location". – Jason C Nov 26 '14 at 03:45
  • Would you mind giving me an example on how I would set up the string substr() function? I've searched google and on here and I'm coming up with nothing :( – Zach Law Nov 26 '14 at 04:00
  • Did you not read the link in my first comment, which contains a clear example of exactly that... ? I didn't type that comment just to practice my typing. – Jason C Nov 26 '14 at 04:03

1 Answers1

0

You can see this code... I've edited your code... Hope, you will get this... :)

#include <iostream>
using namespace std;
#include <string>
#include <string.h>
#include <iomanip>


int main()
{
    int x, i,y,fg=0,z;
    char name[100];
    string answer;
    i=0;
    y=0;

    cout<<"Enter Your First & Last Name:";
    cout << endl;
    cin.getline(name,100);

    x=strlen(name);

    for (int i = 0; i < x; i++){
        if (!fg&&isspace(name[i]))
        {
            y=i;
            fg=1;
        }
        else if(fg==1&&!isspace(name[i]))
        {
            z=i;
            fg=2;
        }
        answer+=name[i];
    }

    string fname=answer.substr(0,y);
    cout<<fname<<endl;
    string lname=answer.substr(z,x-z);
    cout<<lname<<endl;
    return 0;
}
Mukit09
  • 2,956
  • 3
  • 24
  • 44