-1

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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Sullen
  • 3
  • 4
  • Your last line in the `while` doesn't do anything. Did you actually mean to read input again there? - i.e. `guess = getchar();` – 500 - Internal Server Error May 26 '22 at 15:11
  • @500-InternalServerError, I was trying to make it so that if the user guessed wrong, it made them guess again. – Sullen May 26 '22 at 15:13
  • You've just started learning C, or you've just started coding C? Are you getting lessons from somewhere, or are you typing things in that "seem reasonable" and then seeing if they work? My advice is to go with the former: trial and error is a *terrible* way to learn C. – Steve Summit May 26 '22 at 15:13
  • @SteveSummit I bought a book on C and it's been teaching me a lot, so I wanted to try using what I've learned from the first 30 pages. – Sullen May 26 '22 at 15:14
  • You're reading one character from the user, and then never reading anything after that. Also the one character you try to read may not even get read, since the user's going to have to hit (say) `5` to guess the number 5, then Enter or Return to say that he's done typing, and that's two keystrokes right there. – Steve Summit May 26 '22 at 15:15
  • Okay, good, glad to hear you've got a book. Your problem is that last line in the loop, `guess;` — it doesn't do what you think it does. – Steve Summit May 26 '22 at 15:16
  • @SteveSummit I mean... Yeah... I'm the only person who can test it, so I'm going to have to hit enter or return. I'm just doing this on an online compiler. – Sullen May 26 '22 at 15:17
  • @SteveSummit Oh, okay. How do I get it to ask the user again? – Sullen May 26 '22 at 15:17
  • You basically have three options for reading input: (1) getchar for reading single characters, (2) `fgets` for reading whole lines, or (3) `scanf` for reading formatted stuff. But (1) is not the way to go here. (2) is the best, but you have to learn two or three things at once before you can use it. Most introductory books have you use (3), which is sort of okay at first, except `scanf` has a whole basketful of traps and pitfalls that no one ever warns you about. Maybe jump ahead to see what your book has to say. – Steve Summit May 26 '22 at 15:18
  • Okay, thanks. That's enough. I'll go do some more reading and then come back to it. – Sullen May 26 '22 at 15:20
  • If you do choose to use `scanf`, see [here](https://stackoverflow.com/questions/72178518/how-can-i-fix-the-scanf-to-take-data-into-the-array/72178652#72178652) for the secret list of things to watch out for. – Steve Summit May 26 '22 at 15:21

3 Answers3

0

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 :)

Ohad Sharet
  • 1,097
  • 9
  • 15
0

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 );
    }
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

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);
LucaFar0
  • 26
  • 1