first name initials last name

Feb 21, 2019 at 9:23am
closed account (4wpL6Up4)
Hi,

I am working on a program that requires the user to input a string (full and last name) and the output has to be the first name initials and the full last name.
I think I got most of it but there is one minor detail that I am not able to adjust.
The last name is printed vertically, and I don't know why.
What should I do to print it correctly?
Thanks
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 "pch.h"
#include <stdlib.h>
#include<iostream>
#include<cstdlib>
#include<cctype>
#include <sstream>
#include <string>
#include<cmath>
#include <algorithm>
#include <iterator>
#include <ctype.h>
#include <iomanip>
#include <limits>

using namespace std;

int main()
{
	string name, temp;
	int i, j, k;
	char answer;
	do {
		cout << "enter full name" << endl;
		getline(cin, name);

		cout << "The formatted name is: " << name[0] << ".";
		for (i = 0; i < name.length(); i++) {
			if (name[i] == ' ')
			{
				k = i;

				for (j = k; j <= name.length(); j++) {
					temp = name[j];
					cout << temp << '\n' << endl;
				}
			}
		}
		cout << "try again (Y/N)?" << endl;
		cin >> answer;
		cin.ignore();
	} while (answer == 'Y' || answer == 'y');

	return 0;
}
	
Feb 21, 2019 at 9:36am
Because you made it print vertically when you decided to print each character individually, separated by a newline.
1
2
3
4
			for (j = k; j <= name.length(); j++) {
					temp = name[j];
					cout << temp << '\n' << endl;
				}
Feb 21, 2019 at 9:43am
closed account (4wpL6Up4)
removing '\n' just removes the spaces and puts the characters closer together, however
are still printed vertically.
Last edited on Feb 21, 2019 at 9:43am
Feb 21, 2019 at 11:03am
endl is the same as '\n' (but that it also does a std::flush), that's what's also causing your output to be vertical.
Topic archived. No new replies allowed.