Array in C - How to print values from an Array

Asked

Viewed 53 times

-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

  • 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.

  • 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 first for. 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 from for, you have to remove where you lock the key }

  • I did it the way I spoke and appeared the message "Signal: Segmentation fault (core dumped)" after asking once product and value. {} erased correctly.

  • 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

  • ready, tidy.

Show 1 more comment

2 answers

1

In the second snippet, where you print, you switched the arguments of for, compare with the pair of for’s of the first snippet, where i = 1.. 3 and t = 1.. 1, while in the second section both i and t are 1 to 3

printf("PRODUTO      PREÇO\n");

for (i = 0; i < 3; i++) {

    for (t = 0; t < 3; t++) {

    (...)
    }
 }

If you change the "t<3" of the second is above by "t<1", as it is done in the first section, you should have what you wanted: print only a copy of each product.

  • I suggest using the code formatting available in https://answall.com/editing-help#code

0

Instead of

int i, t; product[i] and value[t]

pus : int i; product[i], value[i]

and everything settled to have the need for just one loop.

Browser other questions tagged

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