Exercise in C returns wrong value

Asked

Viewed 299 times

-1

I am trying to do an exercise in C that does not generate any syntax error,I have searched and can not find the error,it simply reads or returns the wrong values.

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

    int main()
    {



        int codigo,quantidade;
        float precototal;
        char especificacao[] = "";
        printf("Escreva o codigo do produto:");
        scanf("%d",&codigo);
            printf("Escreva a quantidade do produto:");
        scanf("%d",&quantidade);

        switch(codigo)
        {
        case 100:
        strcpy(especificacao, "Cachorro Quente"); 
         printf("%d",quantidade);
        precototal = 1.20 * quantidade;

        printf("Produto:%s,Quantidade:%d,preco unitario:%f,preco total:%f",especificacao,quantidade,1.20,precototal);

        break;
        case 101:

        break;
        case 102:

        break;
        case 103:

        break;
        }
        return 0;
}

the value that is returned is this: execução

  • Is there any way to improve your question? Can’t know what you want.

  • When entering the code and quantity of the product, wrong values are returned, as shown in the image, the total price and quantity go wrong.

  • What were the input data? Could put the output in writing?

  • were 100 and 1, but has already been resolved according to the answer below,!!

2 answers

2


try to match your code correctly

I initialized your string char especificacao[] of a size [30] and here worked see how the code:

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

int main()
{
    int codigo,quantidade;
    float precototal;
    char especificacao[30];
    printf("Escreva o codigo do produto:");
    scanf("%d",&codigo);
    printf("Escreva a quantidade do produto:");
    scanf("%d",&quantidade);

    switch(codigo)
    {
    case 100:
    strcpy(especificacao, "Cachorro Quente");
    printf("%d",quantidade);
    precototal = 1.20 * quantidade;
    printf("Produto:%s,Quantidade:%d,preco unitario:%f,preco total:%f",especificacao,quantidade,1.20,precototal);
    break;
    case 101:

    break;
    case 102:

    break;
    case 103:

    break;
    }
    return 0;
}

you could also initialize your string especificacao as pointer to char char *especificacao; would also work vetor pointing to "" 0 in the case.

  • It really worked! Would you like to explain to me why I can’t put just specification[] ? without setting a size ? Thanks in advance!

  • can but Voce initialized its vector in the array with "" in the case 0 or NULL i walk don’t really know why but with the tests I’ve been doing if Voce try to start a vector string like this char vs[0]; it is impossible to work with this variable ^^

1

Our friend @Assanges is correct. The problem is the line containing the statement

char especificacao[] = "";

What happens is you’re not declaring the area size in memory reserved to keep a String, that is, how many bytes do you want to use to store it. For some reason the function strcpy correctly returns the word "Cachorro Quente" into especificacao.

However, since there is no exact amount of memory reserved, possibly the return of the function ends up overwriting reserved area for some of its other variables and so the result is incorrect.


The correct way to declare variables of type String ,which is a pointer to character, is as Assanges said

char especificacao[30];

Here it separates 30 bytes in memory to store characters. Note that you can save only 29 caractares, because the character \0 denotes the end of the string and is indispensable.


This problem can be best seen if you try to read from any console string of arbitrary size into the variable with no declared size, as in the example below:

char especificacao[] = "";
        scanf("%s",&especificacao); 
    printf("Produto: %s ",especificacao);

Depending on the size of your input, the program can write your input, or it can give segmentation failure.

  • Very good explanation, thank you!

Browser other questions tagged

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