-1

Okay, first, I should let you all know that I'm a relatively new programmer and I just can't seem to get functions right, so I apologise in advance if these are really stupid/obvious questions.

Anyway, to the actual programming now. This is a school project, kind of like hangman. I've been at it for weeks and I'm close to finally getting finished but these pesky error are getting in my way and I just can't fix them!

My beautiful errors

If anyone could help me eliminate these last few errors it would be greatly appreciated! And again, beginner programmer here, I probably made some cringe-worthy mistakes. Sorry for the lengthy code as well..

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

typedef struct {
    char title[50];
    char hidden[50];
}Title;


void Film(Title* pT)
{
    int i=0;
    int number;
    char movies[44][50];

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

    for(i=0; i<44; ++i)
    {
        fgets(movies[i], sizeof(movies[i]), fp);
    }


    fclose(fp);


    number = (rand() % 44);
    strcpy(pT->title, movies[number]);
}

char Star(Title* pT, char lowerc, char higherc, char character)
{
    int val; 
    char c;
    int lenMovie;

    lenMovie = strlen(pT->title);
    strcpy(pT->hidden, pT->title);

    for(val=0; val <= lenMovie; val++)
    {

        c = pT->hidden[val]; 



        if(c == lowerc || c == higherc)
        {
            pT->hidden[val] = character;
        }


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

            pT->hidden[val] = '*';
        }


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

            pT->hidden[val] = '*';
        }


        else
        {

            pT->hidden[val] = c;
        }

    }

    printf("%s", pT->hidden);
}


char Film_Guess(Title* pT, int attempt)
{

    int guess[50], answer, size;

    printf("What movie do you think it is: ");
    scanf("%s", &guess);

    size = strlen(pT->title);
    answer = strncmp(pT->title, guess[50], size);


    if(answer = 0)
    {
        printf("You beat the Film Genie, nce work!");
        return 0;
    }


    else
    {
        attempt++;
        return main();
    }

}


int main(void)
{

    char option; 
    int attempt = 0;
    char lowerc, higherc, character, reply;

    Title t;
    srand(time(NULL));

    printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t Welcome Player\n\n\n");




    while(attempt <= 5)
    {


        printf("\nWould you like to try: ");
        scanf("%c", &reply);


        if(reply == 'y' || reply == 'Y')
        {

        Film (&t);
        Star(&t, lowerc, higherc, character);

        printf("\n Would you like to guess a character(c) or the whole film(f):");
        scanf("%c",&option);



            if(option =='c' || option =='C')
            {

                printf("\nPlease enter a character: ");
                scanf("%c", &character);

                lowerc = tolower(character);
                higherc = toupper(character);

                Star(&t, lowerc, higherc, character);


            }


            else if(option =='f' || option =='F')
            {

                Film_Guess(&t, attempt);

            }

            else
            {

                printf("\nInvalid response");
                return main();

            }


        }




    else{
            break;
        }

    }




    if(reply == 'n' || reply == 'N')
    {

        printf("\nLoser");
        return 0;

    }



    else
    {

        printf("\nInvalid response");
        return main();

    }

}
sgress454
  • 24,870
  • 4
  • 74
  • 92
Realt
  • 17
  • 5

2 Answers2

2

Some errors that I've spotted (there may be more):

  1. strncmp() takes two char* arguments, you're providing an int* as one of them. Check the manpages: http://www.manpagez.com/man/3/strncmp/

  2. at line 117: Star(&t, char lowerc, char higherc, char character); -- you should not provide the types when passing arguments, only when declaring the parameters(*note below).

  3. at line 129: Star (&t); -- your function is declared with 4 parameters. You can't just provide one, you have to provide all four.

  4. at line 135: Film_Guess(&t, int attempt); -- same problem as at line 117

*note: If you're confused regarding what "arguments" are vs what "parameters" are, see this question/answer.

Community
  • 1
  • 1
Tim Čas
  • 10,501
  • 3
  • 26
  • 32
  • This really helped, it's compiling now but after it asks the user if they want to play and they input 'Y' it bypasses everything and goes back to that question, any idea why? – Realt Mar 14 '14 at 23:26
  • Well, it's going back to the question because of the `while` loop. As to why it's bypassing everything, I'm not quite sure. I'd recommend fixing your indentation though, might help you find the issue; I'm having a hard time telling what's in which conditional (`if` statement) and what isn't. – Tim Čas Mar 14 '14 at 23:33
  • Okay thanks, I'll fix it up now a little and edit it above and have another look at why it might be bypassing everything, thanks again! – Realt Mar 14 '14 at 23:38
  • @Realt: it is probably skipping stuff because it reads the newline after reading the first letter. Use `" %c"` to skip white space. – Jonathan Leffler Mar 15 '14 at 07:43
1

Given:

int guess[50], answer, size;
...
answer = strncmp(pT->title, guess, size);

You need char guess[50] or thereabouts.

This line in main():

Star(&t, char lowerc, char higherc, char character);

is a cross between a declaration and a function call.

Star (&t);

You need to be consistent in the number of arguments passed to a function.

The other problem seems to be similar:

Film_Guess(&t, int attempt);
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278