Doubt of using Strings in C++

Hi everybody! I´m new at C++ and I´m trying to separate the name and the surname inserted in a char. Here is my code:




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

int main(){

int i;
char* name;
char* surname;
char a= ' ';

nombre="Andrew Jona"; // Andrew is the name and Jona de surname
cout<<nombre;

for(i=0;i<strlen(name);i++){


if(name[i]==a){
i++;
for(i;i<strlen(name);i++){

strcat(surname,name);

}

}
cout<<surname;




I don´t know why, this is giving no erroes but doesn´t work properly.

Can anyone help me please?
What is nombre?
I understand you may have edited your code. It's best to post the actual code which you are running, otherwise we have to guess.

The logic which you are using looks odd.
If we assume for a moment, that you have a valid string, you need to step through it one character at a time until a space is found. Then, in your case you just add 1 to skip past that space (if there is more than one space, you might need to loop again until the next non-space character is found).

Then, you need a valid destination, surname is just an uninitialised pointer, it doesn't point to a valid address. You should allocate an array in which to store the result, for example char surname[30];. Then lastly, you could just use strcpy() to copy from name/nombre from the point previously found.
Maybe strcpy(surname, nombre+i);

Unless you really have to use character arrays like this, you should consider using c++ std::string, these are safer and easier to use.

Last edited on
okey sorry, i was trying to translate my code from spanish to english... nombre is name.
I followed your tips but still not working... should I try with this?


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

int main(){
int z;
z=;
int i;
char* name;
char* surname;
char a= ' ';

name="Andrew Jona";
cout<<name;

for(i=0;i<strlen(name);i++){


if(name[i]==a){
i++;
//cout«name[i];

for(i;i<strlen(name);i++)
{
for(z;z<strlen(name);z++){

name[i]==surname[z];
}




}

cout<<surname[z];
}



}

return 0;


}




My intetion is to copy every character from name to apellido after the white space. I mean, copy in surname only "Jona"

thank you for your help!
There is an error in this line,
 
    z=;
so that code does not compile. In fact z is never given any meaningful value, though it is used within the code.

There are also a number of warnings, which point to various areas which are definite or probable problems.


Here, the == operator is the relational operator to test for equality. I assume that you intended to use just = in order to assign the value, that is copy a character to name[i]
 
    name[i]==surname[z];


That also gives rise to a problem. name is pointing to the literal string "Andrew Jona" which under the rules of C++ is a constant, that is, a read-only value. So you cannot modify it by assigning any value there.

But there is another problem. surname is a pointer. But what does it point to? It has not been initialised and therefore contains garbage. Hence you may neither read from nor write to anything at that address.

Another thing which looks wrong is that the for loops are nested three deep. That is there are three for loops, one inside the other. I don't think that makes sense. Keep the loops separate.

Also I don't think you should simply output the surname character by character, cout<<surname[z]; - except perhaps during debugging.

Overall, this looks a bit of a mess, and not really well-thought out.

You need something like this:
1
2
char name[30] = "Andrew Jona";
char surname[30] = "";

Then you need to step through the name array until the space is found. That is the first loop. (Note, you may need to use a break statement to exit from this loop, or add the condition to the top of the for loop instead). Note, you could simply use strchr() instead of writing your own loop.
http://www.cplusplus.com/reference/cstring/strchr/

Add 1 to skip past the space.

Then copy characters one at a time from the name array to the surname array. That is the second loop. (Here you could just use strcpy() instead of writing your own loop).
http://www.cplusplus.com/reference/cstring/strcpy/

If necessary, add a terminating null character '\0' at the end of the surname.

Lastly print out the surname.
Last edited on
An example using strchr() and strcpy().

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <cstring>
#include <iostream>

using namespace std;

int main()
{
    
    const char * name = "Andrew Jona"; // const because string is read-only
    cout << name << '\n';
          
    char surname[30] = "";
    
    char * from = strchr(name, ' ');
    
    if (!from)
    {
        cout << "space not found\n";
    }
    else
    {
        ++from; // skip past the space
        strcpy(surname, from);
    }
     
        
    cout << surname << '\n';   
}
Thank you so much for your help Chervil! and sorry for my poor programming knowledge...
Well, you increase your programming knowledge by actually writing code, perhaps you are trying to run before you learned to walk. Working from a book can help too. Also, the code I gave above was mostly C rather than C++ as that was the style of your own code. But if you want to learn C++, then you could start by going through the tutorial on this site, but it probably won't be enough on its own:
http://www.cplusplus.com/doc/
Topic archived. No new replies allowed.