C programming help

Watch this thread
Soul Wavel3ngth
Badges: 15
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#1
Report Thread starter 4 weeks ago
#1
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/attachmen...528_213647.jpg

for added context, "difference" was set to be the difference between the number the user inputs and the randomly generated number beforehand.
0
reply
mqb2766
Badges: 19
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#2
Report 4 weeks ago
#2
(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/attachmen...528_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
0
reply
Soul Wavel3ngth
Badges: 15
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#3
Report Thread starter 4 weeks ago
#3
(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.
0
reply
mqb2766
Badges: 19
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#4
Report 4 weeks ago
#4
(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.
0
reply
Soul Wavel3ngth
Badges: 15
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#5
Report Thread starter 4 weeks ago
#5
(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
Attached files
0
reply
mqb2766
Badges: 19
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#6
Report 4 weeks ago
#6
difference is calcuated at the start and not again.
its before the number is entered and so could be anything.
0
reply
Soul Wavel3ngth
Badges: 15
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#7
Report Thread starter 4 weeks ago
#7
(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.
0
reply
mqb2766
Badges: 19
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#8
Report 4 weeks ago
#8
(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
0
reply
Soul Wavel3ngth
Badges: 15
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#9
Report Thread starter 4 weeks ago
#9
(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?
0
reply
mqb2766
Badges: 19
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#10
Report 4 weeks ago
#10
(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.
0
reply
Soul Wavel3ngth
Badges: 15
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#11
Report Thread starter 4 weeks ago
#11
(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
Attached files
0
reply
mqb2766
Badges: 19
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#12
Report 4 weeks ago
#12
(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.
0
reply
Soul Wavel3ngth
Badges: 15
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#13
Report Thread starter 4 weeks ago
#13
(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
Attached files
0
reply
mqb2766
Badges: 19
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#14
Report 4 weeks ago
#14
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?
0
reply
Soul Wavel3ngth
Badges: 15
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#15
Report Thread starter 4 weeks ago
#15
(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.
Attached files
0
reply
mqb2766
Badges: 19
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#16
Report 4 weeks ago
#16
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.
Last edited by mqb2766; 4 weeks ago
0
reply
Soul Wavel3ngth
Badges: 15
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#17
Report Thread starter 4 weeks ago
#17
(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
0
reply
mqb2766
Badges: 19
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#18
Report 4 weeks ago
#18
(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.
Last edited by mqb2766; 4 weeks ago
0
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Back
to top
Latest

Were exams easier or harder than you expected?

Easier (63)
27.51%
As I expected (71)
31%
Harder (87)
37.99%
Something else (tell us in the thread) (8)
3.49%

Watched Threads

View All