Thread: sscanf extracts same wrong integers every time

  1. #1
    Registered User
    Join Date
    Nov 2016
    Posts
    33

    sscanf extracts same wrong integers every time

    Good day C Gurus

    I m trying to insert the list of the name surnames ID and grades of 5 students and then exctrating their grades using sscanf my code is the folowing
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    
    
    
    
    int main()
    {
    
    
        char matrix[5][35];
      char name[5][10];
      char surname[5][10];
      int IDE[5][6]={0};
    /* i intilized all the int array to 0 to avoid them be filled with garbage*/
      int mark[5][3]={0};
      int average =0;
      int i;int cnt; int flag[5]={0};
      for (i=0;i<5;i++)
      {
          gets(matrix[i]);
      }
    
    
    
    
    
    
    
    
      for (i=0;i<5;i++)
      {
      sscanf(matrix[i],"%s %s %d %d",name[i],surname[i],&IDE[i],&mark[i]);
      }
    
    
       for (i=0;i<5;i++)
      {
      printf("%s %s %d %d",name[i],surname[i],IDE[i],mark[i]);
      }

    the printf part is only to check my data and i was surprised to see that sscanf gave me every time same wrong values for integers

    what s wrong with me and how can i adress it?

    Many thanks in advance

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You should make sure your code is formatted and indented properly, and nothing is cut out (you're missing a closing brace).

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
     
     
    int main()
    {
        char matrix[5][35];
        char name[5][10];
        char surname[5][10];
        int IDE[5][6]={0};
        
        int mark[5][3]={0};
        int average =0;
        int i;
        int cnt;
        int flag[5]={0};
      
        for (i=0;i<5;i++)
        {
              fgets(matrix[i], 35, stdin);
        }
    
        for (i=0;i<5;i++)
        {
            sscanf(matrix[i],"%s %s %d %d",name[i],surname[i],&IDE[i],&mark[i]);
        }
     
        for (i=0;i<5;i++)
        {
            printf("%s %s %d %d",name[i],surname[i],IDE[i],mark[i]);
        }
        
        return 0;
    }
    Notice I took the courtesy of replacing gets() with fgets(), since gets() is dangerous and obsolete.

    Then make sure you are compiling with full warnings:

    $ gcc cboard.c -Wall -Wextra
    cboard.c: In function ‘main’:
    cboard.c:11:5: warning: missing braces around initializer [-Wmissing-braces]
    cboard.c:11:5: warning: (near initialization for ‘IDE[0]’) [-Wmissing-braces]
    cboard.c:13:5: warning: missing braces around initializer [-Wmissing-braces]
    cboard.c:13:5: warning: (near initialization for ‘mark[0]’) [-Wmissing-braces]
    cboard.c:26:9: warning: format ‘%d’ expects argument of type ‘int *’, but argument 5 has type ‘int (*)[6]’ [-Wformat]
    cboard.c:26:9: warning: format ‘%d’ expects argument of type ‘int *’, but argument 6 has type ‘int (*)[3]’ [-Wformat]
    cboard.c:31:9: warning: format ‘%d’ expects argument of type ‘int’, but argument 4 has type ‘int *’ [-Wformat]
    cboard.c:31:9: warning: format ‘%d’ expects argument of type ‘int’, but argument 5 has type ‘int *’ [-Wformat]
    cboard.c:17:9: warning: unused variable ‘flag’ [-Wunused-variable]
    cboard.c:16:9: warning: unused variable ‘cnt’ [-Wunused-variable]
    cboard.c:14:9: warning: unused variable ‘average’ [-Wunused-variable]


    The middle warnings about "format '%d' ..." are the problem. "IDE" and "mark" are two-dimensional arrays, but you are trying to use them as if they were one-dimensional arrays.

  3. #3
    Registered User
    Join Date
    Nov 2016
    Posts
    33

    problem with sscanf

    Thank you Matticus for your prompt reply and I could fix the problem following your remarks

    Code:
    sscanf(matrix[i],"%s %s %d %d",name[i],surname[i],&IDE[i][1],&mark[i][1]);
    you said that IDE and mark are two dimensional arrays


    int mark[5][3]
    IDE[5][6]
    so first here are my questions

    • How can C make the difference between char matrix[5][8] to indicate 5 rows 8 colomns and char matrix[5][8] that means that matrix has 5 rows and one colonne with the size of 8 for each row (same if the matrix of type integer)????
    • you also said that ""IDE" and "mark" are two-dimensional arrays, but you are trying to use them as if they were one-dimensional arrays" but still this problem didn t happen with the char variable used in the same code ligne why is that?



    Thanks a million!!!








    int mark[5][3]
    IDE[5][6]

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    How can C make the difference between char matrix[5][8] to indicate 5 rows 8 colomns and char matrix[5][8] that means that matrix has 5 rows and one colonne with the size of 8 for each row (same if the matrix of type integer)????
    Well, char and int are different sizes of integers. Characters are specialized for storing texts and are much smaller, so when you ask for char IDE[5][6] and int mark[5][3], the size of the memory required is different. mark would require 60 bytes on a typical 32bit computer (5*3*4) whereas IDE is 30 bytes (5*6*1). The computer would also store things differently in these regions of memory, so it has to know how variables are different in order to work.

    I don't know if it was a typo or what, but you have 6 columns for IDE and 3 columns for mark, not 8 anywhere. The difference matters.

    you also said that ""IDE" and "mark" are two-dimensional arrays, but you are trying to use them as if they were one-dimensional arrays" but still this problem didn t happen with the char variable used in the same code ligne why is that?
    You can think of char IDE[5][6] as a flat array of 5 strings, where each string can have a maximum length of 5 (position 6 would hold the '\0' character). When you access IDE[0], for example, you get to treat it like a single string. By contrast, with an integer array, you have to assign each individual element in a loop; sscanf doesn't understand how to assign whole arrays of integers.
    Last edited by whiteflags; 02-06-2017 at 04:55 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using sscanf to extract integers frmo "[1,2,3,4]
    By lsatenstein in forum C Programming
    Replies: 7
    Last Post: 05-08-2016, 08:15 AM
  2. reading integers with sscanf
    By freakomaniak in forum C Programming
    Replies: 1
    Last Post: 12-04-2013, 12:23 AM
  3. Reading integers in sscanf
    By Blasz in forum C Programming
    Replies: 1
    Last Post: 04-30-2010, 06:57 AM
  4. What is wrong with my sscanf function?
    By doampofo in forum C Programming
    Replies: 5
    Last Post: 03-09-2003, 10:42 PM
  5. program that extracts certain words from the string
    By MKashlev in forum C++ Programming
    Replies: 2
    Last Post: 08-06-2002, 10:03 AM

Tags for this Thread