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
    Angola
    Posts
    7,974
    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 07:54 AM.
    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
    23,817
    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;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    Angola
    Posts
    7,974
    Wow.
    Didn't know I could that. Also tested the OP code and it works. I thought a space was mandatory.
    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
    Chi! whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    8,221
    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, 08:31 PM
  2. Printing Length of Input and the Limited Input
    By dnguyen1022 in forum C Programming
    Replies: 33
    Last Post: 11-29-2008, 03:13 PM
  3. EOF messing up my input stream?
    By Decrypt in forum C++ Programming
    Replies: 4
    Last Post: 09-30-2005, 03:00 PM
  4. How to Flush standard input?
    By Hardboy in forum C++ Programming
    Replies: 7
    Last Post: 02-21-2003, 02:30 PM
  5. Trouble with receiving input
    By BellosX in forum C Programming
    Replies: 4
    Last Post: 09-20-2001, 11:58 PM

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