Null Character VS Space

I'm a bit confused.

Page 1 of 1

5 Replies - 1369 Views - Last Post: 16 July 2007 - 06:37 PM Rate Topic: -----

#1 ljfox4  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 67
  • Joined: 10-February 07

Null Character VS Space

Posted 16 July 2007 - 05:25 PM

I've been programming for years and I still tend to wonder about something. First, an example:

#include <iostream>

using namespace std;

int main()
{
	char name[32] = {'\0'};
	cout<<"Enter your name\n";
	cin>>name;
	cout<<"Hello, "<<name<<".\n";
	return 1;
}



I know it is lacking code to prevent overflowing the char buffer, but that's not the point. I'm curious as to why if I enter "John Doe", then the final result displays "Hello, John."

I've always been under the impression that a NULL character and only a NULL character terminates a NULL-terminated string. As far as I have known, a space is just another character.

That's been bugging me for a while, so please, any and all replies are appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Null Character VS Space

#2 MorphiusFaydal  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 43
  • View blog
  • Posts: 1,376
  • Joined: 12-May 05

Re: Null Character VS Space

Posted 16 July 2007 - 05:43 PM

View Postljfox4, on 16 Jul, 2007 - 05:25 PM, said:

I've been programming for years and I still tend to wonder about something. First, an example:

#include <iostream>

using namespace std;

int main()
{
	char name[32] = {'\0'};
	cout<<"Enter your name\n";
	cin>>name;
	cout<<"Hello, "<<name<<".\n";
	return 1;
}



I know it is lacking code to prevent overflowing the char buffer, but that's not the point. I'm curious as to why if I enter "John Doe", then the final result displays "Hello, John."

I've always been under the impression that a NULL character and only a NULL character terminates a NULL-terminated string. As far as I have known, a space is just another character.

That's been bugging me for a while, so please, any and all replies are appreciated.


The string itself may be NULL-terminated, but the "cin" function cuts off at a space or a newline.
Was This Post Helpful? 0
  • +
  • -

#3 ljfox4  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 67
  • Joined: 10-February 07

Re: Null Character VS Space

Posted 16 July 2007 - 05:52 PM

Oh alright, thank you. And what can I use as an alternative to cin?
Was This Post Helpful? 0
  • +
  • -

#4 MorphiusFaydal  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 43
  • View blog
  • Posts: 1,376
  • Joined: 12-May 05

Re: Null Character VS Space

Posted 16 July 2007 - 06:10 PM

View Postljfox4, on 16 Jul, 2007 - 05:52 PM, said:

Oh alright, thank you. And what can I use as an alternative to cin?


getline()

Example:
#include <iostream>
#include <string>
using namespace std;

void main() {
	string name;

	cout << "Please enter your name. >> ";
	getline(cin, name);

	cout << "Hello there, " << name << "!" << endl;

	return 0;
}


Please enter your name. >> John Doe
Hello there, John Doe!

Was This Post Helpful? 0
  • +
  • -

#5 ljfox4  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 67
  • Joined: 10-February 07

Re: Null Character VS Space

Posted 16 July 2007 - 06:13 PM

Alright. Thank you so much for your help. I appreciate it.
Was This Post Helpful? 0
  • +
  • -

#6 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Null Character VS Space

Posted 16 July 2007 - 06:37 PM

As long as you're using cin, you may as well use the getline() method of cin, as opposed to the standalone function:
cin.getline()
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1