void functions to count between numbers

Pages: 12
I checked my code in the vim editor to make sure it matched my most recent post with the program. It does match up.

I did try the numbers 3 and 9 again to check. It did display part of the correct outcome:
When 3 and 9 are entered by user the outcome is currently showing as:

0 1 2 3 //correct
3 2 1 0 //correct
-1 0 // not correct outcome, what it should have been is 3 4 5 6 7 8 9

so it seems that everything is working except the count_between function..... or something related to it....

Any suggestions on what the problem could be?
Thanks!
As I tried to explain before, your problem is references, declare your functions as follows:
1
2
3
4
void getNumbers (int& a, int& b) // using both parameters as references
void count_up (int a, int b=0) // no references
void count_down (int a, int b=0) //no references
void count_between(int a, int b) // no references 


You will have to make the appropriate changes when you define them.
I did try the numbers 3 and 9 again to check. It did display part of the correct outcome:
When 3 and 9 are entered by user the outcome is currently showing as:
Your getNumbers function means that b is currently set as 0, not 9.
0 1 2 3 //correct, only because b has been wrongly set as 0
3 2 1 0 //correct, not really, your value of a is now -1 because of the function
-1 0 // not correct outcome, what it should have been is 3 4 5 6 7 8 9 it is the correct output according to what a and b are


Bolded parts added by me. As previously mentioned, by passing an argument by reference, you are actually changing the value of the variable, rather than changes only being contained within the function.

Also, when calling the count_up and count_down functions for the first 2 tasks, don't bother putting a second argument in, it will default to the 0 default mentioned unless you override it.
Last edited on
Also, when calling the count_up and count_down functions for the first 2 tasks, don't bother putting a second argument in, it will default to the 0 default mentioned unless you override it.

Um, what? I think you've misunderstood how default argument values work.

The default values get used if you don't pass an argument in the function call.

If you do pass an argument in, then its value gets used instead of the default.
So I have made the suggested changes to the references, but left the arguments alone.
Now my program is back to running an infinite loop of counting up numbers.....!?!
Below is how the code currently looks:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>

using namespace std;

void count_up (int a, int b);
void count_down (int a, int b);
void count_between (int a, int b);
void getNumbers (int& a, int& b);

int main()
{
    int firstNum;
    int secondNum;

    getNumbers(firstNum, secondNum);
    count_up(firstNum, secondNum);
    count_down(firstNum, secondNum);
    count_between(firstNum, secondNum);

    return 0;
}

  void getNumbers (int& a, int& b)
  {    
    cout << "Enter 2 numbers please: " << endl;
    cin >> a;
    cin >> b; 
    }

void count_up (int a, int b =0)
{
  do
   {
     cout << b << endl;
   } while (b++ !=a);

   cout << endl;
 }

void count_down (int a, int b = 0)
{
  do
   {
     cout << a << endl;
   } while (a-- != b);

   cout << endl;
}

void count_between (int a, int b)
{
  if (a < b)
  {
    count_up (b,a);
   }
  
   else
   {
     count_down (a, b);
   }
}
Change lines 16 and 17 to count_up(firstNum) and count_down(firstNum) respectively, that sets the other parameter to 0 (the default provided) so you get the output you want.

You may want to edit the functions to make sure you are counting up from the smaller number and down from the larger number though.
Last edited on
I did as you suggested, but when I compiled it gave several errors directly related to not having a second parameter or argument or something. specifically for those two lines of code.

You need to specify the default parameter value in the function declaration, not the definition.
THANK YOU all so much for your help. I adjusted line 16 and 17 as manudude suggested and I adjusted the defaulted parameter value in the function declaration (NOT the definition) as mikey boy suggested.

IT WORKS! :)

Unfortunately that was only the beginning..... :|

now I have to "add a function to get a positive integer as a string, get_positive_int().

and add error.checking ....meaning if the user doesn't enter a positive value then they get another prompt and chance....as many as it takes for them to enter positive integers.......

suggestions?
closed account (Dy7SLyTq)
look up string stream. its not actually as hard as it sounds
The only think I can think to add to the code is the error checking. by adding a do while loop just under the cout prompt for the user to enter the numbers:

1
2
3
4
5
6
7
8
9
10
11
12
13
void getNumbers(int& a, int& b)
{
  cout << "Enter 2 numbers, please:" << endl;
  cin>> a;
  cin >> b;

  do
  {
    cout >> "You did not enter positive numbers, try again:" >> endl;
    cin>> a;
    cin >>b;
  }while (a<=0 && b <=0 && ((int&)a != a && (int&)b !=b));
}


The idea is that a <=0 and b <=0 would check for negative numbers and the (int)a !=a and (intb != b) would check for none number entries

it is giving error when compile of no match for operator 'a' in cout "you did not enter positive numbers"

got it my cout had >> instead of <<.....

okay, it compiles now. results:

when I enter two negative numbers i get the expected statement to re enter two numbers.

when I enter first negative number and second positive number, I get infinite loop of numbers

when I enter both positive numbers (first number smaller than second), I get the "you did not enter positive numbers prompt:

when I enter both positive numbers (first number larger than second), I get the correct results printout: (ex 9, 4)
0 1 2 3 4 5 6 7 8 9
9 8 7 6 5 4 3 2 1 0
9 8 7 6 5 4

when I enter first positive and second negative, I get the correct prompt that I did not enter two positive numbers.

When I enter characters or symbol only get to the first number after hitting enter after first character it runs:
0
0
0
Last edited on
okay, I think I need to change the loop to a while loop. because the do while loop will run through once before error checking, which is why I am getting the cout I did not enter positive numbers.

but for the other un expected print outs I dont know.....?

Changed the do while loop to a while loop:

1
2
3
4
5
6
while(a <=0 && b<=0 && ((int&)a !=a && (int&)b !=b))
{
 cout << "you did not enter 2 positive numbers, please try again:" << endl;
cin >> a;
cin >> b;
}


This fixed the problem when I entered 2 positive, it gave the message that I did not enter positive numbers. So that is fixed.

But I still have problem with:
numbers enter:
-1 (so a negative number in general for first number)
8 ( a positive number for second number)
result: (infinite loop)

numbers:
8
-1
Result: the program ran as if the error check was not checking:
0 1 2 3 4 5 6 7 8
8 7 6 5 4 3 2 1 0
8 7 6 5 4 3 2 1 0 -1

numbers:
-3
-6
Result: infinite loop of numbers counting up

something is wrong with my while loop....? Maybe the && between the first a and b condition and the && between the int a and int b should be || in both spots.
I will try that......
Last edited on
while(a<=0 || b <=0 && ((int&)a != a || (int&) b != b))
{
cout<< " ......not positive:"
cin >>a;
cin >>b;
}

user enters:
-1
8
results: you did not enter two positive numbers, please try again:

user enters:
8
-1
results: (not as expected)
0 1 2 3 4 5 6 7 8
8 7 6 5 4 3 2 1 0
8 7 6 5 4 3 2 1 0 -1
program ends

user enters:
f (upon hitting enter the infinite loop of "you did not enter two positive numbers, please try again:" runs until hitting control c to stop the loop.

user enters:
8
f
results:
0 1 2 3 4 5 6 7 8
8 7 6 5 4 3 2 1 0
8 7 6 5 4 3 2 1 0

user enters:
-3
f
results: infinite loop of "you did not enter positive......."


user enters:
8
4
results: as expected

user enters:
4
8
results: as expected
Topic archived. No new replies allowed.
Pages: 12