Standard Input Trouble

This is a discussion on Standard Input Trouble within the C++ Programming forums, part of the General Programming Boards category; I am having trouble reading standard input from the user. The user must enter in a floating point number followed ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    2

    Standard Input Trouble

    I am having trouble reading standard input from the user. The user must enter in a floating point number followed by a letter with no spaces between. Ex: 5b, 3c, 6t, etc. I've tried this to no avail...

    float num;
    char letter;

    cout<<"Enter your selection: ";
    cin>>num>>letter;

    It only works if there is a space between them. Anyone have a workaround to this problem?

    Thanks

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,581
    Well, the input is clearly a std::string. Use that to define the variable used to store the input and then write the necessary code to split the string into the double and char tokens.

    Edit: Other possibility is using get(). I'm not all too familiar with it though. Maybe someone can help better
    Last edited by Mario F.; 07-09-2006 at 08:54 AM.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,313
    I dont have a problem with that on this machine, but my guess is that the state of the stream is being set to failed, so the second read never happens. A solution is:

    Code:
    float num;
    char letter;
    
    cout<<"Enter your selection: ";
    cin>>num;
    cin.clear(); // clear status of stream
    cin>>letter;
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,581
    Wow.
    Didn't know I could that. Also tested the OP code and it works. I thought a space was mandatory.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,763
    It depends on what you're reading. If I wanted to extract a float and then an int, a delimiter would really be necessary.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 09:31 PM
  2. Printing Length of Input and the Limited Input
    By dnguyen1022 in forum C Programming
    Replies: 33
    Last Post: 11-29-2008, 04:13 PM
  3. EOF messing up my input stream?
    By Decrypt in forum C++ Programming
    Replies: 4
    Last Post: 09-30-2005, 04:00 PM
  4. How to Flush standard input?
    By Hardboy in forum C++ Programming
    Replies: 7
    Last Post: 02-21-2003, 03:30 PM
  5. Trouble with receiving input
    By BellosX in forum C Programming
    Replies: 4
    Last Post: 09-21-2001, 12:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21