Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

checking user input answer

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I am new to C and I am having a hard time figuring out my issue here. Basically the program generates two random numbers based on the users selection, and the user enters an answer to the problem. If its right it will say so, and if its wrong it will say so as well. I have the random number generated, but no matter what I input it always marks the answer as correct. I just provided a bit of the code as it is all the same with functions containing add, multiply, subtract, and etc. I just didnt want to post 300+ lines of repetitive code. I guess my problem is finding out if the user entered an answer that matches the total of the random numbers

 
Rancher
Posts: 234
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ashton - haven't tried compiling/running your code, but I did spot a couple of lines which means it's not going to work:
That's assigning rightAnswer to answerAttempt - presumably you want to test whether they are equal, which is ==.

Classic C - take 2 operators which have 2 very different meanings and make them look similar, with syntax that makes both valid. Great for beginners
 
John Matthews
Rancher
Posts: 234
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And I can see you've used == elsewhere so I guess the = were just typos.

Your compiler should have warned you about them. For example using with compiler gcc and command line option -Wall to enable 'all' warnings results in it telling me 'suggest parentheses around assignment used as truth value'. Is there a similar option you can use with your compiler?
 
John Matthews
Rancher
Posts: 234
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry - you probably already know this but I forgot to say that (answerAttempt = rightAnswer) returns true unless rightAnswer is 0, hence the behavior you've observed.
 
John Matthews
Rancher
Posts: 234
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just spotted some more bugs -
The ++ operator has higher precedence than *, so you need to do:
otherwise you are just reading (and throwing away) the value pointed to by ttlIncorrect before incrementing ttlIncorrect. And the * is missing in line 56.

You could have got the compiler to spot this bug by using
This tells the compiler that the values of pointers ttlCorrect and and ttlIncorrect are constant, or they should be. So your *ttlIncorrect++, which increments the pointer instead of what it's pointing at, would be flagged as an error.
 
Ashton Colby
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

John Matthews wrote:Hi Ashton - haven't tried compiling/running your code, but I did spot a couple of lines which means it's not going to work:
That's assigning rightAnswer to answerAttempt - presumably you want to test whether they are equal, which is ==.

Classic C - take 2 operators which have 2 very different meanings and make them look similar, with syntax that makes both valid. Great for beginners



I did try that before, and then every answer came back wrong
 
John Matthews
Rancher
Posts: 234
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried correcting the other bugs I mentioned?

If it still doesn't work, how much debugging have you tried? For example, just add some printfs to show you the values of variables eg. output answerAttempt and rightAnswer, check they're the values you think they are. Is it even going into the function(s) you think it's going into? Of course if you have a development environment that allows to you single-step through code and see what it's doing and where it's going wrong, then great, give that a go.

Knowing how to debug code is as useful/important a skill as knowing how to write it.
 
lowercase baba
Posts: 12908
63
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use a lot more printf statements to validate your data.  Make sure what your code is doing is what you THINK it is doing.  

if your If block isn't taking the side you think it should, I would print out answerAttempt and rightAnswer, surrounding them with something like a '#' to make sure there are no trailing spaces, new lines, etc.
 
John Matthews
Rancher
Posts: 234
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great minds think alike
 
There are 29 Knuts in one Sickle, and 17 Sickles make up a Galleon. 42 tiny ads in a knut:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic