Hi Jaymz,
There are a few basic problems with the code (although probably NOT the problems that you actually want us to help with!)
First is that
void main()
is non-standard C++ and you really SHOULD be using
int main()
(or even preferably
int main(int argc, char *argv[])
). In which case you need to include
return 0;
in main().
Second, there are none of those lovely comments which are supposed to be there to help other people make sense of what you're trying to do.
Third, you've got a curious mix of C and C++ code in there -- why precisely are you using both cout and printf()?
May I suggest that you fix these and see if it doesn't help? This may seem pointless, but believe me when I say if you insert CLEAR comments which indicate when you are entering a loop, checking an exit condition, etc, then you will see your problem -- a quick look at the section of the function generate(x) below should serve to demonstrate both why your program isn't working as you want AND the advantage of clear commenting;
1 2 3 4 5 6 7 8 9 10 11 12
|
int m, a, c, p1, p2, f, n, d;
m = 8;
// if else loop
// proceeds if (x<m), else prints error message and exits generate(x)
if (x < m)
{
a = 5;
c = 7;
|
But you don't WANT to exit if (x<m) is false, right?
For what it's worth -- I usually start small programs like this by writing comments explaining what I'm trying to achieve;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
// prompt user for an input. if input is out of range, prompt again (need
// an exit condition, though). if input is within range, then perform
// calculation
// top of input loop - use cin to get input, use "q" as exit condition
// both "q" and valid input will terminate loop - but only valid input
// will call the calculation
// if input valid perform calculation (and terminate loop)
// if input invalid show error message
// if input "q" do nothing (and terminate loop)
// bottom of input loop
// And so on...
|
All I then need to do is slip in the relevant code under each heading.
Hope it helps in more than one way!
Regards,
muzhogg