Some help for a Noobie!

This is a discussion on Some help for a Noobie! within the C Programming forums, part of the General Programming Boards category; Originally Posted by Elysia There are more elegant solutions... Well, none of them is offered here....

  1. #16
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by Elysia View Post
    There are more elegant solutions...
    Well, none of them is offered here.

  2. #17
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    Code:
    char c = getchar();
    if (is_digit(c))
    {
        c -= '0';
        // Do whatever
    }
    For example.
    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.

  3. #18
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by Elysia View Post
    Code:
    char c = getchar();
    if (is_digit(c))
    {
        c -= '0';
        // Do whatever
    }
    For example.
    But the original poster, remember? Used a switch/case for a menu. And the above solution breaks in case of UTF16 or Unicode characters. I though you were on about a universal "bullet proof" input method for numbers. But in this case we don't need numbers, so why bother?
    Last edited by Subsonics; 11-29-2009 at 07:43 AM.

  4. #19
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    I suppose the OP was, yes. So in the OP's code, it would work fine, your example. For a generic solution of getting the actual number it represents (the -48 discussion), my example is better.
    And neither handles UTF properly.
    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.

  5. #20
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by Elysia View Post
    For a generic solution of getting the actual number it represents (the -48 discussion), my example is better.
    Yes, but IMO the -48 explains better what is going on, even though it is not a good practice to follow in code for an actual program.

    Quote Originally Posted by Elysia View Post
    And neither handles UTF properly.
    But what is the point of improving upon scanf's deficit, if there are still holes in the method.

  6. #21
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    I would argue that - '0' explains it even better (how can you subtract two letters from each other?).
    To the last, then what is the point of using any method at all? Nothing is perfect. Remember that well. But there are ways to make it more perfect.
    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.

  7. #22
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Wouldn't it be possible to make a univeral input method that recognices and handles any character encoding properly? I would think so, but I bet it's not going to be elegant. ;-)

  8. #23
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    Possible? No doubt. But messy, yes.
    C++ handles these things much more smoothly.
    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.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. putting text onto the next line...lol...noobie questions
    By JOlszewski in forum C Programming
    Replies: 2
    Last Post: 01-24-2006, 03:02 PM
  2. A fourth noobie question - namespace std?
    By Noobie in forum C++ Programming
    Replies: 24
    Last Post: 08-12-2005, 02:10 PM
  3. Resource ICONs
    By gbaker in forum Windows Programming
    Replies: 4
    Last Post: 12-15-2003, 06:18 AM
  4. C++ program jumps for noobie
    By bandito9111 in forum C++ Programming
    Replies: 3
    Last Post: 03-01-2003, 12:48 AM
  5. A fifth noobie question-enum color?
    By Noobie in forum C++ Programming
    Replies: 19
    Last Post: 01-21-2003, 08:30 AM

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