Strings Program with full names

I am having trouble getting the last two statements to execute that should print the full name in the middle of the screen. any help is greatly appreciated. Thanks!!

Program is as follows:

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int main()
{
string name;
int length;
string lname = " ";
string fname = " ";
int space;
int i;
char ans = 'y';

while (ans == 'Y' || ans == 'y')
{
cout << "What is your first and last name?" << endl;
getline(cin, name);
length = name.length();

for (i = 0; i < length; i++)
{
cout << name[i] << endl;
}

cout << left << setw(40 + (.5 * length)) << setfill(' ');

space = name.find(' ');
//cout << "Position = " << space << endl;

for (int i = 0; i < length; i++)
{
if (i < space)
{
fname += name[i];
}
else if (i > space)
{
lname += name[i];
}
}
name = lname + ", " + fname;
length = name.length();
cout << setw(40 + (.5 * length)) << left << setfill(' ');

}
cout << "Would You Like To Continue?" << endl;
cin >> ans;

return 0;
}
Code tags are nice to use because they make your posts more inviting and less scary... Anyways:

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
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int main()
{
  string name;
  int length;
  string lname = " ";
  string fname = " ";
  int space;
  int i;
  char ans = 'y';

  while (ans == 'Y' || ans == 'y')
  {
    cout << "What is your first and last name?" << endl;
    getline(cin, name);
    length = name.length();

    for (i = 0; i < length; i++)
    {
      cout << name[i] << endl;
    }

    cout << left << setw(40 + (.5 * length)) << setfill(' ');

    space = name.find(' ');
    //cout << "Position = " << space << endl;

    for (int i = 0; i < length; i++)
    {
      if (i < space)
      {
        fname += name[i];
      }
      else if (i > space)
      {
        lname += name[i];
      }
    }
    name = lname + ", " + fname;
    length = name.length();
    cout << setw(40 + (.5 * length)) << left << setfill(' ');

    cout << "Would You Like To Continue?" << endl; // Move this here...
    cin >> ans;                                    // Move this here...
    cin.ignore();                                  // Add this because you're
                                                   // using getline as well.
                                                   // (fixes the whole
                                                   // "getline skipping issue
                                                   // and many have when using
                                                   // getline with cin")
  }

  return 0;
}
Thanks a lot!! Helped greatly with the execution needed, but it is still not printing in the name in the middle along with it down the side. It is supposed to print the name down the left side the first time and then in the middle two times. Instructions are as follows:

Determine the length of the name and store the results in a variable.

Using a loop, print each of the characters of the name (array) on a separate line (from beginning of name list to end of name list).

The program should then, using a screen width of 80, print the name centers the name on the screen.

Next the system should add the function to find the blank space within the name and store the results in a variable.

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).

Next – concatenated (add) the pieces together with the appropriate comma to store the name as lname, fname into the original name identifier.

The program should then, using a screen width of 80, center the name on the screen, using the format: lname, fname (don’t forget the comma)

Place the entire program into a loop allowing the user to repeat the section as desired.
//Kimberly Rappold string.cpp
//Assignment: 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int main()
{
	string name;
	int length;
	string lname = " ";
	string fname = " ";
	int space;
	int i;
	char ans = 'y';

	while (ans == 'Y' || ans == 'y')
	{
		cout << "What is your first and last name?" << endl;
		getline(cin, name);
		length = name.length();

		for (i = 0; i < length; i++)
		{
			cout << name[i] << endl;
		}

		cout << left << setw(40 + (.5 * length)) << setfill(' ');

		space = name.find(' ');
		//cout << "Position = " << space << endl;

		for (int i = 0; i < length; i++)
		{
			if (i < space)
			{
				fname += name[i];
			}
			else if (i > space)
			{
				lname += name[i];
			}
		}
		name = lname + ", " + fname;
		length = name.length();
		cout << setw(40 + (.5 * length)) << left << setfill(' ');

		cout << "Would You Like To Continue?" << endl;
		cin >> ans;
		cin.ignore();
	}
	return 0;
}
You can read my comments.
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int main()
{
  string name;
  int length;
  string lname = ""; // I removed your space here.
  string fname = ""; // I removed your space here.
                     // Strings will count spaces as a character
  int space;
  int i;
  char ans = 'y';

  while (ans == 'Y' || ans == 'y')
  {
    cout << "What is your first and last name?" << endl;
    getline(cin, name);
    length = name.length();

    // Printing each character of the string on it's own line
    for (i = 0; i < length; i++)
    {
      cout << name[i] << endl;
    }

//  cout << left << setw(40 + (.5 * length)) << setfill('');
    // Here, we are printing the name the middle of a 80 width terminal screeen
    cout << setw(40+name.size()/2) << name << endl;

    // Here, you've already found the position of the space
    space = name.find(' ');
    //cout << "Position = " << space << endl;

    /* There is a much better way to do this. I explain below...
    for (int i = 0; i < length; i++)
    {
      if (i < space)
      {
        fname += name[i];
      }
      else if (i > space)
      {
        lname += name[i];
      }
    }
    */

    // Below, we use the space variable to determine where the first and last
    // name are within the string "name". By doing this, we will set fname and
    // lname to their appropriate values.
    
    // "I want fname to be equal to a specific part of the string name. For
    // this reason, I will use the built-in substr function. Since the first
    // name starts at the beginning, I will feed in parameters 0 and space,
    // with 0 denoting the beginning of the name and space is where we should
    // cut it off."
    fname = name.substr(0, space);

    // "I want lname to be the last name. For this reason, I will use the
    // built-in substr function. The last name portions are the position after
    // the space and the very ending of the name of the string."
    lname = name.substr(space+1, name.size()-1);

//  cout << fname << " " << lname << endl; // Because your steps don't say you
                                           // need this, I would leave it out.

    // Here, you concatenated the strings with comma
    name = lname + ", " + fname; // Here, you concatenated the strings w/ comma

    // You don't really need these parts now...
//  length = name.length();
//  cout << setw(40 + (.5 * length)) << left << setfill(' ');

    // Print the new name in the middle of a 80 width terminal screen.
    cout << setw(40+name.size()/2) << name << endl;

    cout << "Would You Like To Continue?" << endl;
    cin >> ans;
    cin.ignore();
  }
  return 0;
}


The terminal emulator I use sets wrap at 80 characters, so it's centered nicely on mine. You can play around with iomanip library though...
Thank you so much!! I had completely forgotten about substr. It works great!! Thank you again.
Topic archived. No new replies allowed.