#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    23
    Rep Power
    0

    Can someone explain this code?


    Code:
    for(j=1; j<i; j++)
                                      if(strcmp(a[i].nume], a[j].nume==1)
                                      {
                                                            struct angajat aux;
                                                            aux=a[i]; a[i]=a[j];
                                                            a[j]=a[i];
                                                            }
    It's supposed to alphabeticly sort the names from a file. But I don't actually understand what each instruction does.

    The whole program is this:

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    
    struct angajat{ 
         char nume[30], prenume[30];
         int nr;
         float vanzari1,vanzari2,vanzaritotale;
         };
    
    int main()
    {int n,i,j;
        struct angajat a[50];
         FILE *file_in;
         file_in=fopen("date_in.txt","r");
         fscanf(file_in,"%d",&n);
         printf("Datele din fisier sunt: ");
         for(i=1; i<=n; i++)
         {
                   fscanf(file_in, "%d%s%s%f%f%f", &a[i].nr,a[i].nume,a[i].prenume,&a[i].vanzari1,&a[i].vanzari2,&a[i].vanzaritotale);
                   printf("\nInformatii angajat %d:\n",i);
                   printf("\nNr: %d",a[i].nr);
                   printf("\nNume: %s\nPrenume: %s",a[i].nume,a[i].prenume);
                   printf("\nVanzari perioada 1(RONI):%f\nVanzari perioada 2(RONI):%f\n",a[i].vanzari1,a[i].vanzari2,a[i].vanzaritotale);
                   }
                   fclose(file_in);
                   
                   printf("\n\n\tVanzarile totale sunt:\n");
                   for(i=1; i<=n; i++)
                   {
                            a[i].vanzaritotale=(a[i].vanzari1+a[i].vanzari2)/2;
                            printf("\nVanzarile totale pentru angajatul %d este(%.2f+%.2f)/2=%.2f", i, a[i].vanzari1, a[i].vanzari2, a[i].vanzaritotale); 
                            }     
                            
                            for(i=1; i<n; i++)
                            {
                                      for(j=1; j<i; j++)
                                      if(strcmp(a[i].nume], a[j].nume==1)
                                      {
                                                            struct angajat aux;
                                                            aux=a[i]; a[i]=a[j];
                                                            a[j]=a[i];
                                                            }
                                     }       
      
      getch();
      return 1;  }
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,406
    Rep Power
    1871
    > if(strcmp(a[i].nume], a[j].nume==1)
    You could post the real code, because this doesn't stand a chance at compiling.
    There's one too many ], and one too few )

    Also, strcmp() does not return 1 (well it might, but that's not what the manual says).
    The strcmp() function compares the two strings s1 and s2. It returns an integer less than, equal to, or greater than zero if s1 is found, respectively, to be less than, to match, or be greater than s2.
    You should have something like
    Code:
    if ( strcmp(a,b) > 0 ) {
      // a is after b
    }

    Comments on this post

    • dwise1_aol agrees : In fact, I seem to recall a thread here where somebody asked why strcmp returned a value like 48. ==, !=, >, or < zero is the reliable way to test strcmp.
    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
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    23
    Rep Power
    0
    It works when compiling just fine for me :confused:
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,406
    Rep Power
    1871
    Lemme guess - TurboC user right?

    Here's what I get.
    Code:
    $ gcc -W -Wall -Wextra baz.c
    baz.c: In function ‘main’:
    baz.c:24:16: warning: too many arguments for format [-Wformat-extra-args]
    baz.c:38:54: error: expected ‘)’ before ‘]’ token
    baz.c:38:54: error: too few arguments to function ‘strcmp’
    baz.c:38:54: error: expected ‘)’ before ‘]’ token
    baz.c:38:54: error: expected statement before ‘]’ token
    baz.c:38:55: error: expected expression before ‘,’ token
    baz.c:38:55: warning: left-hand operand of comma expression has no effect [-Wunused-value]
    baz.c:38:55: warning: statement with no effect [-Wunused-value]
    baz.c:38:69: error: expected statement before ‘)’ token
    baz.c:40:72: warning: variable ‘aux’ set but not used [-Wunused-but-set-variable]
    baz.c:46:3: warning: implicit declaration of function ‘getch’ [-Wimplicit-function-declaration]
    One of the first things you need to sort out is how to indent code properly.
    Code:
    #include <stdio.h>
    //!!#include <conio.h>
    #include <string.h>
    
    struct angajat {
      char nume[30], prenume[30];
      int nr;
      float vanzari1, vanzari2, vanzaritotale;
    };
    
    int main()
    {
      int n, i, j;
      struct angajat a[50];
      FILE *file_in;
      file_in = fopen("date_in.txt", "r");
      fscanf(file_in, "%d", &n);
      printf("Datele din fisier sunt: ");
    
      for (i = 1; i <= n; i++) {
        fscanf(file_in, "%d%s%s%f%f%f", 
               &a[i].nr, 
               a[i].nume, 
               a[i].prenume, 
               &a[i].vanzari1,
               &a[i].vanzari2, 
               &a[i].vanzaritotale);
        printf("\nInformatii angajat %d:\n", i);
        printf("\nNr: %d", a[i].nr);
        printf("\nNume: %s\nPrenume: %s", a[i].nume, a[i].prenume);
        printf("\nVanzari perioada 1(RONI):%f\nVanzari perioada 2(RONI):%f\n", 
               a[i].vanzari1,
               a[i].vanzari2 /*!! this is not printed, 
               a[i].vanzaritotale */);
      }
      fclose(file_in);
    
      printf("\n\n\tVanzarile totale sunt:\n");
      for (i = 1; i <= n; i++) {
        a[i].vanzaritotale = (a[i].vanzari1 + a[i].vanzari2) / 2;
        printf("\nVanzarile totale pentru angajatul %d este(%.2f+%.2f)/2=%.2f", 
               i, 
               a[i].vanzari1,
               a[i].vanzari2, 
               a[i].vanzaritotale);
      }
    
      for (i = 1; i < n; i++) {
        for (j = 1; j < i; j++)
          if (strcmp(a[i].nume, a[j].nume) > 0) {
            struct angajat aux;
            aux = a[i];
            a[i] = a[j];
            a[j] = aux; //!! not a[i];
          }
      }
    
    //!!  getch();
      return 0; //!! 0 is success, return 1;
    }
    Having fixed the completely screwed strcmp call, I was left with these errors.
    Code:
    $ gcc -W -Wall -Wextra baz.c
    baz.c: In function ‘main’:
    baz.c:24:16: warning: too many arguments for format [-Wformat-extra-args]
    baz.c:40:72: warning: variable ‘aux’ set but not used [-Wunused-but-set-variable]
    baz.c:46:3: warning: implicit declaration of function ‘getch’ [-Wimplicit-function-declaration]
    These are fixed in the above code, annotated with "//!!" comments.

    You really should get a modern compiler.
    GCC with lots of warnings enabled can tell you about lots of dumb stuff (like incorrect parameters to printf/scanf).

    > for (i = 1; i <= n; i++)
    However, what it can't tell you is that arrays start at subscript 0, not 1.
    So if you typed in 50 as your number of records, you would overflow the end of your array.
    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

IMN logo majestic logo threadwatch logo seochat tools logo