#something wrong in do while loop please correct this

36 messages · Page 1 of 1 (latest)

thin path
#

program asks to enter enter grade between 0 to 100 if its true it will print valid grade if its less than 0 or above 100 it should print invalid grade. This code almost correct but something wrong look at output.

hidden sluiceBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

thin path
#

if i input value greater than 100 it prints "invalid grade" as well as "you have entered a valid grade %d"

oblique marlin
#

can you simulate on your head or pen/paper or with a debugger what is happenning when user enters 6453?

#

like step by step of the program flow from top to bottom

#

then i think you'll see why

thin path
#

the printf in while statement will only run if condition in while satisfy right? which is grade must be >0 and < 100?

oblique marlin
#

do you mean the one on line 10 or which

thin path
oblique marlin
#

that while governs when the do-while loop runs or exits yeah

thin path
#

my question is why it prints invalid grade as valid grade the last line in output?

oblique marlin
#

the printf on line 10 is outside the do-while loop's block

#

it will run when the loop exists

#

it is unconditional

thin path
#

oh how do i fix this

oblique marlin
#

well delete that printf? :p

#

but you want to say "that was valid" on valid inputs?

thin path
#

hah but it should also print when i enter a valid grade between 0 to 100

#

yes

oblique marlin
#

i see

#

after user enters the grade you are checking if it was invalid right?

#

then printing "invalid grade"

#

if it is not invalid, i.e., if that if statement's condition is not satisfied, then it is valid grade

thin path
#

yes

oblique marlin
#

so what goes together with if to say "otherwise"?

#

else, isn't it

#
if (grade_is_good)
    print("ok")
else
    print("bad")
#

^pseudocode, not valid C code at the moment

thin path
#

oh got it i was dumb

oblique marlin
#

cool

thin path
#

now it works, thanks

oblique marlin
#

np

hidden sluiceBOT
#

@thin path Has your question been resolved? If so, type !solved :)