The Student Room Group

C programming help

A part of an assignment that I'm working on requires the game/programme to reward the user if they enter a number that's within +/-0.1 of the correct random number generated. I've tried using both abs and fabs but my game no longer works in the way I want it to and doesn't even acknowledge when the number I put in is within the valid range. Here's a snippet of my code:

https://cdn.discordapp.com/attachments/692344148212187226/980212575105060945/20220528_213647.jpg

for added context, "difference" was set to be the difference between the number the user inputs and the randomly generated number beforehand.
Original post by Soul Wavel3ngth
A part of an assignment that I'm working on requires the game/programme to reward the user if they enter a number that's within +/-0.1 of the correct random number generated. I've tried using both abs and fabs but my game no longer works in the way I want it to and doesn't even acknowledge when the number I put in is within the valid range. Here's a snippet of my code:

https://cdn.discordapp.com/attachments/692344148212187226/980212575105060945/20220528_213647.jpg

for added context, "difference" was set to be the difference between the number the user inputs and the randomly generated number beforehand.

abs() is for integers, so youd probably need fabs() which youll need to include math.h. Is difference a float or double? Do you compile with all warnings turned on? Probably need to see the whole function
Original post by mqb2766
abs() is for integers, so youd probably need fabs() which youll need to include math.h. Is difference a float or double? Do you compile with all warnings turned on? Probably need to see the whole function

Float is a double and math.h is included. I'm still relatively new to visual studios so I'm not quite sure what you mean by "compile with all warnings turned on". I've since reverted the code back to its original state before I included the fabs() but I'll see if it's possible to undo it if you'd like.
Original post by Soul Wavel3ngth
Float is a double and math.h is included. I'm still relatively new to visual studios so I'm not quite sure what you mean by "compile with all warnings turned on". I've since reverted the code back to its original state before I included the fabs() but I'll see if it's possible to undo it if you'd like.


Not sure about c programming with visual studio, but sometimes warnings are switched off by default. Is there some form of lint with VS? However, if you post the program (or message me) Ill take a quick look.
Original post by mqb2766
Not sure about c programming with visual studio, but sometimes warnings are switched off by default. Is there some form of lint with VS? However, if you post the program (or message me) Ill take a quick look.


If you're talking about errors then no I haven't gotten any whilst using the fabs() function and compiling my code. Also, Am I supposed to use fabs() only with float variables? My entire code has been linked btw
difference is calcuated at the start and not again.
its before the number is entered and so could be anything.
Original post by mqb2766
difference is calcuated at the start and not again.
its before the number is entered and so could be anything.


It's weird because when I put difference after the point where the user is prompted to enter their number, my game no longer registers some of the other if/esle if statements and doesn't even reward me when I input the correct value.
Original post by Soul Wavel3ngth
It's weird because when I put difference after the point where the user is prompted to enter their number, my game no longer registers some of the other if/esle if statements and doesn't even reward me when I input the correct value.


put it where you think it should be and post again
Original post by mqb2766
put it where you think it should be and post again


I placed it at various points within the main for loop and outside the for loop but the end result remains the same. I suspect that the issue is with how I set up the win condition/the fabs function?
Original post by Soul Wavel3ngth
I placed it at various points within the main for loop and outside the for loop but the end result remains the same. I suspect that the issue is with how I set up the win condition/the fabs function?


It really would be best to post what youve tried / what you think is happening, but note that the two else if tests before the fabs() one are < and > so the only way the final else if(abs()) test will be performed is if the numbers are identical.
Original post by mqb2766
It really would be best to post what youve tried / what you think is happening, but note that the two else if tests before the fabs() one are < and > so the only way the final else if(abs()) test will be performed is if the numbers are identical.


At first I just pasted the difference variable just underneath the scan_f prompt and proceeded with other points within and out of the for loop. As said before, the introduction of the fabs() function as well as me creating the difference variable is now interfering with my game's ability to acknowledge when I input the correct number as well as the > or < conditions, I suspect it has something to do with the value of the difference variable being entered number - randomnumber.

when you said "if the numbers are identical" did you mean entering the same/similar number multiple times? If so, I've just tried that but my game still won't acknowledge any of the other conditions properly
Original post by Soul Wavel3ngth
At first I just pasted the difference variable just underneath the scan_f prompt and proceeded with other points within and out of the for loop. As said before, the introduction of the fabs() function as well as me creating the difference variable is now interfering with my game's ability to acknowledge when I input the correct number as well as the > or < conditions, I suspect it has something to do with the value of the difference variable being entered number - randomnumber.

when you said "if the numbers are identical" did you mean entering the same/similar number multiple times? If so, I've just tried that but my game still won't acknowledge any of the other conditions properly


Firstly, its worth learning how to use the debugger in VSC. Its precisely for these sort of problems where the code runs, but the behaviour is not what you expect. Can you post your current code.
Original post by mqb2766
Firstly, its worth learning how to use the debugger in VSC. Its precisely for these sort of problems where the code runs, but the behaviour is not what you expect. Can you post your current code.


sure
After entering the number and calculating the difference, can you put a printf() which prints the 3 variables: randomnumber, enterednumber, difference?
You seem to have random being 42 and the start yet its saying the correct guess is lower than 6?
Original post by mqb2766
After entering the number and calculating the difference, can you put a printf() which prints the 3 variables: randomnumber, enterednumber, difference?
You seem to have random being 42 and the start yet its saying the correct guess is lower than 6?

I decided to get rid of everything else below the printf() you've just instructed me to do and it seems that the game is registering all three variables. I could check and see if it continues to do so if I add the deleted stuff back in.
Yes, enter the conditions one at a time and enter a value which tests it works each time?

Tbh, it really would be good to learn to use the debugger. It does save time and does exactly the type of thing youre doing here.
(edited 1 year ago)
Original post by mqb2766
Yes, enter the conditions one at a time and enter a value which tests it works each time?

Tbh, it really would be good to learn to use the debugger. It does save time and does exactly the type of thing youre doing here.

Ok so I just added back fabs() as well as the printf() statement that tells the user that they win if their value is within +/-0.1 of the correct answer. The game wasn't acknowledging the printf() statement when I used +/-0.1 but is now acknowledging it when +/-0.10001. So I guess it was a problem with VS. I'll add back the other if/else if statements one-by-one to see if it still works after I take a break thnx
Original post by Soul Wavel3ngth
Ok so I just added back fabs() as well as the printf() statement that tells the user that they win if their value is within +/-0.1 of the correct answer. The game wasn't acknowledging the printf() statement when I used +/-0.1 but is now acknowledging it when +/-0.10001. So I guess it was a problem with VS. I'll add back the other if/else if statements one-by-one to see if it still works after I take a break thnx


Id order the else if statements so that you check to see if they win first then if not, offer advice about limits, too low, too high, This may have been your original problem as there was no "tolearance" on the too low / high advice.
(edited 1 year ago)

Quick Reply

Latest