how to print

hi guys
im new in c++, and i don't know much.

when i run the following code

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
	char input[180] = {};
	char name[80] = {};
	char age[4] = {};
	char title[50] = {};

	cout << "Enter a name, age, and title: ";
	cin.getline(input, 180, '.');

	cout << "\nYou have entered: " << input << endl;

	int firstSpace = 0;
	int secondSpace = 0;

	for (int i = 0; i < strlen(input); i++)
	{
		if (input[i] == ' ' && secondSpace == 0)
			firstSpace = i;
		if (input[i] == ' ' && firstSpace != 0)
			secondSpace = i;
	}

	strncpy_s(name, input, firstSpace);
	cout << "Name: " << name << endl;

	strncpy_s(age, input + firstSpace + 1, secondSpace - firstSpace - 1);
	cout << "Age: " << age << endl;

	strncpy_s(title, input + secondSpace + 1, strlen(input) - secondSpace);
	cout << "Title: " << title << endl;


it prints out as follows:

You have entered:
enteredName enteredAge enteredTitle

Name:
enteredName
Age: enteredAge
Title: enteredTitle


but i want above output to be as follows:

You have entered: enteredName enteredAge enteredTitle

Name: enteredName
Age: enteredAge
Title: enteredTitle


i have the same problem when using regular string

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
	string input;
	string name;
	string age;
	string title;

	cout << "Enter a name, age, and title: ";
	getline(cin, input, '.');

	cout << "\nYou have entered: " << input << endl << endl;

	int firstSpace = input.find_first_of(' ');
	int secondSpace = input.find_last_of(' ');

	name = input.substr(0, firstSpace);
	age = input.substr(firstSpace + 1, secondSpace - firstSpace - 1);
	title = input.substr(secondSpace + 1, input.length() - secondSpace);

	cout << "Name: " << name << endl
		<< "Age: " << age << endl
		<< "Title: " << title << endl;
Last edited on
Can't replicate. I don't see how you are getting an extraneous newline as the first character in the string unless that's the first thing you enter as your string.

It is strange that you would use a period as the terminating character in your getline call. Normally if you are reading a line you use the newline as a terminator.

What EXACTLY is the input you are entering?
Post a complete program with a main and all includes that we can run.
Last edited on
hi dutch
thanks for your reply.

the assignment is that the user is supposed to enter their first name, age, and title in life regardless if it's the input is in the same line or in different lines (that's why the terminator is a period).

so let's say I enter the following in one line:

MyName 21 MyTitle


above input should have the same output if broken down in different lines

My Name
21 MyTitle


Which as I mentioned above should look like the following

You have entered: enteredName enteredAge enteredTitle

Name: enteredName
Age: enteredAge
Title: enteredTitle


UPDATE:
Here's the weirdest thing: when I run the codes by itself and apart from the rest of the program, it runs okay and without the extraneous newline... not sure if the error is in the other parts of the program...
Last edited on
Hello gongong ,


UPDATE:
Here's the weirdest thing: when I run the codes by itself and apart from the rest of the program, it runs okay and without the extraneous newline... not sure if the error is in the other parts of the program...


I am not sure either because you did of include the rest of the program. Errors are notorious for starting in places you do not expect.

Here are two possibilities that should work better:
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
#include <iostream>
#include <iomanip>
#include <string>


int main()
{
	std::string name;
	std::string age;
	std::string title;

	std::cout << "Enter a name, age, title: ";
	std::getline(std::cin, name, ',');

	std::getline(std::cin >> std::ws, age, ',');

	std::getline(std::cin >> std::ws, title);

	std::cout << "\n Name: " << name << '\n'
		<< " Age: " << age << '\n'
		<< " Title: " << title << std::endl;

	std::cout << "\n\n Press Enter to continue: ";
	std::cin.get();

	return 0;
}

This works for entering everything on one line and takes into account that name may have a space in it. The std::ws will eat any white space at the beginning. So as the prompt you can enter "name, age, title" or "name,age,title" and either would work.

Another possibility is to enter each variable seperately:
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 <iomanip>
#include <string>


int main()
{
	std::string name;
	std::string age;
	std::string title;

	std::cout << "Enter a name: ";
	std::getline(std::cin, name);

	//std::cout << "Enter an age: ";
	std::getline(std::cin, age,);

	//std::cout << "Enter a title: ";
	std::getline(std::cin, title);

	std::cout << "\n Name: " << name << '\n'
		<< " Age: " << age << '\n'
		<< " Title: " << title << std::endl;

	std::cout << "\n\n Press Enter to continue: ";
	std::cin.get();

	return 0;
}


Hope that helps,

Andy
Hi Andy!
thanks for your input!!!
My friend reminded me about the cin.ignore(), and it worked just okay.
is that the same as the std::ws you mentioned?
Hello gongong ,


No. "std::cin.ignore()" and "std::ws" may appear to work the same. but they are completely different.

std::cin.ignore(1000, '\n'); works on the input buffer and will ignore or remove 1000 characters from the input buffer or until it finds a "\n" whichever comes first.

std::cin >> std::ws >> aVariable; works in the input stream after you press Enter and will ignore any leading white space before you reach something that can be put in a variable be it a letter, punctuation or a number. I found http://www.cplusplus.com/reference/cctype/isspace/ which has a nice table to show what is considered a white space. And http://www.cplusplus.com/reference/cctype/isspace/ that covers "std::cin >>std::ws" although the example code may not be the best.

Hope that helps,

Andy
Topic archived. No new replies allowed.