Results 1 to 9 of 9

Thread: Validating Integer Input - ansi C

  1. #1
    Theoretical Element Spud1's Avatar
    Join Date
    Jul 2003
    Location
    North West
    Posts
    7,494
    Thanks
    335
    Thanked
    313 times in 249 posts
    • Spud1's system
      • Motherboard:
      • Gigabyte Aorus Master
      • CPU:
      • 9900k
      • Memory:
      • 16GB GSkill Trident Z
      • Storage:
      • Lots.
      • Graphics card(s):
      • RTX3090
      • PSU:
      • 750w
      • Case:
      • BeQuiet Dark Base Pro rev.2
      • Operating System:
      • Windows 10
      • Monitor(s):
      • Asus PG35VQ
      • Internet:
      • 910/100mb Fibre

    Validating Integer Input - ansi C

    Pretty simple problem here i rekon, but I cant figure it out =0

    if i promt the user for an integer with something like:

    Code:
     
    printf("please input an integer");
    scanf("%d", &intOpt1);
    how can I make certain that an integer is input, and not a char, string, double etc. My problem is that in my program, if some1 enters a character then it crashes - going into an infinite loop. Any ideas ?

  2. #2
    Senior Member
    Join Date
    Oct 2003
    Posts
    2,069
    Thanks
    4
    Thanked
    7 times in 3 posts
    doesnt the %d specify that your goign to be storing it as a number anyway?
    but then it will just store it but as crap...
    no ok...thats completely useless info...lol
    how about changing the printf line to "Enter a number or this will screw up and it will be all your fault!"
    Twigman

  3. #3
    Theoretical Element Spud1's Avatar
    Join Date
    Jul 2003
    Location
    North West
    Posts
    7,494
    Thanks
    335
    Thanked
    313 times in 249 posts
    • Spud1's system
      • Motherboard:
      • Gigabyte Aorus Master
      • CPU:
      • 9900k
      • Memory:
      • 16GB GSkill Trident Z
      • Storage:
      • Lots.
      • Graphics card(s):
      • RTX3090
      • PSU:
      • 750w
      • Case:
      • BeQuiet Dark Base Pro rev.2
      • Operating System:
      • Windows 10
      • Monitor(s):
      • Asus PG35VQ
      • Internet:
      • 910/100mb Fibre
    lol =0

    yeah it stores it as crap hence why it makes the program fubar

    ;/ I know theres a way to do it, just cant remember

  4. #4
    Large Member
    Join Date
    Apr 2004
    Posts
    3,720
    Thanks
    47
    Thanked
    99 times in 64 posts
    I suggest you get a book on C and learn it. There really is a whole host of possible ways it can be done, and there is no particular sure and fast way of doing it.

    I would recommend against using scanf at every possible moment.


    char str_y[3];
    int y;

    do
    {
    cprintf("Enter number between 1-15");
    gets(str_y);
    y = atoi(str_y);
    fflush(stdin);
    if(y < 1 || y > 15)
    cprintf("Enter a value in the specified range sonny");
    }
    while (y < 1 || y > 15);
    Last edited by yamangman; 23-10-2004 at 11:33 PM.
    To err is human. To really foul things up ... you need a computer.

  5. #5
    Senior Member
    Join Date
    Jul 2003
    Location
    ZA ✈ UK
    Posts
    622
    Thanks
    0
    Thanked
    0 times in 0 posts
    My lack of C skill showing, but how would atoi handle non-numeric characters in your example there?

  6. #6
    Large Member
    Join Date
    Apr 2004
    Posts
    3,720
    Thanks
    47
    Thanked
    99 times in 64 posts
    Your absolutely right, between 'gets' and 'ASCII to Int' is where you would place the validation, you don't want me to do it for him do ya?
    To err is human. To really foul things up ... you need a computer.

  7. #7
    Senior Member
    Join Date
    Jul 2003
    Location
    ZA ✈ UK
    Posts
    622
    Thanks
    0
    Thanked
    0 times in 0 posts
    Alternatively, one could just loop through the character array and subtract whatever the ASCII value of 0 is (Assuming that comes first - I forget), then test if it it's < 0 or > 9.

  8. #8
    Large Member
    Join Date
    Apr 2004
    Posts
    3,720
    Thanks
    47
    Thanked
    99 times in 64 posts
    Or use the function strchr() or possibly strpbrk() and simply locate any non numeric values within the string before using atoi.

    I believe strchr is ANSI.
    To err is human. To really foul things up ... you need a computer.

  9. #9
    Senior Member
    Join Date
    Oct 2003
    Posts
    2,069
    Thanks
    4
    Thanked
    7 times in 3 posts
    scanf("%d", a=0)....check to make sure it equals 0 or something...

    use scanf...its easyish (for noobs like me).
    Twigman

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Sound input issues on K8V mobo
    By StarkMjolk in forum Software
    Replies: 0
    Last Post: 11-04-2004, 08:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •