void functions to count between numbers

Pages: 12
I am working on a program that uses void functions. one counts up from 0 to a single parameter. One counts down to 0 from a single parameter and the third will count between two parameters. The main body of the program asks the user to input two positive numbers. The first number enter will be used for the count up and count down. while both numbers will be used for the count between.

As far as the functions, this is what I have so far...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int counter = 0;
int a;   //user will input number
int b;   //user will input number
 void count_up (0, int a)
   for (int counter =1; counter<=a; counter++)
   {
    cout << counter << endl;
   }

 void count_down (int a, 0)
  for (int counter = a; counter >=1; counter--)
  {
   cout << counter <<endl;
  }
 void count_between(int a, int b) //this function is where I am getting confused

   if (a <=b)
   {
    count_up(int a, int b)
   }
   else
   {
   count_down(int a, int b)
   }
What's the problem?

I notice you have a lot of syntactical errors. You should reread whatever tutorial/textbook you have and fix those before trying to finish up the logic.
I guess I should have been more specific. I definitely know there are some syntax errors. However those are fairly easy to fix.
In this specific instance, I was just looking for advice more so on the 'logic' of the 'functions'. This obviously isn't the entire program.

If the program were to be executed it should show:

Please enter 2 numbers:
4 9 //user input, user can input any 2 positive numbers

0 1 2 3 4 //result of function count_up
4 3 2 1 0 //result of function count_down
4 5 6 7 8 9 //result of function count_between
If count_up takes one parameter as you indicated in the text above (in contrast to what you show in the code) and count_down also only takes one parameter, you may not use either function in the implementation of count_between.

1
2
3
4
5
6
7
8
9
void count_between(int a, int b)
{
    int step = a > b ? -1 : 1;

    for (int i = a; i != b; i += step)
        std::cout << i << '\n';

    std::cout << b << '\n';
}
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
void count_up( unsigned int n, unsigned int m = 0 )
{
   do
   {
      std::cout << m << ' ';
   } while ( m++ != n );

   std::cout << std::endl;
}

void count_down( unsigned int n, unsigned int m = 0 )
{
   do
   {
      std::cout << n << ' ';
   } while ( n-- != m );

   std::cout << std::endl;
}

void count_between( unsigned int n, unsigned int m )
{
   ( n < m ) ? count_up( n, m ) : count_down( n, m );
}
Last edited on
vlad or anyone:

Can you tell me what the ' ' means in the line of code:

std::cout << m << ' ';

also

in the line of code:

(n <m) ? count_up (n,m) : count_down (n,m);

what does the ? mean and what does the : mean

Thank you.
Can you tell me what the ' ' means

The same as any character inside single-quotes means. It's a character, of type char. It's adding a space to the end of the text output - presumably to separate it from the next bit of text.

(n <m) ? count_up (n,m) : count_down (n,m);

It's the conditional operator, a.k.a the ternary operator. A Google search should find you more info.
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
#include <iostream>

using namespace std;

int main()
{
    int a;
    int b;

    cout << "Enter 2 numbers please: " << endl;
    cin >> a;
    cin >> b;

   return 0;
}

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 (int a, int b);
   }
  
   else
   {
     count_down (int a, int b);
   }
}


I was going off of the function code that vlad provided, but changed it up to use an if else condition in the count_between function instead. The above is my full program.

An error is showing for the count_up and count_down in the if-else condition of the count_between function. Error says: expected primary expression before int.
Lines 41 and 46 have an incorrect syntax. That's not how you call a function.
Thanks MikeyBoy,

I took the 'int' out of the call to functions and now it compiles. YAY!

within the if else condition:
count_up (a,b);
count_down (a,b);

BUT when I run my program. It asks the user for 2 numbers.
I enter two numbers. and then the program quits. It does not go through the process of (for example if the 2 numbers enter were 3 and 9)

Enter 2 numbers please: //this part works
3 9 // user input (program ends after entering 2
numbers
0123 //should be the output of function count_up
3210 // should be the output of function count_down
3456789 //should be the output of function count_between

any thoughts on this? is it because of the location of the 'return 0' code or something else??

Thanks for your help!
Well, that's because your main function doesn't actually call any of the functions you've written! It just asks the user for two numbers and then exits.

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 =0);
void count_down (int a, int b = 0);
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 (int a, int b);
   }
  
   else
   {
     count_down (int a, int b);
   }
}


So I called the functions in main. I was referencing my book for this part and it looks like they created a function called getNumbers (which is similar to what I am trying to do).
So I created a new function called getNumbers. In the book they created different variables for inside the main function.(i dont know why, but I just went with it.)

so now the program compiles and runs, but the the following is the output:

Enter 2 numbers please: // prompt for user
3 // user input
9 // user input

0
0
0
In C and C++, parameters are passed into functions by value, not reference. This means that the values of a and b that get set in getNumbers are not passed back to the variables in main. Are you sure that's what was in your book? Because if it is, your book is error-ridden and unreliable.

If you want to pass parameter values back to the calling code, you'll need to use references (or pointers, though I'd recommend references).

And your function calls in count_between are still wrong.
The book was demonstrating a call by reference. But it says to put the '&' after the variable type. So I did this for int a and int b. But got error, because int b is set to 0. So I deleted '&' from int b. Now code compiles and runs, but it is an ifinite loop of numbers:

it asks for the 2 numbers. After entering the 2 numbers the output is infinite counting up numbers. same for counting down....it counts up numbers doesn't stop counting up.


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
62

#include <iostream>

using namespace std;

void count_up (int& a, int b =0);
void count_down (int& a, int b = 0);
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 (a, b);
   }
  
   else
   {
     count_down (a, b);
   }
}
1
2
3
4
5
6
void count_between (int& a, int b)
{
  if (a < b)
  {
    count_up (b, a);
   }
Okay. Now I am getting somewhere! With my current code(below), the output is as follows:

enter 2 numbers please:
3 //user input
9 //user input

0
1
2
3
4
5
6
7
8
9

9
8
7
6
5
4
3
2
1
0

-1
0

So some of the problems are:
1. it is suppose to take the users FIRST number input for the count_up and count_down (NOT the second number)

2. I wanted the counting for each to happen on the same line:
example:
9 8 7 6 5 4 3 2 1 0

3. where is the count between?
currently output for this part looks like:
-1
0
BUT it should be:

3 4 5 6 7 8 9


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 =0);
void count_down (int& a, int b = 0);
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);
   }
}
Are you sure the code you've posted here is your actual code? Because your getNumbers function as posted here still doesn't pass the value of b back to main.

yes, this is the code. Is it not passing the value of b back to main, because I don't have the '&' after 'int' in the 'int b' parameter?

Sorry, I am getting really confused at this point.....
I fixed the problem where I wanted the counting to happen on the same line. I deleted the endl; and substituted " ";

and when I ran the code again. it did use the first number as the count up and count down number.

but I am still getting the

-1 0

for the count_between part........?
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;
}
Last edited on
Pages: 12