Thread: sscanf() string version question

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    15

    sscanf() string version question

    I'm trying to get this code to read errors that are not positive integers. Right now if you type 50394e,for example, it won't catch the "e" as an error. I think the sscanf line has to change the string it's reading to only include positive intergers, but I'm not sure what to change it too. Minor help would be appreciated.

    Thanks,
    mattflick



    Code:
    #inlcude <stdio.h>
    
    int main(void)
    
    char    line[MAXLINE];
    int       error, n;
    
    do  {
          printf("Input a positive integer:   ");
          fgets(line, MAXLINE,  stdin);
          errors = sscanf(line,  "%d", &n)  ! = 1  | | n <= 0;
          if (error)
             printf("\nERROR: Do it again.\n");
    } while (error);

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    A slight modification of this:
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int mygeti(int *result)
    {
       char c, buff [ 13 ]; /* signed 32-bit value, extra room for '\n' and '\0' */
       return fgets(buff, sizeof buff, stdin) && !isspace(*buff) &&
              sscanf(buff, "%d%c", result, &c) == 2 && (c == '\n' || c == '\0') &&
              *result >= 0;
    }
    
    int main(void)
    {
       int value;
       do
       {
          fputs("Enter an integer: ", stdout);
          fflush(stdout);
       } while ( !mygeti(&value) );
       printf("value = %d\n", value);
       return 0;
    }
    Note the extra condition checking in boldface.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    15
    It was good to see it done differently just for my experience and thanks for the help.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    28
    I was working on the same thing. I tried isdigit and couldnt seem to get the logic quite right. Thanks to your advice, I solved my problem this way:

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #define MAXLINE 12
    int get_int(int *result)
    {
       char c, line [MAXLINE]; // Initialize function variables
       printf("Input a positive integer with a maximum of %d digits:  ",MAXLINE); 
       return fgets(line, MAXLINE, stdin) && !isspace(*line) && //uses fgets to return only a positive integer
              sscanf(line, "%d%c", result, &c) == 2 && (c == '\n' || c == '\0') &&
              *result >= 0;
       
    }
    
    int main(void)
    {
       int n; //Initlaizes the n variable
       
    //SELF DOCUMENTATION BEGIN
       printf("This program will take a line from the keyboard \nand print ONLY if the input is a valid positive integer.\n\n"); 
    //SELF DOCUMENTATION END
    
    while( !get_int(&n)){ // While function get_int does not equal a positive integer
    	   fputs("\nERROR: DO IT AGAIN!\n\n", stdout);//  Looping printed error message
    	   fflush(stdout);}
    
       printf("Your positive integer is: %d\n", n);
       return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM