Thread: Need help getting program print out the digit in words

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I was merely repeating what was quoted. But anyway, as mentioned, the code doesn't really handle EOF.
    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.

  2. #17
    Registered User
    Join Date
    Apr 2008
    Posts
    32
    How did you learn about that in the first place?

  3. #18
    Registered User
    Join Date
    Apr 2008
    Posts
    32
    So I replaced fflush(stdin). Is this better?
    Code:
    #include <stdio.h>
    
    int main()
    {
            int number=10, ch;
            const char *numbers[] = { "Zero", "One", "Two",   "Three", "Four", 
                                      "Five", "Six", "Seven", "Eight", "Nine"  };
            printf("Input a number [0-9]: ");
            while ( !scanf("%d", &number) || !(number >= 0 && number <=9) )
            {
                    printf("Sorry input outside of valid range: Try again Please\n");
                    printf("Input a number [0-9]: ");
                    //replaced fflush(stdin);
                    while ((ch = getchar()) != '\n' && ch != EOF);
            }
            printf("You entered the number %s\n", numbers[number] );
    return 0;
    }

  4. #19
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by sillyman View Post
    How did you learn about that in the first place?
    Who is this for, and/or Learn about what?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by sillyman View Post
    So I replaced fflush(stdin). Is this better?
    Yep. No more undefined behaviour. That's a good thing.

    Quote Originally Posted by matsp View Post
    Who is this for, and/or Learn about what?
    Was wondering that myself.
    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
    Apr 2008
    Posts
    32
    Quote Originally Posted by Elysia View Post
    Yep. No more undefined behaviour. That's a good thing.


    Was wondering that myself.
    Sorry, It was for Elysia. When did you first encouter the situation?

    Perhaps, I should just skip that and feed your ego ;-)

    Thanks Elysia. You're a programming master! How the hell do you know every thing?

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by sillyman View Post
    Sorry, It was for Elysia. When did you first encouter the situation?
    What situation?

    Perhaps, I should just skip that and feed your ego ;-)
    Actually, a member on the board once quoted that for the first time long ago. I copied it into my signature and people simply keep quoting it (mostly) instead of quoting something else. Many other members on the board have been graciously thanked, as well.
    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
    Apr 2008
    Posts
    32
    The situation where fflush doesn't work as excepted. This is my first time using it and it seemed to work. What situation caused you to notice that it returned unpred results? and learn this secondary tech?

  9. #24
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by sillyman View Post
    The situation where fflush doesn't work as excepted. This is my first time using it and it seemed to work. What situation caused you to notice that it returned unpred results? and learn this secondary tech?
    I have never used fflush(stdin), and until it was pointed out to me that it actually works this way IN SOME compilers (or some C runtime libraries, to be more precise), I had no idea that there was some valid work being done by fflush(stdin) - the reason being that it's very clear in the C standard what fflush does - it flushes the OUTPUT of the buffer to the actual file or device that it's representing. This is obviously meaningless on input files, and it's a specific extension added by some compiler vendors. Other compiler vendors are not doing this, so it won't work. It's also completely possible that fflush() on an input file actually causes a crash or other unwanted behaviour.

    Edit: So there's no EXPECTATION that fflush() should work on input files - only output files.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #25
    Registered User
    Join Date
    Apr 2008
    Posts
    32
    I see. So in reality it means about as much to stdin as rewind would. thanks.

  11. #26
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Ctrl + Z... Though EOF varies... But its always been Ctrl + Z on windows.

  12. #27
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by sillyman View Post
    The situation where fflush doesn't work as excepted. This is my first time using it and it seemed to work. What situation caused you to notice that it returned unpred results? and learn this secondary tech?
    The thing is that some things work on some compilers and some not. Such as void main. On Visual Studio, the return will always be 0 if you use void main. However, on GCC it returns garbage.
    We like to call it undefined behaviour because the standard does not say there is a void main. The standard can also use a "implementation dependant" term which means the implementers are free to do it like they want.
    Like std::string is not required to put a null char at the end of the string - it's implementation dependant, so you shouldn't rely on it.
    The same goes for fflush(stdin). The standard does not mention it and its behaviour is therefore not defined, technically then, undefined.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  4. Beginners Contest #2 For those who wanted more!!
    By ILoveVectors in forum Contests Board
    Replies: 16
    Last Post: 08-12-2005, 12:03 AM
  5. why does my program print off the wrong answers?
    By kl3pt0 in forum C Programming
    Replies: 18
    Last Post: 06-10-2004, 06:52 PM