-1
How can I take this information from the keyboard, name and value of three products, and print in table form the name with its value beside?
I did so (code at the end), but at the moment appears the message "Signal: Segmentation fault (core dumped)"after reading once the name and value.
I want to print this way:
PRODUTO PREÇO
d 5.00
l 4.00
p 3.00
TOTAL: 12.00
Complete code:
#include <stdio.h>
int main(void)
{
int i, t;
char (mes)[20], produto[30][30];
float valor_total, valor[30];
char lista;
printf("Lista de compras do mês.\n");
printf("Digite o mes das compras: ");
fgets(mes,20,stdin);
for (int i = 0; i < 2; i++) {
printf("Digite o nome do produto: ");
scanf(" %s", produto[i]);
printf("Digite o preço do produto: ");
scanf(" %f", &valor[t]);
valor_total += valor[t];
}
printf("Tabela de compra do mes de %s\n", mes);
printf("PRODUTO PREÇO\n");
for (i = 0; i < 2; i++) {
printf("%s %4.2f\n", produto[i], valor[t]);
}
printf("TOTAL: %4.2f\n", valor_total);
return 0;
}
for (int t = 0; t < 1; t++)
I don’t see why use this is only going to count 1x, if only will type 1 price– Ricardo Pontual
I want to be able to type the price of the first product and then the second product and so on, but I still can’t figure out how to do it in this way. If I just take this one is indicated, the program does not work, but with it, it only takes the value of the last assigned product and not the 3.
– user237530
remove this
for
and only have the name printf/scanf and after the price will work, it will read in following product and price, and will do this 3x, which is what has in the firstfor
. This second one would only make sense if you type 3 price for each product, which is clearly not what you want :) attention that "remove for" is not just deleting the line fromfor
, you have to remove where you lock the key}
– Ricardo Pontual
I did it the way I spoke and appeared the message "Signal: Segmentation fault (core dumped)" after asking once product and value. {} erased correctly.
– user237530
not to know what is "just imagining" here, can edit the question and put the code in it, including the declaration of variables before the is
– Ricardo Pontual
ready, tidy.
– user237530