Problem in the parameter of a function

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 passes through the pos_setinha function, the function always returns to the beginning of the function when x is 0, how can I fix this?

    
asked by anonymous 05.07.2018 / 15:14

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 and explain better what the program does or should do, so it's easier for us to help you, anyway let's get the solution and what I understood you wanted.

First the error you're having is due to the fact that you're not storing the value of the x variable anywhere, the first thing we should do is sort this out so this is simple, we should just look at the function (or routine), main and pos_setinha .

Here's 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 beg 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 that the chance for you to better understand your error will be greater, than just look at a result already ready okay;).

Continuing, if you have not gotten an answer here is mine. The first thing to do is to assign the value returned by the function to a variable, so the value is not lost, the second is to take that x = 0 from the function pos_setinha , and the third put the return 0 on the function main so the program will look like this:

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 should be wondering is if your problem is over, and the answer is no, your program has one more problem which is buffer is not clean, so you need to clean it, there are several ways to do this that I will use here is the same as it is in the video that I linkei the word buffer, it will be easy for you to understand. So to clean it just before reading the character you use the setbuf function, this way you will clear the buffer, so the program will look like this:

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's over, and you're right, just started, lol, seriously, there are also a few more things that can be done, given the fact that you're using characters you do not need to enter the table ASCII , you can put the character straight, but remember to put between apostrophes ('letter'), this way knowing that 72 and 80 are respectively H and P, the program will look 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 this makes the program more intuitive for the user, so we know if you are only working with int or char , because this was one of the ambiguities of your program, I did not know, and I still do not know , if you wanted to work with char and looked at the table ASCII values and put, 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, to make a change in it, since it serves to print the value of a variable you do not have to do those% s of% s, just print the variable , so the function will be this 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 if 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 to ask, and good luck in programming studies.

    
05.07.2018 / 16:59
0

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

int cNumber;
scanf("%d\n",&cNumber)
    
05.07.2018 / 15:41