Thread: Error with invalid operands

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    2

    Error with invalid operands

    Well, hello everybody, I've been following the site for a while now but never really needed to post. But right now I'm in an extreme situation and I need to solve this asap xdd

    This is my first year in college (yay! ) and I'm supposed to deliver my last program for this semester in 4 hours (I know, I know, I'm a lazy piece of s**t xdd).

    Ok, so after this brief introduction, I'll ask you guys to excuse my 'spaniard' English level and help me out with this.
    ______

    The exercise consists on 3 procedures. We get the information from a .txt like these:

    01/03/2011 A
    02/03/2011 F
    03/03/2011 C
    04/03/2011 T
    (...)

    Simulating a Videoclub database where the letters stand for the type of movie (A=Action, T=Terror, C=Comedy, ...) and the dates they were rented.

    a) How many movies from one specific genre have been rented more than 'n' times? The genre and the value 'n' must be entered by the user.

    b) How many movies and which genres belong to a certain date? The date must be entered by the user.

    c) Print a list that shows the number of times a movie from each genre has been rented.

    ____

    So far this is what I've got:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define MAX 100
    
    typedef struct {
     int dia,mes, any;
     char genere;
    
    }lloguer;
    
    int apartat_a(lloguer llista[MAX][MAX],int M,int n, char z)
    {
    int i,j, cont;
    
    i=0; j=0; cont=0;
    
    while((i<M) &&(j<10)){
    if (llista[i][j].genere==z){cont++;}
    i++; j++;
    }
    if (cont<n){cont=0;}
    
    return (cont);
    }
    
    void apartat_b (lloguer llista[MAX][MAX],int M,int resultat[]){
    
    int i,j,d,m,a;
    
    printf("\nIntrodueixi les dades\n"),
    printf("\nIntrodueixi el dia:\n"),
    scanf("%d",&d);
    printf("\nIntrodueixi el mes:\n"),
    scanf("%d",&m);
    printf("\nIntrodueixi el any:\n"),
    scanf("%d",&a);
    
    
    for (i=0;i<M;i++)
    for (j=0;j<10;j++){
        if ((llista[i][j].any==a)&&(llista[i][j].mes==m)&&(llista[i][j].dia==d)){
    
            if ((llista[i][j].genere=='A')){resultat[0]++;}
            if ((llista[i][j].genere=='F')){resultat[1]++;}
            if ((llista[i][j].genere=='C')){resultat[2]++;}
            if ((llista[i][j].genere=='T')){resultat[3]++;}
            if ((llista[i][j].genere=='R')){resultat[4]++;}
    
        }
    }
    
    printf("En el data seleccionat es van llogar:");
        printf("%d de accio",resultat[0]);
        printf("%d de ficcio",resultat[1]);
        printf("%d de comedia",resultat[2]);
        printf("%d de terror",resultat[3]);
        printf("%d de romantiques",resultat[4]);
    }
    
    void apartat_c (lloguer llista[MAX][MAX],int M,int resultat2[]){
    
    int i,j;
    i=0;
    j=0;
    
    while (i<M && j<10){
    if (llista[i][j].genere=='A'){resultat2[0]++;}
    if (llista[i][j].genere=='F'){resultat2[1]++;}
    if (llista[i][j].genere=='C'){resultat2[2]++;}
    if (llista[i][j].genere=='T'){resultat2[3]++;}
    if (llista[i][j].genere=='R'){resultat2[4]++;}
    
    i++; j++;
    }
    
    printf("\n El total de pelicules llogades es:\n");
    printf("%d de accio",resultat2[0]);
    printf("%d de ficcio",resultat2[1]);
    printf("%d de comedia",resultat2[2]);
    printf("%d de terror",resultat2[3]);
    printf("%d de romantiques",resultat2[4]);
    
    
    
    }
    int main (){
    lloguer llista[MAX][MAX];
    int resultat[5]={0,0,0,0,0};
    int resultat2[5]={0,0,0,0,0};
    int M,n,i,j,r;
    char z;
    FILE *f;
    f=fopen ("Videos.txt","r");
    
    i=0; j=0;
    while (llista[i][j]!= EOF)
        {
    fscanf(f,"%d",&llista[i][j].dia);
    fscanf(f,"%d",&llista[i][j].mes);
    fscanf(f,"%d",&llista[i][j].any);
    fscanf(f,"%c",&llista[i][j].genere);
    i++; j++;
    }
    fclose(f);
    
    M=i;
    
    printf("\nIntrodueixi el tema:\n");
    scanf("%c",&z);
    printf("\nIntrodueixi el vlaor de n:\n");
    scanf("%d",&n);
    r=apartat_a(llista,M,n,z);
    printf("\nHi ha %d pelis del tema %c que s'han llogat mes de %d vegades.\n",r,z,n);
    
    apartat_b(llista,M,resultat);
    apartat_c(llista,M,resultat2);
    
    return 0;
    }
    First of all, I'm sorry the variables and the words overall are in spanish.

    I'm sure I'll have many mistakes and I'll be glad to hear them if you can point them out. But right now, my main problem is that I can't even debug because i get an error in line 97 --> while (llista[i][j] != EOF) <-- saying "invalid operands to binary != (have 'lloguer' and 'int').

    I've tried to cast (int) before "llista[i][j]" but it says that I'm already supposed to get an integer from that.

    Could you guys help me out, please?

    Thank you very much.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So llista[i][j] is a whole entire struct, not an integer. Why are you trying to compare a struct to EOF? You should do a read and check whether it works or not.

    I wonder why it's a doubly-subscripted array though.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It does not make sense to compare llista[i][j] to EOF. What you probably want is to change this:
    Code:
    while (llista[i][j]!= EOF)
        {
    fscanf(f,"%d",&llista[i][j].dia);
    fscanf(f,"%d",&llista[i][j].mes);
    fscanf(f,"%d",&llista[i][j].any);
    fscanf(f,"%c",&llista[i][j].genere);
    i++; j++;
    }
    To:
    Code:
    while (fscanf(f,
                  "%d %d %d %c",
                  &llista[i][j].dia,
                  &llista[i][j].mes,
                  &llista[i][j].any,
                  &llista[i][j].genere) == 4)
    {
        i++;
        j++;
    }
    I am not sure why you increment both i and j within the loop: like this, you're only populating the 2D array diagonally. Also, to avoid buffer overflow, you should check that the number of items read does not exceed the bounds of the array.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Dec 2013
    Posts
    2
    Ok, guys, thank you very much. The main problem is that it's the first time I'm using registers and I still haven't got the idea right :/

    Thanks laserlight, as you said, the increment of 'i' is correct, but j should be restarted to '0' every time, right? That way the matrix moves downwards which is what I need in every loop.

    Ok, now at least I am able to compile. Now it's debugging time

    Thank you so much to both of you. I'll post again in a few minutes whenever I get stucked again xdd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 'error: invalid operands to binary *' problem
    By Kevin Breslin in forum C Programming
    Replies: 3
    Last Post: 04-05-2013, 04:02 PM
  2. Getting error: invalid operands to binary &
    By P6nn in forum C Programming
    Replies: 3
    Last Post: 05-06-2012, 04:27 AM
  3. Invalid Operands to Binary Error
    By MikeyVeeDubb in forum C Programming
    Replies: 17
    Last Post: 09-23-2009, 04:13 AM
  4. error: invalid operands to binary %
    By OxKing033 in forum C Programming
    Replies: 12
    Last Post: 03-18-2008, 09:21 AM
  5. error: invalid operands to binary *
    By cblix in forum C Programming
    Replies: 5
    Last Post: 01-29-2006, 08:45 PM