0

I'm doing a project in C for school and and I'm testing out one of the functions to read in a random title from a file and storing that film and that film same film but with asterisks replacing the letters in a struct.

Anyway, I keep getting the same error of "Expected declaration before 'struct'" when trying to run the code and I've tried loads of different things but I keep getting the same error, I don't even know if my code will work! If you see any error in my code feel free to point them out :)!

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

typedef struct {
            char title[500];
            char hiddentitle[500];
        }Title;

char Film(Title t){

FILE *fopen(), *fp;

    fp = fopen("Film.txt", "r");

    int i=0;
    int j;
    int number;
    int lenMovie;
    char c, film;
    char movies[45][500];
    int val =0;
    
    
    while( i<45 ){

        fgets(movies[i], sizeof(movies[i]), fp);
        i++;

    }

    srand(time(NULL));
    number = (1+(rand() % 45));
    
    t.title[500] = movies[number];


    printf("%s", t.title);
    lenMovie = strlen(t.title);

    while(val <= lenMovie)
    {
        c = t.title[val];

        if(c >= 'a' && c<= 'z'){

            
            t.hiddentitle[val] == '*';
        }

        else if(c >= 'A' && c<= 'Z'){

            t.hiddentitle[val] == '*';
        }
        else{

            t.hiddentitle[val] == c;
        }

        val++;
    }
    printf("\n%s", t.hiddentitle);
    fclose(fp);
}

int main(void){

    Film (struct Title);
    
    }
3
  • 1
    Also, t.title[500] = ... looks wrong. Commented Mar 8, 2014 at 23:31
  • I was thinking that myself but I honestly just can't think of how else to go about this Commented Mar 8, 2014 at 23:43
  • You perhaps want strcpy(t.title, movies[number]);, or a safer variant such as snprintf(t.title, sizeof t.title, "%s", movies[number]); Also, number looks wrong: it ranges from 1 to 45, you want 0 to 44. And what happens if your input file has fewer than 45 lines? Commented Mar 9, 2014 at 1:01

2 Answers 2

2

You have

typedef struct {
            char title[500];
            char hiddentitle[500];
        }Title;

So in main() use:

int main(void){
Title t;

    Film (t);

    }
Sign up to request clarification or add additional context in comments.

4 Comments

You really ought to initialize t.
That is upto the OP, how he/she wants to handle it. I just pointed out the error.
However I think that's a duplicate of my answer which was posted first :)
That's one way to look at it.. It's like the fastest finger first.. Yours was faster but mine was correct.. :)
0

In your main() function you are doing this, which is invalid:

Film (struct Title);    

Instead, you must pass an argument in your call to File():

Title t = { }; // initialize it
Film(t);

1 Comment

This is not a safe way to pass an argument. t is uninitialized.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.