Jump to content
I have to write a program which solves for the roots of a quadratic equation (ax^2 + bx + c). I have all the math down and the program works, its just handling bad input. The user has to input 3 numbers, formatted as such: "a,b,c" (ex. 1,2,1).  The problem arrises when I enter in something as such ("a,b,c" or "a,b" i.e. non-number characters). From what I have tested, if I enter in a good input first (1,2,1) and then I try a bad input (a, b ), the values from before are kept. I need some way to test whether there is a valid input or not. I have posted my code below.

 

From what I have read, scanf is very bad for when you cannot control how the input if formatted, as an input for an integer such as "123abc" would count as a good input, since the "123" is read fine but scanf just 'ignores' the abc.

 

For the purpose of this assignment, we need to use scanf

 

My code is posted below (forum won't let me post my code as its blocking me so I made a pastebin link):


Link to comment
https://linustechtips.com/topic/526140-c-scanf-bad-input/
Share on other sites

Link to post
Share on other sites

Thanks everyone for you're suggestions, I will definitely look into those!

 

I talked to my prof about it, and it turns out that we are able to assume that a bad input will only be a single character instead of something more complicated like "12a", which allows us to just check the value of scanf as an int and as long as it is equal to the number of floats I am trying to get, then that is fine.

 

:)

Link to comment
https://linustechtips.com/topic/526140-c-scanf-bad-input/#findComment-6993929
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×