Thread: Only Accepting Ints and Doubles

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,558
    Quote Originally Posted by Erik Ingvoldsen
    The test, however, asks me to input two things for some reason.
    Because now you call getline twice, discarding the input from the first call:
    Code:
    std::getline(std::cin, line);
    if (getline(std::cin, line)) {
    Quote Originally Posted by Erik Ingvoldsen
    I admit, I should have asked you from the start what these if statements were doing exactly but I was stressing over other problems with the code. But it's better that I understand what's going on, so I'd also like to ask what's happening here.
    Code:
    if (getline(std::cin, line))
    {
        std::stringstream ss(line);
        if (ss >> result && ss.eof())
        {
    First, getline is called to read a line from standard input. (I did not qualify getline here because of argument dependent lookup: since cin is in the std namespace, the compiler will search for a matching function named getline in the std namespace.) Then, the return value of getline is checked to ensure that it was successful. Next, since a line was successfully read, a string stream (i.e., input/output streams, but string based) is created from that line. We then extract/parse the line via the stringstream into result. The return value of that operation is checked, and finally we check that the end of file condition has been set on the string stream, i.e., to detect unwanted input like a stray alphabetic character after the integer input.
    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

  2. #17
    Registered User
    Join Date
    Feb 2015
    Posts
    225
    Great, it's working perfectly now! Now I need to find a way to do the same thing, but only allow doubles, not ints. If I were to change result from an int to a double, would that do the job or is it not quite as simple as that?

    I wasn't aware to having getline in an if statement would still call it, but to make sure I understand this properly: The first if statement is to ensure the getline was a success, the stringstream is needed for the parsing, and the next if statement is to ensure that the input matches the result type. Is that right?

  3. #18
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The more stringent your input needs get the more you may find it helpful to pursue other or even homemade options. What you have will work, but it is also true that input like "1" is as much a double as "1.0". To reject inputs like this, you will need to know if the string has a dot, or if it is in scientific notation first (as in "1e-3" which is 0.001). If it passes, then you convert.

  4. #19
    Registered User
    Join Date
    Feb 2015
    Posts
    225
    This function will get a floating-point number from input. The
    function returns the floating-point numberread from input.
    Note: No need to accept scientific notation.
    Error Message #1 Named invalid_double_. When input is not a double.

    When reading this, I assumed I had to get a double as a input, but now that you mention an int is technically a double, it occurs to me that this might be asking me to covert an int to a double...thankfully I don't need to worry about scientific notation.

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I say it just sounds like you should read a double from the input. Since an int is also a double, you could just read a double to accept "1", "1.0", "3.14".
    There is a std::stod to convert a string to a double (C++11 forward).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #21
    Registered User
    Join Date
    Feb 2015
    Posts
    225
    Unfortunately I'm limited to 98. Is there anything for that?

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    String streams as laserlight showed you before. Why are you limited to 98?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #23
    Registered User
    Join Date
    Feb 2015
    Posts
    225
    I wish I knew. XD The teacher hasn't explained to us why yet, but I'm assuming it has something to do with the fact that we're beginners.

    Anyway, assuming I understood the process, all I would need to do is change "result" from int to double and that would work with the string stream process, yes?

  9. #24
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Erik Ingvoldsen View Post
    I wish I knew. XD The teacher hasn't explained to us why yet, but I'm assuming it has something to do with the fact that we're beginners.
    Well, that in itself makes no sense. C++11 and later has a lot of new facilities that makes it easier for programmers.
    Anyway...

    Quote Originally Posted by Erik Ingvoldsen View Post
    Anyway, assuming I understood the process, all I would need to do is change "result" from int to double and that would work with the string stream process, yes?
    Yeah, that's the gist of it. It tells the stringstream to extract a double from the string.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #25
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,398
    Quote Originally Posted by Erik Ingvoldsen View Post
    I wish I knew. XD The teacher hasn't explained to us why yet, but I'm assuming it has something to do with the fact that we're beginners.
    It probably has more to do with the teacher not having a friggin' clue about the new language features.

    I never met an educational instructor who could come within a thousand miles of an actual developer in the field. Programming should be taught through internships and apprenticeships. The people in universities don't know what the hell they are talking about.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. accepting two inputs
    By Aratel in forum C++ Programming
    Replies: 11
    Last Post: 10-09-2012, 07:14 PM
  2. getchar() not accepting 'ans' value
    By Zaheer Attar in forum C Programming
    Replies: 4
    Last Post: 09-10-2011, 01:42 PM
  3. accepting +123 help
    By peanut in forum C Programming
    Replies: 7
    Last Post: 11-01-2006, 11:48 PM
  4. ints and doubles problem
    By iLLiCiT in forum C Programming
    Replies: 1
    Last Post: 12-04-2004, 03:42 PM
  5. Replies: 17
    Last Post: 08-03-2004, 02:16 PM