![]() |
Hi everybody! B)
I'm a beginner in C programming so I'm still learning the language. I decided that to understand the language well I would write simple programmes. So while learning about if-loops I got the idea of doing a simple math programme so I would get familiar to if-loops. But there's one problem - I would like the programme to return to the question if you've been writing the wrong answer . Could anybudy help with that thing? and by the way, I'm also a new member on this forum :D |
Use a loop:
:
#include <stdio.h> |
No offence Eggy, but that's scary. Try this:
:
int number = 32;Hopefully you'll understand that - it's about as far as I can go without confusing myself when I come back to look at it. |
>No offence Eggy, but that's scary.
How is it scary (aside from my inability to distinguish 1 from 2[*])? By removing the proper error handling code, and assuming an environment that flushes the output stream automatically, the code can be tightened up to this: :
#include <stdio.h>[*] My excuse is that I'm getting senile. |
Fine... here's the same thing in C:
:
int number = 32;Granted, it's nowhere near perfect, but it's a start. As you might have noticed, I'm not exactly a pro. Of course, your solution minus the error checking wins. And about the redundant code: it's either that or use flags, if you want to signal a wrong answer. And I don't like flags :P |
>And I don't like flags
You already have a flag sleeping in count, why not use it to make your code simpler? :) You could also duplicate the test for a correct answer and use that instead of a flag if you feel the need to avoid flags at all costs. Also, even though they aren't used that often, a do..while loop is best suited to this example because the question is asked at least once. The alternatives are an infinite loop that breaks from the body, initialization of n1 and n2 that guarantees that the condition will be false, or redundant code. :
#include <stdio.h>:
#include <stdio.h> |
Quote:
|
| All times are GMT -5. The time now is 1:18 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC