KYC KYC - 1 year ago 66
C Question

Can't get a number guessing game to work in C

Tried to write a program in C to say the amount of times you guessed the right number.

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

int main()
int i, searchNumber, Number, rightGuess;
rightGuess = 0;
printf("Give your number: ");
printf("\n\n Give 10 numbers: ");
scanf("%d \n",&Number);
if(Number == searchNumber){

printf("You guessed the number %d times",&rightGuess);
return 0;

However every time I run it, it says I guessed the number 6356736 times. Even though I only entered a number 0 times. Any help?

Answer Source

Maybe you made a mistake in printf().

If there is a variable named var, &var means the memory address where variable var is. So the number 6356736 you saw in your program is the memory address, not the value in variable var.

You will have to change this line

printf("You guessed the number %d times", &rightGuess);

To this line.

printf("You guessed the number %d times", rightGuess);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download