Thread: sscanf (I think)

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    37

    sscanf (I think)

    Im having some difficulties inputing elements into an array using sscanf. (The book Im using only has one like about sscanf). Anyway, all Im trying to do is input numbers into an array using fgets and sscanf, then print them.

    I dont think Im too far off but I dont know what else to try.

    Code:
    static int valid( char array[], int count )
    {   
    	int i = 0, decCount = 0, negCount = 0;
    
    	for( i = 0; i < count; i++)
      {
    		if( array[i] == '\n' ) continue;
    
    		if(( array[i] < '0' || array[i] > '9' ) && array[i] != '.' && array[i] != '-')
    		return BOOL_TRUE;
    		
    		if( array[i] == '.' )
    			decCount++;
    		else if( array[i] == '-' )
    			negCount++;
    
    		if( decCount > 1 || negCount > 1 )
    			return BOOL_FALSE;
    	}
    	return BOOL_TRUE;
    }
    
    
    int main (void)
    {
    	int maxnum;
    	int e;
    	char a[BUFSIZ];
    
    	printf("How many numbers would you like to sort?\n");
    	scanf("%d%*c", &maxnum);
    	printf("\nPlease begin entering your %d numbers.\n", maxnum);
      
    // GATHER DATA
    	for( e = 0; e < maxnum; e++)
    	{
    		printf( "Enter number %d : ", e + 1 );
    		fgets( a, BUFSIZ, stdin );
    
    		while( valid( a, strlen(a) ) == BOOL_FALSE ) 
    		{
    			printf( "Invalid Integer.\n" );
    			printf( "Please re-enter number %d: ", e + 1 );
    			fgets( a, BUFSIZ, stdin );
    		}
    
    #if 0
    		sscanf( a, "%d", a[e] );
    #endif
    	
    	}
    
    // OUTPUT
    	printf("\n\n  Output");
    	printf("\n ================ ");
    
    	for (e = 0; e < maxnum; e++)
    	{
    		printf("\n  %c", a[e] );
    	}
    
    	return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Since you are accessing a single cell in the array, you must use its address:

    sscanf( line, "%c", &array[x] );

    Because you're reading into a single cell (ie: a single character).

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

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    37

    umm

    pardon my ignorance, but isnt that what I was doing with sscanf( a, "%c", &a[e] ); ?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    You are using a as your buffer for input, that means that it probably will not work as you want with sscanf. Declare another array to hold the final results:
    Code:
    #include <stdio.h>
    
    int main ( void )
    {
      /* Only used for input */
      char buffer[BUFSIZ];
      /* Only used for processed data */
      int final[2];
      int ret;
    
      printf ( "Enter two integers: " );
      if ( fgets ( buffer, sizeof buffer, stdin ) != NULL ) {
        /* scan from buffer to final */
        ret = sscanf ( buffer, "%d %d", &final[0], &final[1] );
        if ( ret != 2 )
          perror ( "An error was detected" );
        else
          printf ( "%d\n%d\n", final[0], final[1] );
      }
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: umm

    Originally posted by RyeDunn
    pardon my ignorance, but isnt that what I was doing with sscanf( a, "%c", &a[e] ); ?
    Nope. Look at your code. You are not using the & symbol. (CTRL+F, type in sscanf, and watch the occurances in the page)

    You have, and I quote:
    Code:
    #if 0
    		sscanf( a, "%d", a[e] );
    #endif
    No & symbol.

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

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    37

    Im too vague

    I knew if I put that it would have been something that was pointed out....

    I did try that and I do have that now but Im still having problems... Im at home now (and drunk if you dont mind, hah) so I cant specifically tell you whats wrong but I definately will 10 hrs from now!....

    Thnx Quz
    Rye

    P.S. WHOO HOO! 30 Posts... Im no longer a jr memeber! (which basically means I have earned the title of "this guy wont give up")
    Last edited by RyeDunn; 07-30-2002 at 09:15 PM.

  7. #7
    Registered User
    Join Date
    Jul 2002
    Posts
    37

    program and results

    If you look at the progs output, it appears that the following number entered always writes over the previous number and splits the numbers into individual characters and puts each into its own cell, see bleow.

    Code:
    Please begin entering your 5 numbers
    Enter number 1 : 632
    Enter number 2 : 5
    Enter number 3 : 6
    Enter number 4 : 2
    Enter number 5 : 85
    
    
      Before Sorting
     ================
     0
      8  <-- first digit of last number
     1
      5  <-- last digit of last number
     2
    
    
     3
    
     4
        Press any key to continue
    Now if you look at the code I am writing the line by using: sscanf(a, "%s", &a[e] ); Where a = array and e = cell or element number.

    Code:
    int main (void)
    {
    	int maxnum;
    	int e;
    	char a[BUFSIZ];
    
    	printf("How many numbers would you like to sort?\n");
    	scanf("%d%*c", &maxnum);
    	printf("\nPlease begin entering your %d numbers.\n", maxnum);
      
    // GATHER DATA
    	for( e = 0; e < maxnum; e++)
    	{
    		printf( "Enter number %d : ", e + 1);
    		fgets( a, BUFSIZ, stdin );
    
    		while( valid( a, strlen(a) ) == BOOL_FALSE ) 
    		{
    			printf( "Invalid Integer.\n" );
    			printf( "Please re-enter number %d: ", e + 1 );
    			fgets( a, BUFSIZ, stdin );
    		}
    
    #if 0
    		sscanf(a, "%s", &a[e] );
    #endif
    	
    	}
    
    // BEFORE SORT
    	printf("\n\n  Before Sorting");
    	printf("\n ================ ");
    
    	for (e = 0; e < maxnum; e++)
    	{
    		printf("\n %d ", e);  //For debuging purposes
    		printf("\n  %c ", a[e] );
    	}
    
    	return 0;
    }
    Could one of you C Gurus please look at this and let me know what Im doing wrong? Much Thanks!

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    You haven't been listening to me, you are trying to read from a into a with sscanf. This is very likely not what you want, try this and see what I do differently:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define DEBUG
    
    #define BOOL_TRUE  1
    #define BOOL_FALSE 0
    
    static int valid( char array[], int count )
    {   
      int i = 0, decCount = 0, negCount = 0;
      
      for( i = 0; i < count; i++)
      {
        if( array[i] == '\n' ) continue;
        
        if(( array[i] < '0' || array[i] > '9' ) && array[i] != '.' && array[i] != '-')
          return BOOL_FALSE;
        
        if( array[i] == '.' )
          decCount++;
        else if( array[i] == '-' )
          negCount++;
        
        if( decCount > 1 || negCount > 1 )
          return BOOL_FALSE;
      }
      return BOOL_TRUE;
    }
    
    int main (void)
    {
      int maxnum;
      int e;
      /* Use this for input */
      char a[BUFSIZ];
      /* Take from a and place the values here */
      double final[BUFSIZ] = {0.0};
    
      printf("How many numbers would you like to sort?\n");
      scanf("%d%*c", &maxnum);
      /* Check that scanf worked */
      printf("\nPlease begin entering your %d numbers.\n", maxnum);
    
      for( e = 0; e < maxnum; e++)
      {
        printf( "Enter number %d : ", e + 1);
        fgets( a, BUFSIZ, stdin );
        /* Check that fgets worked */
    
        while( valid( a, strlen(a) ) == BOOL_FALSE ) 
        {
          printf( "Invalid Integer.\n" );
          printf( "Please re-enter number %d: ", e + 1 );
          fgets( a, BUFSIZ, stdin );
          /* Check that fgets worked */
        }
        sscanf(a, "%lf", &final[e] );
        /* Check that sscanf worked */
      }
    
      printf("\n\n  Before Sorting");
      printf("\n ================\n");
      
      for (e = 0; e < maxnum; e++)
      {
    #ifdef DEBUG
        printf("%d: ", e);
    #endif
        printf("%f\n", final[e] );
      }
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

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, 06: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()
    By task in forum C Programming
    Replies: 4
    Last Post: 11-22-2003, 04:43 PM