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?

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.

This article has been dead for over six months. Start a new discussion instead.