home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / cplus / 18345 < prev    next >
Encoding:
Text File  |  1992-12-23  |  1.5 KB  |  60 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!mcsun!sunic!ericom!eua.ericsson.se!euas62c36!euamts
  3. From: euamts@eua.ericsson.se (Mats Henricson)
  4. Subject: Re: How to read a string with space
  5. Message-ID: <1992Dec23.150200.21813@eua.ericsson.se>
  6. Sender: news@eua.ericsson.se
  7. Nntp-Posting-Host: euas62c36.eua.ericsson.se
  8. Reply-To: euamts@eua.ericsson.se
  9. Organization: Ellemtel Telecom Systems Labs, Stockholm, Sweden
  10. References: <1h9qmnINNlss@manuel.anu.edu.au>
  11. Date: Wed, 23 Dec 1992 15:02:00 GMT
  12. Lines: 46
  13.  
  14. In article 1h9qmnINNlss@manuel.anu.edu.au, cxd653@huxley.anu.edu.au (Chunping Ding) writes:
  15. #
  16. #Could anyone tell me how to read a string with space? Thank you.
  17. #
  18. #I wrote a progam as following:
  19. #
  20. ##include<iostream.h>
  21. #
  22. #main()
  23. #{
  24. #  char name[20];
  25. #  cout<< "Enter a name ...";
  26. #  cin >> name;
  27. #  cout << "The name you entered is "<<name<<'\n';
  28. #}
  29. #
  30. #When I run this program and answer the prompt:
  31. #Enter a name ...
  32. #with:
  33. #Ding Chunping
  34. #I got an output from the computer:
  35. #The name you entered is Ding
  36. #
  37. #The computer ignores the part of the string, Chunping, after the space.
  38. #How can I solve the problem?
  39.  
  40. You can fix this by using the getline function:
  41.  
  42. main()
  43. {
  44.    char name[20];
  45.    cout << "Enter a name ...";
  46.    cin.getline( name, 20 );
  47.    cout << "The name you entered is " << name << endl;
  48. }
  49.  
  50. The function getline for istream is declared like this in the file iostream.h:
  51.     istream&    getline(char* b, int lim, char delim='\n');
  52.  
  53. Input is tricky...
  54.  
  55. Mats Henricson
  56. Ellemtel
  57. Stockholm
  58. Sweden
  59.  
  60.