how to input string with white space characters

how to input string with white space characters

Page 1 of 1

5 Replies - 23069 Views - Last Post: 14 April 2010 - 03:49 PM Rate Topic: -----

#1 123@123.in  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 30-September 09

how to input string with white space characters

Posted 30 September 2009 - 07:16 AM

how to input string with white space characters
Is This A Good Question/Topic? 0
  • +

Replies To: how to input string with white space characters

#2 Ancient Dragon  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 81
  • View blog
  • Posts: 679
  • Joined: 19-July 09

Re: how to input string with white space characters

Posted 30 September 2009 - 07:19 AM

std::string line;
getline(cin, line);


Was This Post Helpful? 0
  • +
  • -

#3 UG Cyber  Icon User is offline

  • D.I.C Addict

Reputation: 32
  • View blog
  • Posts: 546
  • Joined: 24-July 08

Re: how to input string with white space characters

Posted 30 September 2009 - 08:24 AM

View PostAncient Dragon, on 30 Sep, 2009 - 06:19 AM, said:

std::string line;
getline(cin, line);


ummm......
#include <iostream> //header required for basic input / output
#include <string.h> //header required for string

using namespace std; //to avoid std::

int main(int argc, char * argv[])
{
   string line;  //create the string
   cin.getline(line, 2048); /*this will accept any character and stop at a new line or 2048 bytes, which ever comes first*/
   cout << "Input: " << string << endl;
   return 0;
}



That should do the trick

please note that there can be 3 args to the function cin.getline(); The first being the array/string/buffer. the second being the total size allowed for the buffer. and the third is for the escape character, the default is "\n" or new line.

Happy coding!

This post has been edited by UG Cyber: 30 September 2009 - 08:26 AM

Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6036
  • View blog
  • Posts: 23,421
  • Joined: 23-August 08

Re: how to input string with white space characters

Posted 30 September 2009 - 08:48 AM

Sorry, UGCyber, but this #include <string.h> //header required for string is wrong. That's the C header for C string functions. I think you mean #include <string>.
Was This Post Helpful? 0
  • +
  • -

#5 Ancient Dragon  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 81
  • View blog
  • Posts: 679
  • Joined: 19-July 09

Re: how to input string with white space characters

Posted 30 September 2009 - 10:04 AM

View PostUG Cyber, on 30 Sep, 2009 - 07:24 AM, said:

ummm......


I assumed he was smart enough to know the rest. But maybe I underestimated his intelligence.
Was This Post Helpful? 0
  • +
  • -

#6 sobat  Icon User is offline

  • New D.I.C Head

Reputation: -7
  • View blog
  • Posts: 39
  • Joined: 21-January 10

Re: how to input string with white space characters

Posted 14 April 2010 - 03:49 PM

its giving error! I want to input two strings with spaces!

#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char a[15],b[15];
cout<<"Enter a name:";
gets(a);
cout<<"Enter a string:";
gets(B);
getch();
}
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1