Thread: Pointer doubt

  1. #31
    Registered User
    Join Date
    Apr 2012
    Posts
    159
    Well, i think i'm starting to get it but i still have a memory adress in function 2 then all rest will have wrong values.

    So,

    Code:
    gp2_Ques(usr1, &result, &total_perg, &certas, &erradas);
    //Function2 int gp2_Ques(struct gp2 *p, char *usr1, int *result,int *total_perg,int *certas,int *erradas) //all variables are incremented here (*result)++;//incrementa a variavel result (*total_perg)++; (*certas)++; (*erradas)++; //Total values printf("\nTem um total de %d pontos\n", *result); printf("\nTem um total de %d perguntas\n", *total_perg); printf("\nTem %d de respostas certas.\n", *certas); printf("\nTem %d de respostas erradas.\n", *erradas); //THIS one has a memory adress

  2. #32
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Post your main, it might be the way you are declaring/calling erradas in main that is making the problem.

  3. #33
    Registered User
    Join Date
    Apr 2012
    Posts
    159
    Hi

    I really thank you all for helping me with this.

    My main doesn't start like this, but i'm sendind all functions envolved in this subjet.
    Firts let me explain that main function are (gp1_Ques,gp2_Ques, gp3_Ques, gp4_Ques), all those functions call jogo1GP1, jogo1GP2, jogo1GP3, jogo1GP4.


    Code:
    int jogo1Gp1(FILE *f, int res1)
    {
        struct perg p;
        int resposta;
        system("cls");
        printf ("\n*=============================================================================*\n");
        printf ("|                          >>>>Escolha Multipla<<<<                            |\n");
        printf ("*=============================================================================*\n");
        fseek(f, res1 * sizeof p, SEEK_SET);
        fread(&p, sizeof p, 1, f);
        printf("\nPergunta : %s", p.ques);
        printf("\nOpcao 1 : %s",p.op1);
        printf("\nOpcao 2 : %s",p.op2);
        printf("\nOpcao 3 : %s",p.op3);
        printf("\nOpcao 4 : %s",p.op4);
        printf("\nResposta correta?\n");
        scanf("%d", &resposta);
        getchar();
        if(resposta==p.res)
        return 1;
        else
        return 0;
    }
    
    
    int jogo1Gp2(FILE *f, int res1)
    {
        struct gp2 p;
        char buf[10];
        system("cls");
        printf ("\n*=============================================================================*\n");
        printf ("|                          >>>>Facto ou Ficcao<<<<                            |\n");
        printf ("*=============================================================================*\n");
        fseek(f, res1 * sizeof p, SEEK_SET);
        fread(&p, sizeof p, 1, f);
        printf("\nPergunta : %s",p.ques);
        printf("\nResposta correta? (facto ou ficcao)\n");
        fgets(buf,9,stdin);
        if(strcmp(buf,p.res)==0)
        return 1;
        else
        return 0;
    }
    
    
    
    
    int jogo1Gp3()
    {
        int num[TAM];
        int vet[TAM], i;
        int n=4;
        system("cls");
            printf ("\n*=============================================================================*\n");
            printf ("|                       >>>>Ordenacao rapida<<<<                                |\n");
            printf ("*=============================================================================*\n");
            printf ("                                                                               \n");
        LeVetor(num, n);
        system("cls");
            printf ("\n*=============================================================================*\n");
            printf ("|                       >>>>Numeros Selecionados<<<<                          |\n");
            printf ("*=============================================================================*\n");
            printf ("                                                                               \n");
        for(i=0; i<n;i++)
        {
            printf("%d\n",num[i]);
        }
        bubbleSort(num,n);
            printf ("\n*=============================================================================*\n");
            printf ("|                       >>>>Vetor ordenado<<<<                                |\n");
            printf ("*=============================================================================*\n");
            printf ("                                                                               \n");
        for(i=0; i<n;i++)
        {
            //printf("%d\n",num[i]);
        }
        for(i=0;i<n; i++)
        {
            printf("\nOrdene os 4 primeiros numeros de forma ascendente\n");
            scanf("%d",&vet[i]);
        }
         for(i=0;i<n; i++)
         {
             //printf("\n%d        %d \n",num[i], vet[i]);
             if(num[i]==vet[i])
             return 1;
             else
             return 0;
         }
         return 0;
    }
    
    
    int jogo1Gp4(FILE *f, int res1)
    {
        struct perg p;
        int resposta;
        system("cls");
        printf ("\n*=============================================================================*\n");
        printf ("|                          >>>>Escolha Multipla<<<<                            |\n");
        printf ("*=============================================================================*\n");
        fseek(f, res1 * sizeof p, SEEK_SET);
        fread(&p, sizeof p, 1, f);
        printf("\nPergunta : %s", p.ques);
        printf("\nOpcao 1 : %s",p.op1);
        printf("\nOpcao 2 : %s",p.op2);
        printf("\nOpcao 3 : %s",p.op3);
        printf("\nResposta correta?\n");
        scanf("%d", &resposta);
        getchar();
        if(resposta==p.res)
        return 1;
        else
        return 0;
    }
    
    
    int gp1_Ques(struct perg *p, char *usr1)
    {
        FILE *f; //apontador para ficheiro
        f=fopen("gp1.dat", "r"); //abertura do ficheiro
        int reg=0,reg1; //Variaveis contadoras de perguntas e random
        int random; // Variável de random
        int result=0; //Variáveis resultados
        int total_perg=0, certas=0, erradas=0;
        float t_certas;
        float t_erradas;
        rewind(f); //retorna a posição corrente do ficheiro para o início
        srand(time(NULL)); //função random
        system("cls");
        fseek(f, 0, SEEK_END);
        reg=ftell(f) /sizeof *p;
        fseek(f, 0 , SEEK_SET);
        if (reg<=0) //condição que verifica se existem registos
        {
            printf ("Sem registos \n");
            printf ("\n Necessario ter registos para jogar...\n");
            return 0;
            }
            else
            {
                printf ("\n*=============================================================================*\n");
                printf ("|                       >>>>Visualizao de perguntas<<<<                        |\n");
                printf ("*=============================================================================*\n");
                printf ("                                                                               \n");
                printf ("\n      Bemvindo %s \n",usr1);
                printf ("\n      TOTAL de perguntas: %d \n",reg); //conta as perguntas
                printf ("\n      O seu score maximo sera: %d\n",reg/10); //pontuação máxima do jogo
                printf ("                                                                               \n");
                printf ("*=============================================================================*\n");
                Sleep(2000);
                random=rand()%reg; //função random
                reg1 = random; //atribui a variavel reg o valor de random
                if ( jogo1Gp1(f,reg1) )
                {
                    printf("\nAcertou na resposta...\n");
                    result++;
                    total_perg++;
                    certas++;
                    random=rand()%reg;//função random
                    printf("Atencao a proxima pergunta...\n");
                    Sleep(1000);
                    if ( jogo1Gp1(f,random))
                    {
                        printf("\nAcertou novamente na resposta...\n");
                        result++;
                        total_perg++;
                        certas++;
                    }
                    else
                    {
                        printf("Errou nesta questao...\n");
                        erradas++;
                        total_perg++;
                    }
                }
                else
                {
                    printf("Errou nesta questao...\n");
                    printf("\nTem ainda mais uma tentativa...\n");
                    Sleep(1500);
                    erradas++;
                    total_perg++;
                    random=rand()%reg; //função random
                    reg1 = random; //atribui a variavel reg o valor de random
                    if ( jogo1Gp1(f,reg1) )
                    {
                        printf("\nAcertou na resposta...\n");
                        result++;
                        total_perg++;
                        certas++;
                    }
                    else
                    {
                        printf("Errou novamente...ai esta cultura geral!!! \n");
                        erradas++;
                        total_perg++;
                        printf("\nNao consegue qualificacao para a proxima fase....tente de novo\n");
                        jogo();
                    }
                }
            }
            if (result>=1)
            {
                if ((certas > 0) && (erradas==0))
                {
                    t_certas=total_perg*100 /certas;
                }
                else
                {
                    t_certas=certas*100 /total_perg;
                    t_erradas=erradas*100 / total_perg;
                }
                system("cls");
                printf ("\n*=============================================================================*\n");
                printf ("|                       >>>>Resultado desta fase 1<<<<                        |\n");
                printf ("*=============================================================================*\n");
                printf ("                                                                               \n");
                printf("\nTeve um total de %d pontos\n", result);
                printf("Respondeu a %d perguntas \n", total_perg);
                printf("\nAcertou %d perguntas\n", certas);
                printf("\nErrou %d perguntas\n", erradas);
                printf("\nPercentagem certas: %.2f %% \n", t_certas);
                printf("\nPercentagem erradas: %.2f %%\n", t_erradas);
                printf("\n                     Vai passar para a fase 2...aguarde \n");
                printf ("*=============================================================================*\n");
                printf ("                                                                               \n");
                Sleep(5000);
                gp2_Ques(usr1, &result, &total_perg, &certas, &erradas);
            }
            else
            {
                printf("Nao atingiu o minimo para seguir....vai voltar ao menu anterior");
                Sleep(2000);
                system("cls");
                jogo();
            }
            fclose(f);
            return 0;
    }
    
    
    int gp2_Ques(struct gp2 *p, char *usr1, int *result,int *total_perg,int *certas,int *erradas)
    {
        FILE *f; //apontador para ficheiro
        f=fopen("gp2.dat", "rb"); //abertura do ficheiro
        int reg=0,reg1; //Variaveis contadoras de perguntas e random
        int random; // Variável de random
        rewind(f); //retorna a posição corrente do ficheiro para o início
        srand(time(NULL)); //função random
        system("cls");
        fseek(f, 0, SEEK_END);
        reg=ftell(f) /sizeof *p;
        fseek(f, 0 , SEEK_SET);
        if (reg<=0) //condição que verifica se existem registos
        {
            printf ("Sem registos \n");
            printf ("\n Necessario ter registos para jogar...\n");
            return 0;
            }
            else
            {
                printf ("\n*=============================================================================*\n");
                printf ("|                           >>>>Facto ou Ficcao<<<<                            |\n");
                printf ("*=============================================================================*\n");
                printf ("                                                                               \n");
                printf ("\n      Boa sorte neste fase %s \n",usr1);
                printf ("\n      Traz da outra fase: %d pontos \n",*result); //conta as perguntas
                printf ("\n      TOTAL de perguntas: %d \n",reg); //conta as perguntas
                printf ("\n      O seu score maximo sera: 3 \n"); //pontuação máxima do jogo
                printf ("                                                                               \n");
                printf ("*=============================================================================*\n");
                Sleep(3000);
                random=rand()%reg; //função random
                reg1 = random; //atribui a variavel reg o valor de random
                if ( jogo1Gp2(f,reg1) )
                {
                    printf("\nAcertou na resposta...\n");
                    (*result)++;//incrementa a variavel result
                    (*total_perg)++;
                    (*certas)++;
                    random=rand()%reg;//função random
                    printf("Atencao a proxima pergunta...\n");
                    Sleep(2000);
                    if ( jogo1Gp2(f,random))
                    {
                        printf("\nAcertou novamente na resposta...\n");
                        (*result)*=2; //incrementa a variavel result
                        (*total_perg)++;
                        (*certas)++;
                    }
                    else
                    {
                        printf("Resposta errada....foi eliminado :(");
                        (*total_perg)++;
                        (*erradas)++;
                        Sleep(2000);
                        system("cls");
                        jogo();
                    }
                }
                else
                {
                    printf("\nResposta errada....foi eliminado :(");
                    (*total_perg)++;
                    (*erradas)++;
                    Sleep(2000);
                    system("cls");
                    jogo();
                }
            }
            if (*result>=6)
            {
                system("cls");
                printf ("\n*=============================================================================*\n");
                printf ("|                       >>>>Resultado desta fase 2<<<<                        |\n");
                printf ("*=============================================================================*\n");
                printf ("                                                                               \n");
                printf("\nTem um total de %d pontos\n", *result);
                printf("\nTem um total de %d perguntas\n", *total_perg);
                printf("\nTem %d de respostas certas.\n", *certas);
                printf("\nTem %d de respostas erradas.\n", *erradas);
                printf("\n                        Vai passar para a fase 3...aguarde \n");
                printf ("*=============================================================================*\n");
                printf ("                                                                               \n");
                Sleep(5000);
                gp3_Ques(usr1, result, total_perg, certas, erradas);
            }
            else
            {
                printf("Nao atingiu o minimo para seguir....vai voltar ao menu anterior");
                printf("Tem um total de %d perguntas", *total_perg);
                printf("Tem %d respostas certas.", *certas);
                printf("Tem %d respostas erradas.", *erradas);
                Sleep(5000);
                system("cls");
                jogo();
            }
            fclose(f);
            return 0;
    
    
    }
    
    
    
    
    int gp3_Ques(char *usr1, int *result,int *total_perg,int *certas,int *erradas)
    {
        system("cls");
        printf ("\n*=============================================================================*\n");
        printf ("|                           >>>>Ordenacao numeral<<<<                            |\n");
        printf ("*=============================================================================*\n");
        printf ("                                                                               \n");
        printf ("\n      Esta no bom caminho %s \n",usr1);
        printf ("\n      Traz da outra fase: %d pontos \n", *result); //conta as perguntas
        printf ("\n      O seu score maximo sera: 2 \n"); //pontuação máxima do jogo
        printf ("                                                                               \n");
        printf ("*=============================================================================*\n");
        Sleep(3000);
        if ( jogo1Gp3() )
                {
                    printf("\nAcertou na resposta...\n");
                    (*result)++;//incrementa a variavel result
                    (*total_perg)++;
                    (*certas)++;
                    printf("Atencao a proxima pergunta...\n");
                    Sleep(2000);
                    if ( jogo1Gp3())
                    {
                        printf("\nAcertou novamente na resposta...\n");
                        (*result)++;//incrementa a variavel result
                        (*total_perg)++;
                        (*certas)++;
                    }
                    else
                    {
                        printf("Resposta errada.... :(");
                        (*total_perg)++;
                        (*erradas)++;
                        Sleep(2000);
                        system("cls");
                    }
                }
                else
                {
                    printf("\nResposta errada....foi eliminado :(");
                    (*total_perg)++;
                    (*erradas)++;
                    Sleep(2000);
                    system("cls");
                    jogo();
                }
        if (*result>=7)
        {
            printf ("\n*=============================================================================*\n");
            printf ("|                       >>>>Resultado desta fase 3<<<<                        |\n");
            printf ("*=============================================================================*\n");
            printf ("                                                                               \n");
            printf("\nTem um total de %d pontos\n", *result);
            printf("\nTem um total de %d perguntas\n", *total_perg);
            printf("\nTem %d de respostas certas.\n", *certas);
            printf("\nTem %d de respostas erradas.\n", *erradas);
            printf("\n                        Vai passar para a fase 4...aguarde \n");
            printf ("*=============================================================================*\n");
            Sleep(5000);
            gp4_Ques(usr1, result, total_perg, certas, erradas);
        }
        else
        {
            printf("Nao atingiu o minimo para seguir....vai voltar ao menu anterior");
            Sleep(2000);
            system("cls");
            jogo();
        }
        return 0;
    }
    
    
    int gp4_Ques(struct gp2 *p, char *usr1, int *result,int *total_perg,int *certas,int *erradas)
    {
        FILE *f; //apontador para ficheiro
        f=fopen("gp4.dat", "rb"); //abertura do ficheiro
        int reg=0,reg1; //Variaveis contadoras de perguntas e random
        int random; // Variável de random
        rewind(f); //retorna a posição corrente do ficheiro para o início
        srand(time(NULL)); //função random
        system("cls");
        fseek(f, 0, SEEK_END);
        reg=ftell(f) /sizeof *p;
        fseek(f, 0 , SEEK_SET);
        if (reg<=0) //condição que verifica se existem registos
        {
            printf ("Sem registos \n");
            printf ("\n Necessario ter registos para jogar...\n");
            return 0;
            }
            else
            {
                printf ("\n*=============================================================================*\n");
                printf ("|                           >>>>Fuga aos pontos<<<<                            |\n");
                printf ("*=============================================================================*\n");
                printf ("                                                                               \n");
                printf ("\n      Boa sorte neste fase %s \n",usr1);
                printf ("\n      Traz da outra fase: %d pontos \n",*result); //conta as perguntas
                printf ("\n      TOTAL de perguntas: %d \n",reg); //conta as perguntas
                printf ("\n      O seu score maximo sera: 3 \n"); //pontuação máxima do jogo
                printf ("                                                                               \n");
                printf ("*=============================================================================*\n");
                Sleep(3000);
                random=rand()%reg; //função random
                reg1 = random; //atribui a variavel reg o valor de random
                if ( jogo1Gp4(f,reg1) )
                {
                    printf("\nAcertou na resposta...\n");
                    (*result)++;//incrementa a variavel result
                    (*total_perg)++;
                    (*certas)++;
                    random=rand()%reg;//função random
                    printf("Atencao a proxima pergunta...\n");
                    Sleep(2000);
                    if ( jogo1Gp4(f,random))
                    {
                        printf("\nAcertou novamente na resposta...\n");
                        (*result)*=2; //incrementa a variavel result
                        (*total_perg)++;
                        (*certas)++;
                    }
                    else
                    {
                        printf("Resposta errada....foi eliminado :(");
                        (*total_perg)++;
                        (*erradas)++;
                        Sleep(2000);
                        system("cls");
                        jogo();
                    }
                }
                else
                {
                    printf("\nResposta errada....foi eliminado :(");
                    (*total_perg)++;
                    (*erradas)++;
                    Sleep(2000);
                    system("cls");
                    jogo();
                }
            }
            if (*result>=6)
            {
                system("cls");
                printf ("\n*=============================================================================*\n");
                printf ("|                       >>>>Resultado desta fase 2<<<<                        |\n");
                printf ("*=============================================================================*\n");
                printf ("                                                                               \n");
                printf("\nTem um total de %d pontos\n", *result);
                printf("\nTem um total de %d perguntas\n", *total_perg);
                printf("\nTem %d de respostas certas.\n", *certas);
                printf("\nTem %d de respostas erradas.\n", *erradas);
                printf("\n                        Vai passar para a fase 3...aguarde \n");
                printf ("*=============================================================================*\n");
                printf ("                                                                               \n");
                Sleep(5000);
                gp3_Ques(usr1, result, total_perg, certas, erradas);
            }
            else
            {
                printf("Nao atingiu o minimo para seguir....vai voltar ao menu anterior");
                printf("Tem um total de %d perguntas", *total_perg);
                printf("Tem %d respostas certas.", *certas);
                printf("Tem %d respostas erradas.", *erradas);
                Sleep(5000);
                system("cls");
                jogo();
            }
            fclose(f);
            return 0;
    
    
    }
    I'm sure there is something wrong in there

  4. #34
    Registered User
    Join Date
    Apr 2012
    Posts
    159
    I forgot to mention...
    The problem is in function gp2_Ques

    The printf of variable erradas gives me memory adress

    Thanks

  5. #35
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Gil Carvalho View Post
    I'm sure there is something wrong in there
    Code:
    gp2_Ques(usr1, &result, &total_perg, &certas, &erradas);    // line 229
    ...
    int gp2_Ques(struct gp2 *p, char *usr1, int *result,int *total_perg,int *certas,int *erradas)  // line 243
    Looks like you don't get it :-(.

    Bye, Andreas

  6. #36
    Registered User
    Join Date
    Apr 2012
    Posts
    159
    AndiPersti

    Now yes, i think i get it

    Code:
     gp2_Ques(usr1, &result, &total_perg, &certas, &erradas);
    
    ....
    
    int gp2_Ques(char *usr1, int *result,int *total_perg,int *certas,int *erradas)
    right?

  7. #37
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    At least function definition and call are compatible now.

  8. #38
    Registered User
    Join Date
    Apr 2012
    Posts
    159
    Hi AndiPersti

    Yes, at least, took ages to understand but now i think i get how does this work.

    Thanks for your help and patience

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doubt if this is bad pointer
    By gusth in forum C Programming
    Replies: 2
    Last Post: 09-08-2011, 12:24 AM
  2. pointer doubt....
    By roaan in forum C Programming
    Replies: 14
    Last Post: 07-29-2009, 11:20 AM
  3. Doubt in pointer.
    By shwetha_siddu in forum C Programming
    Replies: 5
    Last Post: 03-21-2009, 01:28 AM
  4. Doubt regarding pointer
    By karthik537 in forum C Programming
    Replies: 7
    Last Post: 01-21-2009, 09:53 AM
  5. A doubt regarding FILE * pointer
    By rohan_ak1 in forum C Programming
    Replies: 4
    Last Post: 08-01-2008, 05:15 AM