Need help with sscanf

This is a discussion on Need help with sscanf within the C Programming forums, part of the General Programming Boards category; I can get the following code to error out when I put anything other than a number first, but if ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    4

    Need help with sscanf

    I can get the following code to error out when I put anything other than a number first, but if I input numbers first followed by a char it won't error out. For example: f431 causes an error but 431f will not. Should I attempt to use fscanf and if I should, how would I go about doing so/

    Code:
    #include <stdio.h> 
     
    
    int check(char *line);
    char line[MAXLINE];
    int error, n, digit;
    int i = 0;
    int flag = 0;
    
    
    void main(){ 
    	
    	do{
    	         printf("Input a positive integer: ");
    	         if (fgets(line, MAXLINE, stdin) != NULL){
    	         digit = check(&line[0]);
    	         error = ((sscanf(line," %d", &n) != 1 || n <= 0) && (digit == 1));  
    		}
    		if (error)
    			printf("\nERROR: Do it again .\n");
    		else 
    			printf("There was no error!");
    	}while (error);
    }
    
    
    int check(char *line){ 
    	while((i < MAXLINE) && (flag == 0)){
    		if (isalpha((int)line[i]))
    	      flag = 1;
    	    ++i;
    	}
    	if (flag == 0)
    		return 0;
    	else
    		return 1;
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Last edited by Dave_Sinkula; 08-08-2005 at 02:41 PM. Reason: Added link to Jack Klein's "Bullet Proof Integer Input Using strtol()".
    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
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by dgs414
    void main(){
    Where ever you learned that was incorrect in its teaching. The main function always returns an int. There's a FAQ entry on it if you need more information on why it's wrong and the correct way to do it.


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

  4. #4
    Destoryer of Worlds
    Join Date
    Jul 2005
    Posts
    17
    wow talk about unreadablity


    the whold error thing is messin me up

    what is your while statement checking for?


    ill continue to try and understand what you were doin here but if i
    cannot understand your intentions here then i cannot help. Sorry
    if im sounding harsh but maybe it would help if you added comments.

  5. #5
    Destoryer of Worlds
    Join Date
    Jul 2005
    Posts
    17
    ok i think i have it tracked down here


    Code:
     
    error = ((sscanf(line," %d", &n) != 1 || n <= 0) && (digit == 1));
    this error checking will not work if both a letter and a number are enterd in whatever combination.

    sscanf does not return 0 on false and 1 on true it returns the number of varibles found so in essence its always true as long is there is input.
    Last edited by exvor; 08-08-2005 at 07:39 PM.

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    Quote Originally Posted by exvor
    sscanf does not return 0 on false and 1 on true it returns the number of varibles found so in essence its always true as long is there is input.
    That's not true. Look at the following.

    Code:
    itsme@itsme:~/C$ cat sscanfexample.c
    #include <stdio.h>
    
    int main(void)
    {
      int rv;
      int n;
    
      rv = sscanf("c1", "%d", &n);
      printf("%d\n", rv);
      rv = sscanf("1c", "%d", &n);
      printf("%d\n", rv);
    
      return 0;
    }
    Code:
    itsme@itsme:~/C$ ./sscanfexample
    0
    1
    itsme@itsme:~/C$
    If you understand what you're doing, you're not learning anything.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by exvor
    sscanf does not return 0 on false and 1 on true it returns the number of varibles found so in essence its always true as long is there is input.
    Actually, it returns the number of assignments. To quote the man page:
    These functions return the number of input items assigned, which can be
    fewer than provided for, or even zero, in the event of a matching fail-
    ure. Zero indicates that, while there was input available, no conver-
    sions were assigned
    ; typically this is due to an invalid input charac-
    ter, such as an alphabetic character for a `%d' conversion. The value
    EOF is returned if an input failure occurs before any conversion such
    as an end-of-file occurs. If an error or end-of-file occurs after con-
    version has begun, the number of conversions which were successfully
    completed is returned.
    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by dgs414
    I can get the following code to error out when I put anything other than a number first, but if I input numbers first followed by a char it won't error out. For example: f431 causes an error but 431f will not. Should I attempt to use fscanf and if I should, how would I go about doing so/
    Read a Number from the User, Part 2
    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.*

  9. #9
    Destoryer of Worlds
    Join Date
    Jul 2005
    Posts
    17
    Sorry man page is kinda clusterd with scanf and sscanf and fscanf

    so I just checked it real fast. when i replied as to what is wrong
    in the above code im not sure yet as I couldent find this post
    on the message board for awhile ?
    Windows - The colorfull clownsuit for dos.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sscanf and string handling question
    By ursula in forum C Programming
    Replies: 14
    Last Post: 05-30-2009, 02:21 AM
  2. Problem using sscanf fgets and overflow checking
    By jou00jou in forum C Programming
    Replies: 5
    Last Post: 02-18-2008, 05:42 AM
  3. Problems reading formatted input with sscanf
    By Nazgulled in forum C Programming
    Replies: 17
    Last Post: 05-10-2006, 12:46 AM
  4. sscanf question
    By Zarkhalar in forum C++ Programming
    Replies: 6
    Last Post: 08-03-2004, 07:52 PM
  5. sscanf (I think)
    By RyeDunn in forum C Programming
    Replies: 7
    Last Post: 07-31-2002, 08:46 AM

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