Thread: validating input

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    2

    validating input

    hello,

    i've tried looking at the FAQs etc. on this topic, but my knowledge of C is very, very small. My problem is that i'm trying to make a program that requests the user to put in a positive integer, but anything other than a positive integer asks the user to try again.

    This is what i've done so far:

    Code:
    #include <stdio.h> 
    
    void main()
    {
    int i, j;
    printf("Please enter a positive integer:");
    scanf("%d", &i);
    
    while (i<=0){printf("\nYou have not entered a positive integer.\n\n");
                 printf("Please enter a positive integer:");
                 scanf("%d", &i);}
    j=i;
    for (i=0; i<j; i++){printf("It Worked!\n");}
    
    }
    So far it won't take negative numbers, but if i put in a character it loops the for statement (sorry if thats the wrong terminology)

    How do i get it to request the user to re-enter a positive integer if they had input a character the first time?

    thanks for any help

  2. #2
    Registered User Markallen85's Avatar
    Join Date
    Nov 2002
    Posts
    53
    I'd go with something like:

    Code:
    int i=-1;
    char string[256];
    while (i<=0)
      {
      printf("please enter a positive interger\n");
      scanf("%s",&string);
      
      i=atoi(string);
      }
    
    //i is now your integer
    that'll not count 0 as a valid value, if you do want 0 to be valid, use while (1<0)

    and you'll probably pick up a few semi-flames for using a void main, it's much better to use:

    int main ()
    {
    return 0;
    }

    -mark
    "never argue with an idiot, they will drag you down to their level and beat you with experience"

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    2

    thanks for the reply

    thanks Mark

    i tried using it, but when i try to compile i get an error saying:

    implicit declaration of function `int atoi(...)'

    and unfortunately i have no idea what that means.

  4. #4
    Registered User Markallen85's Avatar
    Join Date
    Nov 2002
    Posts
    53
    I think atoi is part of stdlib.h, it's not in stdio.h
    put the line

    #include <stdlib.h>

    so the whole code should be:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
      int i=-1;
      char string[256];
      while (i<=0)
        {
        printf("please enter a positive interger\n");
        scanf("%s",&string);
        
        i=atoi(string);
        }
      //i is now your integer
      return 0;
    }
    just tested that and it compiles fine on dev-c++
    "never argue with an idiot, they will drag you down to their level and beat you with experience"

  5. #5
    Registered User
    Join Date
    Dec 2002
    Posts
    18
    include #include<stdlib.h> at the top of the program

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>scanf("%s",&string);
    This is wrong, it should be
    >>scanf("%s",string);

    atoi() isn't a good function to use, due to its lack of error checking and undefined behaviour.

    >>void main()
    This is also wrong, and is covered in the FAQ.

    Read
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    http://faq.cprogramming.com/cgi-bin/...&id=1043284376
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User char's Avatar
    Join Date
    Apr 2002
    Posts
    31
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        int i, ch;
    
        printf("Enter a positive intiger\n");
    
        while(scanf("%d", &i) != 1 || i < 0)
        {
    	printf("That was not a positive intiger. Try again!\n");
    
    	while((ch = getchar()) != EOF && ch != '\n');
        }
    
        return EXIT_SUCCESS;
    }

  8. #8
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Use fgets( ) and sscanf( ).
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Neither sscanf() or scanf() will help the OP achieve their goal. Look:
    make a program that requests the user to put in a positive integer, but anything other than a positive integer asks the user to try again.
    Using scanf() will not let you see the error if the user enters a number such as 999hhh, as the 999 will be placed into the int variable, and hhh will be left in the input stream, unseen by the program. Again, all this is covered in the FAQ.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Hammer, every time I use your links to look at a FAQ I get a 'table of contents' for the FAQs -- no matter what link you give. Links given to eskimo work fine.

    Any ideas why?
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  11. #11
    Registered User
    Join Date
    Jul 2003
    Posts
    61
    Faq links work fine for me (in Mozilla/IE6). What browser are you using?
    $ENV: FreeBSD, gcc, emacs

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by WaltP
    Hammer, every time I use your links to look at a FAQ I get a 'table of contents' for the FAQs -- no matter what link you give. Links given to eskimo work fine.

    Any ideas why?
    Strange, I don't get that, and I haven't had any other complaints. Maybe its the Anti-FAQ Movement trying to passing on another subtle message PM with the details of the problem if you still have it.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  13. #13
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Works fine for me (IE6, Opera 7).
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  14. #14
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by XSquared
    Works fine for me (IE6, Opera 7).
    AHaaaa, in IE it does work (unfortunately). NS7 it is not working.

    the link locations does say http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    but the index is displayed. Looks like the smartfaq.cgi program is a little dumb when in comes to NS/Mozilla browsers.

    And before it's suggested, no, using IE is not an appropriate option... Only NS and Mozilla has the "open in new window" with one click of the mouse wheel. IE/Opera won't open new window's in one click, which is important to me.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by WaltP
    Only NS and Mozilla has the "open in new window" with one click of the mouse wheel. IE/Opera won't open new window's in one click, which is important to me.
    Why don't you just use tabs in Mozilla? Who uses new windows? That's so 1995.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input class project (again)
    By Elysia in forum C++ Programming
    Replies: 41
    Last Post: 02-13-2009, 10:52 AM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  3. Structure and Linked List User Input Question
    By kevndale79 in forum C Programming
    Replies: 16
    Last Post: 10-05-2006, 11:09 AM
  4. Validating Monetary Input
    By Hexxx in forum C++ Programming
    Replies: 7
    Last Post: 02-01-2006, 08:27 AM
  5. need help with some input
    By blindleaf in forum C Programming
    Replies: 2
    Last Post: 03-16-2003, 01:50 PM