Thread: Validate positives

  1. #1
    Registered User
    Join Date
    Sep 2017
    Posts
    3

    Validate positives

    hi everyone i'm programming validation of a number that only accepts positive integers the code below is for only integers
    Code:
    • #include<ctype.h>
    • #include<stdio.h>
    • int main(void)
    • {
    • char buf[BUFSIZ];
    • int value, end;
    • printf("Enter an integer: ");
    • fflush(stdout);
    • while(fgets(buf,sizeof buf, stdin)== NULL ||
    • sscanf(buf,"%d%n",&value,&end)!=1||
    • !isspace(buf[end]))
    • {
    • printf("Invalid integer. Please try again: ");
    • fflush(stdout);
    • }
    • printf("You entered %d\n", value);
    • return0;
    • }
    But when i try to add it the validation of positives
    Code:
    
    
    • #include<ctype.h>
    • #include<stdio.h>
    • int main(void)
    • {
    • char buf[BUFSIZ];
    • int value, end;
    • printf("Enter an integer: ");
    • fflush(stdout);
    • while(value<0) { printf("Only positives numbers. Please try again: "); fflush(stdout); }
    • while(fgets(buf,sizeof buf, stdin)== NULL ||
    • sscanf(buf,"%d%n",&value,&end)!=1||
    • !isspace(buf[end]))
    • {
    • printf("Invalid integer. Please try again: ");
    • fflush(stdout);
    • }
    • printf("You entered %d\n", value);
    • return0;
    • }
    And when i compile ignores the validation of only positives. Help please.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Because you didn't put your test in the same while condition as everything else.

    Try
    Code:
    while(fgets(buf,sizeof buf, stdin)== NULL ||
               sscanf(buf,"%d%n",&value,&end)!=1||
               !isspace(buf[end]) ||
               value < 0)
    Next time, make sure you post only code between code tags. No tables / formatting / fonts / colours.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2017
    Posts
    3
    i didn't put it because i want different messages, one message for only positives and another one for only integers

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    So put the test inside the loop then
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Sep 2017
    Posts
    3
    You mean put the (while<0) inside the other? i already do that, and the problem is when i enter a float number validates it then i enter a negative and doesn't validate it

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by lifeless View Post
    You mean put the (while<0) inside the other? i already do that, and the problem is when i enter a float number validates it then i enter a negative and doesn't validate it
    No Salem means move your condition from while into loop body

    Code:
    while(fgets(buf,sizeof buf, stdin)== NULL ||
               sscanf(buf,"%d%n",&value,&end)!=1||
               !isspace(buf[end]))
        if(value < 0)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do you validate sscanf?
    By MSF1981 in forum C Programming
    Replies: 2
    Last Post: 04-15-2009, 04:29 PM
  2. Validate input value
    By aladin in forum C Programming
    Replies: 1
    Last Post: 03-21-2009, 09:41 AM
  3. How do I validate these inputs?
    By Kyeong in forum C Programming
    Replies: 1
    Last Post: 10-19-2008, 02:20 PM
  4. AVG Free 8.0 and false positives
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 08-14-2008, 07:33 AM
  5. How to Validate an Input
    By slowcoder in forum C Programming
    Replies: 12
    Last Post: 05-17-2007, 07:33 PM

Tags for this Thread