home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!mcsun!sunic!ericom!eua.ericsson.se!euas62c36!euamts
- From: euamts@eua.ericsson.se (Mats Henricson)
- Subject: Re: How to read a string with space
- Message-ID: <1992Dec23.150200.21813@eua.ericsson.se>
- Sender: news@eua.ericsson.se
- Nntp-Posting-Host: euas62c36.eua.ericsson.se
- Reply-To: euamts@eua.ericsson.se
- Organization: Ellemtel Telecom Systems Labs, Stockholm, Sweden
- References: <1h9qmnINNlss@manuel.anu.edu.au>
- Date: Wed, 23 Dec 1992 15:02:00 GMT
- Lines: 46
-
- In article 1h9qmnINNlss@manuel.anu.edu.au, cxd653@huxley.anu.edu.au (Chunping Ding) writes:
- #
- #Could anyone tell me how to read a string with space? Thank you.
- #
- #I wrote a progam as following:
- #
- ##include<iostream.h>
- #
- #main()
- #{
- # char name[20];
- # cout<< "Enter a name ...";
- # cin >> name;
- # cout << "The name you entered is "<<name<<'\n';
- #}
- #
- #When I run this program and answer the prompt:
- #Enter a name ...
- #with:
- #Ding Chunping
- #I got an output from the computer:
- #The name you entered is Ding
- #
- #The computer ignores the part of the string, Chunping, after the space.
- #How can I solve the problem?
-
- You can fix this by using the getline function:
-
- main()
- {
- char name[20];
- cout << "Enter a name ...";
- cin.getline( name, 20 );
- cout << "The name you entered is " << name << endl;
- }
-
- The function getline for istream is declared like this in the file iostream.h:
- istream& getline(char* b, int lim, char delim='\n');
-
- Input is tricky...
-
- Mats Henricson
- Ellemtel
- Stockholm
- Sweden
-
-