Hello, I’m working on a guessing game in C and I have been running into a few problems.
Previously, my code worked but it returned an infinite loop after the first cycle through the algorithm. It’s a simple while loop with a clear perimeter, so I’m lost as to why I’m returning an infinite loop.
#include <stdio.h>
#include <stdlib.h>
int main(){
int secretNumber = 5;
int guess;
while(guess != secretNumber){
printf("Enter the secret number: ");
scanf("%d", &guess);
}
printf("You win!");
return 0;
}
The second time it returned the following error: lvalue required as left operand of assignment|. I’m posting the code below:
#include <stdio.h>
#include <stdlib.h>
int main(){
int secretNumber = 5;
int guess;
int guessCount = 0;
int guessLimit = 3;
int outofguesses = 0;
while(guess != secretNumber && outofguesses = 0){
if(guessCount < guessLimit){
printf("Enter the secret number: ");
scanf("%d", &guess);
guessCount++;
} else{
outofguesses = 1;
printf("You're out of guesses.");
}
}
printf("You win!");
return 0;
}
I’m not clear what the problem here is. The issue the system talks about seems to be fine…so obviously I’m missing an important detail. What?
So should I assign guess 0?
I’d thought that if it you instantiated an integer that would be defined later by user input, then not defining it would be enough. It would be similar to using None…am I way off?
I take back what I said earlier. Its early, and I just woke up lol. Using 1 should get you out of the loop. The loop will only run if its 0. I would give your guess a value before the loop, change the = to. == after outofguesses and see what happens
This isn’t a truth universally accepted. We typically avoid writing answers for people and instead help them write the answers for themselves.
Writing code for people centers the tutoring on our capabilities while helping people write code for themselves centers the discussion on their learning.
Always format and indent your code consistently as you work on it - this will make it easier for your brain to identify problems and when you have bugs it makes it easier to analyze your code and find them - if you have inconsistent formatting, your head will spend time continually re-parsing your code and that detracts from finding a solution
In conditionals always use parentheses to group conditions together - this again helps your brain identify related items together - which will help you find issues with your logic more quickly
As a previous poster mentioned, you are attempting to set outofguesses to the value 0 by using a single “=” equal sign instead of using the double equals “==” to test the value
As mentioned previously, in C if you don’t initialize a variable to a value you might not get the value you expect - always initialize variables - in this guess “int guess” - set it to some reasonable value e.g. “int guess = 0”
C is very tricky when it comes to handling input - with your program if someone doesn’t enter a number, the code will go into an endless loop
Your logic is overly complicated - now for a beginner that is not a complaint, it is normal and happens - think through your logic carefully and see if you can simplify it to the least possible logic in your while loop needed to achieve your goal of either 1) printing a success message and exiting if someone guesses the number or 2) printing a “try again” message if someone does not successfully guess the number within the number of tries you specified
Lets see what you can do to clean up your logic and improve the readability of your code based on what I have shared.
Hints:
Read about stdin (standard input), fflush, and how to use fflush with scanf to clear the input buffer after using sscanf - some combination of those will allow you to handle the user entering something other than a number without the program then looping to the max number of guesses and exiting without the user getting additional chances to guess the number
scanf returns a numeric code that indicates whether it succeessfully parsed input based on the scan format you passed it - you’ll need to handle that case in order for your program to work properly as well when the user enters something other than a number (or just hits return without entering anything)
Okay. I’m still not passing, but I’m trying to implement some of the advice and I’m going to post my code along with explanation of what my logic is. Hoping for correction…
#include <stdio.h>
#include <stdlib.h>
int secretNumber = 5;
int guess = 0;
int guessCount == 0;
int guessLimit = 3;
int outofguesses == 0;
I got an error with guessCount. It said it expected a ‘=’, which I didn’t do because this is a value and it all increment. For the secretNumber I did not use a == because I want this number to stay the same and be a fixed value. So, my logic here is that if a number might possibly change, you should use a double quote to indicate the value is important and to focus on it. While the single quote, focuses on the value as a goal not something that it iterable.
This is also the reason I didn’t use double quotes for guessLimit. The idea behind this number is not something instantiate a variable or to be iterable, it is a variable to be reached. Am I at all in the ball park?
Okay. For outofguesses, I receive the error message error: 'outofguesses' undeclared (first use in this function)| I don’t understand why. I declared it at the top of the page. Right?=
Finally, I used fflush(stdin) to clean any previous guess in case an invalid entry was used. However, I wonder if I should’have just used a conditional and a break statement for this.
Okay. Now , thinking back over it, that makes a lot more sense. However, I’m still not passing. There’s one error. I’m going to post my code and the error message beneath it.
#include <stdio.h>
#include <stdlib.h>
int secretNumber = 5;
int guess = 0;
int guessCount = 0;
int guessLimit = 3;
int outofguesses = 0;
main(){
while( guess != secretNumber && outofguesses = 0)
{
if(guessCount < guessLimit){
printf("Enter the secret number: ");
scanf("%d", &guess);
guessCount++;
} else {
if(guessCount == guessLimit)
{
outofguesses = 1;
printf("You're out of guesses.");
} else {
printf("You win!");
}
}
}
fflush(stdin);
return 0;
}
The error message is the following:
C:\Users\Admin\OneDrive\Desktop\Desktop\prac\main.c|12|error: lvalue required as left operand of assignment|
Okay. Thank you for the clarification. This makes a lot of sense and I went back through and was able to understand the logic of comparison and assignment. Problem solved.