Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,679 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,199 people online right now. Registration is fast and FREE... Join Now!




Null Character VS Space

 
Reply to this topicStart new topic

Null Character VS Space, I'm a bit confused.

ljfox4
post 16 Jul, 2007 - 04:25 PM
Post #1


New D.I.C Head

*
Joined: 10 Feb, 2007
Posts: 30


My Contributions


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

CODE

#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.
User is offlineProfile CardPM

Go to the top of the page

MorphiusFaydal
post 16 Jul, 2007 - 04:43 PM
Post #2


D.I.C Lover

Group Icon
Joined: 12 May, 2005
Posts: 1,086



Thanked 8 times

Expert In: Hardware, Networking

My Contributions


QUOTE(ljfox4 @ 16 Jul, 2007 - 05:25 PM) *

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

CODE

#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.
User is offlineProfile CardPM

Go to the top of the page

ljfox4
post 16 Jul, 2007 - 04:52 PM
Post #3


New D.I.C Head

*
Joined: 10 Feb, 2007
Posts: 30


My Contributions


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

Go to the top of the page

MorphiusFaydal
post 16 Jul, 2007 - 05:10 PM
Post #4


D.I.C Lover

Group Icon
Joined: 12 May, 2005
Posts: 1,086



Thanked 8 times

Expert In: Hardware, Networking

My Contributions


QUOTE(ljfox4 @ 16 Jul, 2007 - 05:52 PM) *

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


getline()

Example:
CODE

#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;
}


CODE
Please enter your name. >> John Doe
Hello there, John Doe!
User is offlineProfile CardPM

Go to the top of the page

ljfox4
post 16 Jul, 2007 - 05:13 PM
Post #5


New D.I.C Head

*
Joined: 10 Feb, 2007
Posts: 30


My Contributions


Alright. Thank you so much for your help. I appreciate it.
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 16 Jul, 2007 - 05:37 PM
Post #6


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,178



Thanked 33 times

Dream Kudos: 25
My Contributions


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()
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 06:38AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month