c string conversion

I seem to be having issues with my program, I am attempting to read in a first name and and last name with a "/" between then separate the first and last names and output them separately can anyone help?
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <cstring>
#include <string>
using namespace std;

int main(){
    string name;
    char *first;
    char *last;


bool slash_flag = false;
    cout << "Enter lastname/firstname : " << endl;
    cin >> name;

char *cname = new char [name.size()+1];

for (int i=0;i<=strlen(cname);i++){
if (!slash_flag){
if (&cname[i]=="/"){
slash_flag=true;
}else{
last[i]=cname[i];
}
}else{// slashflag is true
first[i]=cname[i-(strlen(last)+1)];
}
}
int i = 0;
while(last[i] != NULL){
cout << &last[i];
i++;
}
cout << endl;
i = 0;
while(first[i] != NULL){
cout << &first << endl;
i++;
}
cout << endl;
system("pause");
return 0;
}
    

closed account (z05DSL3A)
Try this:
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
29
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string name;
    string first;
    string last;

    cout << "Enter lastname/firstname : " << endl;
    cin >> name;

    size_t found=  name.find('/');
    if (found != string::npos)
    {
        last = name.substr(0, found);
        first = name.substr(found +1);
        cout << last << " " << first << endl; 
    }
    else
    {
        cout << "\\ not found" << endl;
    }

    system("pause");
    return 0;
}
Last edited on
Thank you for the efficient code, however the instructor had requested taking it into a char array then splitting it by means of two pointers. I should have mentioned that.
I have since started working on new code, but I get a segmentation fault with this one:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <cstring>
using namespace std;

int main(){
    char name;
    char *first;
    char *last;
   
    //get first and last name
    cout << "Enter lastname/firstname : " << endl;
    cin >> name;

char *name1 = &name;
    first = strtok(name1, "/");
    last = strtok(name1, NULL);

cout << "First Name : " << first << endl;
cout << "Last Name : " << last << endl;
system("pause");
return 0;
}
closed account (z05DSL3A)
How about this:
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <string>

using namespace std;

const int bufferSize = 50;

int main()
{
    char name[bufferSize];
    char lastname[bufferSize];
    char firstname[bufferSize];
    
    cout << "Enter lastname/firstname : " << endl;
    cin >> name;

  
    char * namePtr = name;
    char * otherPtr = lastname;

    while(*namePtr != '/')
    {
        *otherPtr = *namePtr;
        ++namePtr;
        ++otherPtr;
    }
    *otherPtr = '\0';

    ++namePtr;
    otherPtr = firstname;

    while(*namePtr != '\0')
    {
        *otherPtr = *namePtr;
        ++namePtr;
        ++otherPtr;
    }
    *otherPtr = '\0';
    
    cout << lastname << endl;
    cout << firstname << endl;

    system("pause");
    return 0;

}
Last edited on
Topic archived. No new replies allowed.