Plz Help B4 I Commit Suicide

This is a discussion on Plz Help B4 I Commit Suicide within the C Programming forums, part of the General Programming Boards category; heya all i have absolute no idea how to put an input error code in my program im required to ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    45

    Exclamation Plz Help B4 I Commit Suicide

    heya all
    i have absolute no idea how to put an input error code in my program
    im required to print a input error statement if two integers were not entered
    this input error code need to work if
    1. the stuff entered were not numbers
    2. the stuff entered were garbage
    3. the stuff entered were english
    Well 2 and 3 is basically the same

    i have no idea about doing this except using scanf and return value
    but i have no idea how to start
    please post an examples or hints please....

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    isXXX() will help you (from ctype.h).

    If you still need help, post your attempt.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    45
    i dont know how to start tho

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Start by reading in a string [this allows YOU to control what happens, rather than the C runtime library].

    Once you have a string, you can use for example strtol() to try to make it into a number, and check the end-pointer.

    --
    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. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    45
    i just learned really basic stuff
    so wahtz a strtol() and end pointer?

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    Have a look at the documentation for strtol. It tells you really neat stuff, you know.
    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. #7
    Banned
    Join Date
    Nov 2007
    Posts
    678
    zacs has given you simple and most effective idea, you will do your own checking for data, and thus learn to do it well. it is also more generic solution. strtol is a quick fix kinda.
    zacs is saying use for example, isdigit to check if string consists only numberss.
    good luck!

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    Have a look at the documentation for strtol. It tells you really neat stuff, you know.
    If you google for "man strtol" (linux variant) or "MSDN strtol" (Windows variant) you should get a hit for the relevant function.

    --
    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.

  9. #9
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    I'd use strtol. What happens if the number is negative? Or if the number has a decimal point? With those considerations accounted for you would still need to invalidate instances such as:

    -.

    or

    34-4..5

    strtol will also ignore leading whitespace, which is nice. The only problem with it is that it will convert a number followed by non-digit characters, something to watch out for. The resulting number and end pointer position can be used to vaidate this tho.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by manav View Post
    zacs has given you simple and most effective idea, you will do your own checking for data, and thus learn to do it well. it is also more generic solution. strtol is a quick fix kinda.
    zacs is saying use for example, isdigit to check if string consists only numberss.
    good luck!
    Yes, but writing a COMPLETE check that covers all sorts of different options (e.g. number starts with + or -, number is overflowing) will require that you essentially implement all of the code in strtol(), and for it to be efficient, it will have to be written in a better way than strtol() - and I have written several "strtol" like functions, and I know how hard it is to cover all the options and permutations.

    And it's not hard to use strtol() - it's one call with three parameters, and you get to check the return value and the endptr afterwards to see if it went OK or not. Hardly rocket science.

    It is of course more educational to write your own function, but hardly more efficient in amount of effort [assuming you don't want to just cover the obvious cases], and it's unlikely that it will be more efficient in performance - not that it usually matters anyways.

    --
    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.

  11. #11
    Banned
    Join Date
    Nov 2007
    Posts
    678
    just a quick note here, i don't wish to stray the topic:
    strtol just has to parse integers, no float parsing needed.
    i wrote my function for checking integers in under 5 lines. not difficult.

  12. #12
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Yeah but strtol wont barf when it read 10.0 which is an integer

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by manav View Post
    i wrote my function for checking integers in under 5 lines. not difficult.
    And is that complete, as in handles 112378923123971239847912 correctly [as in, it returns a defined value and/or gives an error - as that number will not fit in an integer (probably not even a long long)], negative numbers, numbers starting with + or whitespace?

    Yes, I can write a function in (probably less than) 5 lines that does trivial parsing of numbers - but strtol() is more flexible than what I can write in 5 lines. But even without overflow handling, dealing with sign and spaces pushes my code to about 10-12 lines [without counting blank lines between blocks].

    --
    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.

  14. #14
    Banned
    Join Date
    Nov 2007
    Posts
    678
    incomplete example, i hope does not violate forum rules, checks for valid ints, no space trimming:
    Code:
    int isint(char *p)
    {
    	char* q = p;
    	if (*q == '+' || *q == '-') q++;
    	while (*q)
    	{
    		if (! isdigit(*q)) return 0;
    		q++;
    	}
    	if ((q-p) > 10) return 0;
    	return 1;
    }

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Your code is fine for just matching an integer, but presumably the original poster also wants to convert it. Which means that the code dealing with plus or minus will be a bit more complex, and your check for 10 digits is not a complete check against invalid input, e.g:
    -1111111111 is a valid integer, but uses 11 positions.
    9999999999 is an invalid integer, but uses 10 positions (so passes according to your check).

    Not to mention superfluous zeros.

    --
    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.

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

Similar Threads

  1. can any1 plz make this assignment
    By jean in forum C Programming
    Replies: 17
    Last Post: 05-13-2009, 09:19 PM
  2. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  3. Poem: Suicide Note
    By KingZoolerius66 in forum A Brief History of Cprogramming.com
    Replies: 54
    Last Post: 12-31-2003, 04:39 PM
  4. Training children to commit suicide.
    By Liger86 in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 02-06-2003, 08:59 PM
  5. Suicide
    By Series X4 1.0 in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 10-17-2001, 02:26 PM

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