First of all, your count_up function is really insecure, I put in 3 and 9, it was going into pretty high numbers since it was secondNum (b) increasing. I assume it's the same on the count_down function as firstNum (a) decreases, I didn't bother to wait it out.
Out of the 4 functions you have, only the getNumbers function should have its arguments passed by reference (and both a and b should be entered by reference). The other 3 can just be entered normally.
ps. Also, you said in the first post that you wanted to count up from 0 and then down to 0, why are you calling the argument with both numbers? It should be count_up(a,0) and count_down(a,0) respectively.
pss. Also for count_between, when a<b, don't you want to be counting up from a to b rather than b to a?
ugh, so many edits :/
I feel I should explain why your code is doing what it is, here's your declared functions and the calls as you last posted them:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
void count_up (int& a, int b =0); //a will change, b will not
void count_down (int& a, int b = 0); // same
void count_between (int& a, int b); // same
void getNumbers (int& a, int b); // same
int main()
{
int firstNum;
int secondNum;
// let's pretend your numbers are 9 and 3
getNumbers(firstNum, secondNum); //firstNum=9, secondNum=0!
count_up(firstNum, secondNum);//subject to what I mentioned above, this is fine
count_down(firstNum, secondNum); //firstNum becomes -1
// the while loop says if a!=b BEFORE 1 is taken off, it still does the subtraction
count_between(firstNum, secondNum); //As -1<0, it does count_up between -1 and 0.
return 0;
}
|