im keep getting
ror: expected identifier or '(' before '.' token|error: expected expression before
what am i doing wrong?

#include <stdio.h>
#include <conio.h>
#include <stdint.h>


int main()
{
typedef struct {

char name[20];
char year[20];
int inout[20];

   }movie_rent;

   movie_rent[20];

    int i,sum=0;


     FILE *myfile;

if ((myfile = fopen("mov.txt", "rt")) == NULL)
{
    printf("error: cannt open\n");
}

for (i=0;i<20;i++)
{
    movie_rent[i].inout=0;
}
for ( i=0; i<3; i++)
{
 fscanf(myfile,"%c %c %d[^\n]", movie_rent[i].name, movie_rent[i].year,movie_rent[i].inout );

}
for (i=0;i<3;i++)
{
 sum=sum+movie_rent.inout[i];
}
printf("%d",sum);
return sum;
}

For starters, movie_rent is neither an object nor an array, it's a type. The object declaration would be more correct like this:

struct {
    char name[20];
    char year[20];
    int inout[20];
} movie_rent[20];

But that doesn't fix your syntax issues because you can't seem to decide if movie_rent is an array and inout is an int (circa line 30 and 34):

movie_rent[i].inout=0;

...

fscanf(myfile,"%c %c %d[^\n]", movie_rent[i].name, movie_rent[i].year,movie_rent[i].inout );

Or movie_rent is a single structure instance and inout is an array of int (line 39):

sum=sum+movie_rent.inout[i];

I suspect you want an array of movies:

#include <stdio.h>

int main(void)
{
    struct rental {
        char name[20];
        int year;
        int inout;
    } movies[20];

    FILE *in = fopen("mov.txt", "r");

    if (in) {
        size_t i, n, sum = 0;

        for (n = 0; n < 20; n++) {
            if (fscanf(in, "%19s %d %d", movies[n].name, movies[n].year, movies[n].inout) != 3) {
                break;
            }
        }

        fclose(in);

        for (i = 0; i < n; i++) {
            sum += movies[i].inout;
        }

        printf("Available movie count: %d\n", sum);
    }

    return 0;
}

This should work for movies that have a single word name (it's untested, so I reserve the right to have made stupid errors/typos), but for multiword names you're SOL using the %s specifier and need to figure out something different. I'd suggest flipping the format around so that the name is last, then doing this:

if (fscanf(in, "%d %d %19[^\n]", movies[n].name, movies[n].year, movies[n].inout) != 3) {
    break;
}

That way you can use the end of line marker to figure out where the movie's name stops.

Edited 3 Years Ago by deceptikon

but if my txt file is wrriten like this:

hobbit
2012
1-->rented
argo
2013
0-->not rented

its stiil dont do the math

but if my txt file is wrriten like this:

That's fine then.

its stiil dont do the math

That file works for me (assuming the -->rented and -->not rented parts are exposition added by you for the post and not actually in the file. If they're in the file then you need to remove them from the stream:

#include <stdio.h>

int main(void)
{
    struct rental {
        char name[20];
        int year;
        int inout;
    } movies[20];

    FILE *in = fopen("test.txt", "r");

    if (in) {
        size_t i, n, sum = 0;
        int ch;

        for (n = 0; n < 20; n++) {
            if (fscanf(in, "%19[^\n] %d %d ", movies[n].name, &movies[n].year, &movies[n].inout) != 3) {
                break;
            }

            while ((ch = getc(in)) != '\n' && ch != EOF)
                ;
        }

        fclose(in);

        for (i = 0; i < n; i++) {
            sum += movies[i].inout;
        }

        printf("Available movie count: %d\n", sum);
    }

    return 0;
}

the rented and the not rented are not in the txt file but still even when i am running your code it keep showing me only 1 movie count even there are more!!

it keep showing me only 1 movie count even there are more!!

Debug the code and figure out what's wrong then. I don't have your file to test with, so I can only go on what you've provided in this thread.

*thats my txt file save it as txt and see for yourself.

Next
   1994
  1 
1 Single   
     2006
1
hobbit
2013
1
Next
   1994
  1 
This question has already been answered. Start a new discussion instead.