0

I want to make a sort of food ordering system, but whenever I try to make the ID increment by 1 every loop, it doesn't work. It just shows the number 6684076, no matter what.

Here's the code:

struct res {
  char nome[40];
  char endereco[100];
  char pedido[200];
  char valor[20];
};

int main() {
  char M[100];
  int c;
  int menu;
  int cod = 0;

  while (cod < 100) {
    cod = cod + 1;
    struct res R1, R2, R3, R4;

    printf("Digite seu nome: \n");
    gets(R1.nome);
    fflush(stdin);

    printf("Digite seu endereco: \n");
    gets(R2.endereco);
    fflush(stdin);

    printf("Digite seu pedido: \n");
    gets(R3.pedido);
    fflush(stdin);

    printf("Digite o valor total que vai pagar: \n");
    gets(R4.valor);
    fflush(stdin);

    system("cls");
    c = c + 1;
    printf("Codigo: %d\n", &c);
    printf("Nome: %s\n", R1.nome);
    printf("Endereco: %s\n", R2.endereco);
    printf("Pedido: %s\n", R3.pedido);
    printf("Valor: %s\n", R4.valor);
  }

  return 0;
}
user438383
  • 5,716
  • 8
  • 28
  • 43
Scuttler
  • 13
  • 3
  • 1
    The variable c is never initialized – AndyG Aug 20 '22 at 19:01
  • 2
    A lot of warnings from a decent compiler: https://godbolt.org/z/cWcoxMa5h – mch Aug 20 '22 at 19:02
  • 1
    `printf("Codigo: %d\n", &c);` -> `printf("Codigo: %d\n", c);` – 0___________ Aug 20 '22 at 19:25
  • 1
    Another of those warnings that should be heeded is [never use `gets`](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used). Never. If your book tells you to use it, get a better one. If your professor tells you to use it, tell them from me that they are wrong :) – Nate Eldredge Aug 20 '22 at 19:44
  • 1
    And [don't use `fflush(stdin)` either](https://stackoverflow.com/questions/2979209/using-fflushstdin) unless you only care about compatibility with one system, and have very clear documentation saying it does what you want. – Nate Eldredge Aug 20 '22 at 19:46
  • And next time before asking someone to help with your code, indent it properly so that they can read it easily. – Nate Eldredge Aug 20 '22 at 19:47
  • `gets` actually has been removed from the standard... – Aconcagua Aug 20 '22 at 20:20

2 Answers2

1

you should've initialised int c to 0 otherwise it'll take a garbage value

Divyam
  • 43
  • 10
  • This alone won't help out at all as what actually is printed is the *address* of c (or part of at least; actually we have UB due to mismatch between format specifier and function argument). There are *two* errors in, one covered by your answer, the other one by the other answer. – Aconcagua Aug 20 '22 at 20:22
0

I don't see any errors in my compiler and when I debug it, there's no issue with the value of c, so I changed the

printf("Codigo: %d\n", &c);

statment with

printf("Codigo: %d\n", c);

and the output has no issues:

Codigo: 1
Nome: ab
Endereco: cd
Pedido: ef
Valor: gh
Digite seu nome:

EDIT: Additionally you need to initialise c with a meaningful value, e.g. 0, otherwise it might contain a garbage value and reading it provokes undefined behaviour:

int c = 0;
  • @Aconcagua even if you initialize `c` with `0`, you still want to change the statement I wrote because same garbage value still occurs if you don't do it. But surely we won't trust the compiler to initialize it with the value of `0` so I edited the answer and initialized `c`. – Ozdamar Kevser Aug 20 '22 at 20:36
  • @Aconcagua OK, white flag :) – Ozdamar Kevser Aug 20 '22 at 20:50