What’s wrong with my code?

Asked

Viewed 109 times

-1

I want to make a simple hangman game with some pre-determined words, right at the beginning still...

escolhendo palavra...
SIGSEGV on thread : 1685113120"

My code is this

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

int main()
{
    int a,b=0;
    char q;
    char chamar(int z);
    printf("escolhendo palavra...\n");
    srand(time NULL);
    a=(rand()%4);
    q = chamar(a);
    b=strlen(q);
    printf("%d",b);
    for(int x=0;x<=b;x++)
    {
        printf("_");
        }
}

char chamar (int z)
{
    switch(z)
    {
        case 0:
        return "soda";
        break;
        case 1:
        return "hair";
        break;
        case 2:
        return "paper";
        break;
        case 3:
        return "telephone";
        break;
        case 4:
        return "sky";
        break;
        default:
        return "errinho";
        break;
    }
}

1 answer

3

There are a lot of things wrong, but let’s start with the things that make your program work.

  • Its function chamar you want to return a string, so you have to return something like char* and not char, for char is only one letter. This correction has to be done in all places that use the function

  • In the for(int x=0;x<=b;x++) has in comparison <= when I should have just <

  • Is made return within each case in the switch then you don’t have to break

Code after corrections indicated:

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

char *chamar(int z); //com *

int main() {
    int a,b=0;
    char *q; //com *
    printf("escolhendo palavra...\n");
    srand(time(NULL));
    a=(rand()%4);
    q = chamar(a);
    b=strlen(q);
    printf("%d",b);
    for(int x=0; x<b /*< em vez de <=*/; x++) {
        printf("_");
    }
}

char* /*com * */ chamar (int z) {
    switch(z) {
    case 0: return "soda";
    case 1: return "hair";
    case 2: return "paper";
    case 3: return "telephone";
    case 4: return "sky";
    default: return "errinho";
    }
}

Check it out at Ideone

  • C allows declaring i within the for? No need to declare at the start of the function?

  • 1

    @Andersoncarloswoss Depends on the compilation flags. If it is being compiled in C89 for example it is exactly this way and does not allow, but in more modern versions it already allows.

  • Thank you very much, thank you very much.

  • Just to complement: Just like @Isac said, this syntax doesn’t work on C89 because it’s standard C99. The i, is usually an abbreviation of "index" ("index"). Declare outside the for is considered a good practice of programming because it is a syntax required from the perspective of an algorithm. This means that if you need to access the last value of the index, it will only be possible if the i is declared outside the scope of for because all local variables inhabit within a function, so when a function ends, all that is declared within the scope of for is destroyed.

  • Remembering that "statement" and "definition" are different things.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.