Finding names

Im trying to write a program that can gather a users full name and remove any
extra spaces. then display the name back with only one space and the full name rearranged last name, first name an middle initial. I got it to work for the last part but for some reason cant get it to work for the middle name.
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
 {
		char first, middlename, lastname, c,count;
		count = 0;
		cout << "Please enter your first, middle, and last names: ";
		getline(cin,fullname);
		cout << "\n";
		

		while ((fullname.empty()) || (fullname == " "))
		{
			cout << "Please enter your first, middle, and last names: ";
					getline(cin, fullname);
					cout << "\n";
		}
			for (int i = 0; i < fullname.length(); i++)
			{

				c = fullname.at(i);

				if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c == ' ')))
				{
					cout << "Please enter your first, middle, and last names: ";
					getline(cin, fullname);

				}
			}
			
					first = fullname.find(" ", 0) - 1;
					middlename = first + 1;
					while (isspace(fullname[middlename]))middlename++; count++;
					lastname = fullname.find(" ", middlename);
				while (isspace(fullname[lastname]))lastname++;
				cout << "Your Name" << "                 " << "Your name written proffessionally" << endl;
				cout << fullname.substr(0, first + 1) + " " + fullname.substr(middlename, ( first+ count)) + " " + fullname.substr(lastname, fullname.length() - lastname) +
					"       " + fullname.substr(lastname, fullname.length() - lastname) + ", " + fullname.substr(0, first + 1) + ", " + fullname.substr(middlename, 1) + ".\n" << endl;
				return(fullname);
                
			
		
}
Last edited on
I really suggest you stop putting multiple statements on one line. Look at this snippet:
1
2
				while (isspace(fullname[middlename]))middlename++; count++;
					lastname = fullname.find(" ", middlename);

Do you realize that when properly formatted you get:
1
2
3
4
5
    while (isspace(fullname[middlename]))
        middlename++;
    
    count++;
    lastname = fullname.find(" ", middlename);


Is this what you intended?

When I copied my code over I might have deleted a line resulting in multiple statements on one line. But the code looks like what you wrote. Im using the count variable to measure the length of the middle name
The problem I continue to have is that if the user typed in multiple blank spaces then the middle name when printed out includes those extra spaces. Im trying to figure out how to remove the extra spaces
But the code looks like what you wrote. Im using the count variable to measure the length of the middle name

Do you realize that only one line in contained within that loop?

Have you considered using a stringstream to process your string to remove the unwanted spaces?
how would I do that?

edit: I just caught that thanks. I included the count , moved it to were I needed it and it helped. Still checking to make sure its fully solved.
Last edited on
how would I do that?

If you're referring to using a stringstream to process the string then perhaps something like:
1
2
3
4
5
   std::string input("This   is     my   input.");
    std::stringstream sin(input);
    std::string temp;
    while(sin >> temp)
        cout << temp << " " << temp.length() << endl;

This uses the fact that the extraction operator>> skips leading whitespace characters by default when extracting strings.
Topic archived. No new replies allowed.