Results 1 to 5 of 5

Thread: cin >> char with a space...

  1. #1

    Thread Starter
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339

    cin >> char with a space...

    What is up with space:

    char pName[255];
    cin >> pName;

    "Input: Word then space";

    string strName = pName;
    cout << strName <<endl;

    "Output: Word"



    So I geuss, what is the correct way to accept user input...
    Last edited by Halsafar; Jul 22nd, 2005 at 06:31 PM.
    "From what was there, and was meant to be, but not of that was faded away." - - Steve Damm

    "The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm

    "When you do things right, people won't be sure if you did anything at all." - - God from Futurama

  2. #2
    Lively Member
    Join Date
    Jan 2004
    Posts
    82

    Re: cin << char with a space...

    Cin reads until the first whitespace.

    Try this:

    Code:
    int main()
    {
    	char pName[256];
    	cin.getline( pName, 256 );
    
    	cout << pName << endl;
    
    }

  3. #3

    Thread Starter
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339

    Re: cin << char with a space...

    Thats not working....
    What you posted just tries to get the line which is not being inputted...
    If I use cin then what you posted the first like 6 letters are always cut out...
    "From what was there, and was meant to be, but not of that was faded away." - - Steve Damm

    "The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm

    "When you do things right, people won't be sure if you did anything at all." - - God from Futurama

  4. #4
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403

    Re: cin >> char with a space...

    How about this:

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
       string buffer;
       getline(cin, buffer);
    }
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  5. #5
    Addicted Member BobTheBuilder.'s Avatar
    Join Date
    Apr 2005
    Location
    Ohio
    Posts
    149

    Re: cin >> char with a space...

    It is always a good reason to flush the buffer before using a cin.getline()...

    So try doing this and see if it helps...

    Code:
    cin.flush()

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width