Why does my program give me the wrong output?

I want to make a program that takes a string called, "name" and break it up into 3 different strings.

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
#include <cstdlib>
#include <iostream>
#include <string>


using namespace std;

int main(int argc, char *argv[])
{
    string name, firstA, lastA, middleA;
    int first, last, middle;
    
    cout << " Enter your full name: ";
    getline(cin,name);
    
    first = name.find(" ",0);
    
    firstA = name.substr(0,first);
    
    middle = name.find(" ",first);
    
    middleA = name.substr(first,middle);
    
    last = name.find(" ",middle);
    
    lastA = name.substr(middle,last);
    
    cout << lastA << " " << firstA << " " << middleA << endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
You will want to check
last = name.find(" ",middle);

lastA = name.substr(middle,last);

What if a space character is not found? then find will return std::string::npos, to be given to last, to be passed to substr() function...

Aceix.
This is just for a school assignment. The instructions are that the user enters their first, middle, and last name. So there will be spaces in the appropriate places.

Could you elaborate a little more? I notice if I run the code breaking the string into two variables instead of 3, it works fine.
If you know there is first middle and last why not just do:
1
2
3
4
5
6
std::string first = "";
std::string middle = "";
std::string last = "";

std::cout << "Please enter your name(first middle last): ";
std::cin >> first >> middle >> last;


or use a stringstream.

1
2
3
4
5
6
7
8
9
std::string fullname = "";
std::cout << "Please enter your name(first middle last): ";
std::cin >> first >> middle >> last;

std::stringstream ss(fullname);
std::string first = "";
std::string middle = "";
std::string last = "";
ss >> first >> middle >> last;
Yes, that would be convenient. My teacher wants the input to be one string variable that gets divided into 3 different string variables that are ouputed.
Last edited on
Hi,

Found a couple of things to point you in the right direction.

You are starting the finds on a space, so use +1.

Make sure to not include the spaces in your substr

The substr is used incorrectly, should be substr(pos, len)

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

Try outputting your values for first, middle and last to make sure they are what you want.


But in any case here is the code working

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
#include <cstdlib>
#include <iostream>
#include <string>


using namespace std;

int main(int argc, char *argv[])
{
    string name, firstA, lastA, middleA;
    int first, last, middle;
    
    cout << " Enter your full name: ";
    getline(cin,name);
    
    first = name.find(" ",0);
    
    firstA = name.substr(0,first);
    
    middle = name.find(" ",first+1);
    
    middleA = name.substr(first+1,middle-1-first);

    last = name.length();
    
    lastA = name.substr(middle+1,last);
    
    cout << lastA << "," << firstA << "," << middleA << "," << endl;
 
    return EXIT_SUCCESS;
}
Say I have the name, "James W Ross"

first gets assigned the value first = 5.

I increment, "first" by one to rid me of the blank space giving me first = 6.

I then get the value of middle which is middle = 7.

then middle-1-first give me a value of 1 so the length of, "middleA" is one.

last gets assigned the value of the entire string which is 11.

Then lastA is taken from the values 8 to 11.

Is that right?

Last edited on
You may want to read up on substr. The parameters are position and length. So that means you read the 6th position(W) only.

string substr (size_t pos = 0, size_t len = npos) const;
http://www.cplusplus.com/reference/string/string/substr/
"James W Ross"

1
2
3
    cout << "first = " << first << endl;
    cout << "middle = " << middle << endl;
    cout << "last = " << last << endl;


first = 5
middle = 7
last = 12

first is the location of the first space in the entire string

middle is the location of the second space in the ENTIRE string. You only search part of the string but it gives a location in reference to the entire string.

last is the location of the last character
Last edited on
Yep, that is correct :)
I see what you mean with the find function. I keep getting confused since the first character in the string is a 0.
I now have the entire program my teacher wants complete, Thanks! I just have to get all this into functions now.

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
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;



int main(int argc, char *argv[])
{
    string name, firstA, lastA, middleA, newName;
    int first, last, middle;
    
    cout << " Enter your full name: ";
    getline(cin,name);
    
    first = name.find(" ",0);
    
    firstA = name.substr(0,first);
    
    middle = name.find(" ",first+1);
    
    middleA = name.substr(first+1,middle-1-first);

    last = name.length();
    
    lastA = name.substr(middle+1,last);
    
    newName = lastA + ", " + firstA + " " + middleA;   
    
    cout << newName << " This string is " << newName.length() 
         << " characters long " 
         << endl << " The first name is " << first << " Characters long " 
         << endl << " The last name is " << last-(middle+1) 
         << " characters long " << endl << " The comma is located at position " 
         << newName.find(",")+1 << endl;
         
    cout << " To swap things around: "; 
    firstA.swap(lastA);
    
    cout << lastA << " " << firstA << endl;
         
    system("PAUSE");
    return EXIT_SUCCESS;
}
Topic archived. No new replies allowed.