Problem in a function parameter

Asked

Viewed 57 times

0

#include <stdio.h>  
#include <stdlib.h>  
int pos_setinha(int x);
void menu(int x);

int main()
{int x=0;
do
{
    menu(pos_setinha(x));
}
while(1);
}
int pos_setinha(int x)
{   x=0;
char C;
    C=getch();
    switch(C)
    {
    case 72:
        x++;
        break;
    case 80:
        x--;
        break;
    }
return x;
    }

void menu(int x)
 {if(x==0)
    printf("0");
if(x==1)
    printf("1");
if(x==2)
    printf("2");
 }

I’m having trouble saving the new value of x after it goes through the pos_setinha function, the function always returns to the start of the function when x is 0, how can I fix it?

  • Its objective is that the function pos_setinha changes the value of x ? if applicable, x = pos_setinha(x); solves the problem. However the x=0; at the beginning of it is the most.

2 answers

2

Hello, I think it was not very clear what you wanted with the problem, next time try to give better examples of what you want and what is happening, besides explaining better what the program does or should do, so it is easier for us to help you, Anyway we go the solution and to what I understood that you wanted.

First the error you are having is due to the fact that you are not storing the value of the variable x nowhere, the first thing we should do is fix it, for this is simple, we should just look at the function (or routine), main and pos_setinha.

This is your code:

int main()
{
    int x=0;

    do
    {
        menu(pos_setinha(x)); // veja que você não armazena o valor
                              // retornado da função em lugar algum,
                              // desta forma ele é perdido, e o q você
                              // manda para a sua função é apenas um x = 1 ou -1;
    }while(1);

 // Também esta faltando um return 0 aqui, 
 // desta forma a função main não retorna nada para o sistema.
}

Now let’s look at the function pos_setinha:

int pos_setinha(int x)
{
    char C;
    C=getch();

    x=0; // Como você pode ver aqui você atribui 0 ao seu X desta forma perdendo o
         // valor que está contido nele, e também assim fazendo com que
         // sua função sempre retorne 1 ou -1, nunca retornando 2 ou 0
         // como o desejado.

    switch(C)
    {
    case 72:
        x++;
        break;
    case 80:
        x--;
        break;
    }

    return x;
}

So at this point Jose I ask you not to look at the rest of the answer yet, and stop and think about what should be done to solve these mistakes, first try to solve it yourself, so the chance for you to better understand your mistake will be greater, than just look a result already ready okay ;).

Anyway, in case you haven’t gotten an answer here’s mine. The first thing that must be done is to assign the value returned by the function to a variable, so the value will not be lost, the second is to take that x = 0 from the function pos_setinha, and the third place the return 0 in function main so the program will be as follows:

int pos_setinha(int x)
{
    char C = getch(); // Foi tirado o X = 0, para o valor não se perder
                      // e note que você pode por o getch junto com a 
                      // declaração da variável C.

    switch(C)
    {
    case 72:
        x++;
        break;
    case 80:
        x--;
        break;
    }

    return x;
}

int main()
{
    int x = 0;

    do
    {
        x = pos_setinha(x); // Dessa forma o valor será guardado na
                            // variável X.
        menu(x);
    }while(1);

 return 0; // return 0 foi adicionado.
}

Now what you must be wondering is whether your problem is over, and the answer is no, your program has one more problem which is the buffer not being clean, so it is necessary that you clean it, there are several ways to do that which I will use here is the same that is in the video that I Linked the word buffer, so it will be easy for you to understand. Then to do the cleaning just that before reading the character you use the function setbuf, this way you will clean the buffer, then the program will be in such a way:

int pos_setinha(int x)
{
    setbuf(stdin, NULL); // Aqui você estará atribuindo o valor NULL, ou 
                         // seja, 0 ao stdin, que é o buffer do teclado,
                         // assim estará o limpando.
    char C = getch();

    switch(C)
    {
    case 72:
        x++;
        break;
    case 80:
        x--;
        break;
    }

    return x;
}

Now you must be thinking that it is over, and you are right, just started, rs, seriously, there are also a few more things that can be done, given the fact that you are using characters you do not need to put the table number ASCII, can put the direct character, but remember to put between apostrophes ('letter'), so knowing that 72 and 80 are respectively H and P, the program will be like this.

int pos_setinha(int x)
{
    setbuf(stdin, NULL);
    char C = getch();

    switch(C)
    {
    case 'H':
        x++;
        break;
    case 'P':
        x--;
        break;
    }

    return x;
}

Note that so the program becomes more intuitive for those who look, and so we know if you are only working with int or char, because that was one of the ambiguities of your program, I didn’t know, and I still don’t know, if you wanted to work with char and looked at the table ASCII values and placed, or if you wanted to work with numbers, because if it is the second case you will have to change the type of variable C to int, but I thought you wanted to work with characters.

Last but not least there is also the menu function, da to make a change in it, since it serves to print the value of a variable it is not necessary that you do those ifs, just print the variable, so the function will be in such a way:

void menu(int x)
{
    printf("%i\n", x);
}

Now it’s all over. Just one more thing try not to declare the functions on top of main and make the definition below, this creates a Spaghetti code, so always use good programming practices, an example of how I think the code should be:

#include <stdio.h> // Note que eu tirei a stdlib, pois você não estava
                   // utilizando-a.

void menu(int x)
{
    printf("%i\n", x);
}

int pos_setinha(int x)
{
    setbuf(stdin, NULL);
    char C =  getchar();

    switch(C)
    {
    case 'H':
        ++x;
        break;

    case 'P':
        --x;
        break;
    }

    return x;
}

int main()
{
    int x = 0;

    do
    {
        x = pos_setinha(x);
        menu(x);
    }while(1);

    system("pause");
    return 0;
}

That’s it, I hope I’ve helped, anything just ask, and good luck in programming studies.

0

The function getch() is used to read only one character.
To read more than one (a number in this case) you must use scanf:

int cNumber;
scanf("%d\n",&cNumber)

Browser other questions tagged

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