ok, so I have the following code.

#include <iostream>
using namespace std;

int main()
{
	char name[80];
	cout << "\n\nEnter your name: ";
	cin >> name;
	cout << name;
	return 0;
}

but here's the problem. whenever I enter a space in the name, everything after the space doesn't appear in the shell. so is looks like this.

Enter your name: John Appleseed
John

[process complete]
returned with a value of 0

so after think about this for about the past hour I came to the conclusion that the sequence ends when is sees a null character. thinking to myself that a space might be viewed as a null character... I wrote the following code to try and fix it.

#include <iostream>
using namespace std;

int main()
{
	int a;
	char name[80];
	cout << "\n\nEnter your name: ";
	cin >> name;
	for (a=0; a<80; a++)
	{
		if ((name[a] == '\0') && (name[a+1] == '\0'))
			break;
		else if ((name[a] == '\0') && (name[a+1] != '\0'))
			name[a] = ' ';
		else 
			continue;
	}
		
	cout << name;
	return 0;
}

still with no luck...

any suggestions?

Recommended Answers

All 5 Replies

Heh. No, a space is not a null. The space delimited input you are using is delimited by spaces. Use getline if you want to get a line.

when you use cin >> , it reads until it sees a space. Do this :

char firstName[100] = {0};
char lastName[100] = {0};
cin >> firstName >> lastName;

Thats the easy solution and error prone. The better solution is the use strings.

string firstName, lastName;
cin >> firstName >> lastName;

Even better do this :

string fullName;
getline(cin, fullName); //read the whole name

thanks firsPrerson that helped :)

I could use a strung...

but that defeats the purpose of learnung how to use character sequences XD

@Dave Sinkula

getline only works for strings.

@Dave Sinkula

getline only works for strings.

Are you sure about that?

commented: Danke. +13
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.