Home > Mobile >  How do I make my code printf increment from 0 every loop?
How do I make my code printf increment from 0 every loop?

Time:08-21

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;
}

CodePudding user response:

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

CodePudding user response:

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;
  •  Tags:  
  • c
  • Related