Home > front end >  Creating a while loop in C
Creating a while loop in C

Time:05-27

I've just started learning C and I've decided to create a while loop to make sure I get the hang of things. This is my code:

#include <stdio.h>

void main(){
    int num, guess, turns;
    num = 13;
    guess = getchar();
    turns = 0;
    
    while(guess != num){
        printf("%d", guess);
          turns;
        printf("Turns:\n");
        printf("%d", turns);
        guess;
    }      
}

It gives me an infinite loop. Can someone please tell me where I went wrong? Also, if you have any suggestions or tips, please feel free to leave them.

CodePudding user response:

I spotted 2 problems in your code the first is that you are using "getchar" instead of "scanf" when trying to get an integer input.

And the second problem is that you are not updating the value of "guess" inside the while loop (and that's causing your infinite loop).

For your convenience here is a fixed version of the code

void main() {
    printf("start \n");
    int num, guess, turns;
    num = 13;
    
    scanf("%d", &guess);
    turns = 0;
    
    while(guess != num){
        printf("your guess was : %d \n", guess);
          turns;
        printf("Turns:");
        printf("%d \n", turns);
        
        scanf("%d", &guess);
    }      
    
}

Hope I could help :)

CodePudding user response:

Within your while loop the variable guess is not being changed

while(guess != num){
    printf("%d", guess);
      turns;
    printf("Turns:\n");
    printf("%d", turns);
    guess;
}  

Moreover this line

    guess;

does not have an effect.

And this statement before the while loop

guess = getchar();

does not make a sense because the function getchar reads only one character and returns the value of the internal representation of the character.

Also pay attention to that according to the C Standard the function main without parameters shall be declared like

int main( void )

Your program can look the following way

#include <stdio.h>

int main( void )
{
    unsigned int num = 13;
    unsigned int turns = 0;
    
    unsigned int guess = 0;

    puts( "Try to guess the number I thought." );
    printf( "Enter a non-negative number: " );

    while( scanf( "%u", &guess ) == 1 && guess != num )
    {
        printf( "%u is not my number.\n", guess );
          turns; 
        printf( "It is your %u attempt\n", turns );
        printf( "\nEnter a non-negative number: " );
    }

    if ( guess == num )
    {
        printf( "\nYou have guessed the number using %u guesses.\n", turns );
    }
    else
    {
        printf( "\nYou have not guessed the number using &u guesses.\n", turns );
    }
}

CodePudding user response:

getchar() does not read from terminal, you have to use:

scanf(" %d", &guess);

In the while loop you have to read a value from terminal again (if the guess is wrong)

while(guess != num){
   printf("Turns:\n");
   printf("%d", guess);
     Turns;
   scanf(" %d", &guess);
}
printf("%d is the correct guess", guess);
  • Related