Rewriting a Program using Functions

So I wrote a program that basically takes two numbers and counts from 0 to the first number and back down, then will count the numbers between the two. It works fine, but now I need to rewrite it using three functions (one for counting up, one for counting down, and one for counting between). I don't really know how to use functions yet. I was going to try to use a void function for one of them, but I don't know how to format everything. How much change needs to be made to my program to use three different functions? Here is my code:

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>

using namespace std;
int main(){
	int x;
	int y;
	int i;
	int cont;

	
	cout << "Please enter 2 ints. \n";
	cin >> x;
	cin >> y;
	
	while(x<0 || y<0){
		cout << "The ints must be positive!\n";
		cout << "Please enter 2 ints. \n";
		cin >> x;
		cin >> y;
		}
		
	
	cout << "You have selected " << x << " and " << y << ". \n";
	
	for(i=0;i<=x;i++) {
		cout << i << " ";
		}
	
	cout << "\n";		
	
	for(i=x;i>=0;i--) {
		cout << i << " ";
		}	
	
	cout << "\n";
	
	if(x>y){
		for(i=x;i>=y;i--) {
		cout << i << " ";
		}
	}
	if(x<y){
		for(i=x;i<=y;i++) {
		cout << i << " ";
		}
	}
	
	cout << "\nDo you want to run this again using new ints? Enter 1 for yes, or 2 for no. \n";
	cin >> cont;
	
	while(cont!=2){
		cout << "Please enter 2 ints. \n";
		cin >> x;
		cin >> y;	
		
		while(x<0 || y<0){
			cout << "The ints must be positive!\n";
			cout << "Please enter 2 ints. \n";
			cin >> x;
			cin >> y;
			}
	
		for(i=0;i<=x;i++) {
			cout << i << " ";
			}
	
		cout << "\n";		
		
		for(i=x;i>=0;i--) {
			cout << i << " ";
			}	
		
		cout << "\n";
		
		if(x>y){
			for(i=x;i>=y;i--) {
			cout << i << " ";
			}
		}
		if(x<y){
			for(i=x;i<=y;i++) {
			cout << i << " ";
			}
		}
	cout << "\nDo you want to run this again using new ints? Enter 1 for yes, or 2 for no. \n";
	cin >> cont;
	}
return 0;
}


Thank you for any help.
Last edited on
You need to write only one function that will have two parameters. If the first parameter is greater than the second (more precisely if the first argument is greater than the second) you count values down otherwise count them up.
http://www.cplusplus.com/doc/tutorial/functions/

Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

void countUp(int num)
{
    for (int i = 0; i <= num; i++)
        std::cout << i << ' ';
}

int main()
{
    int x = 5;
    countUp(x);
    
    return 0;
}

Output:
0 1 2 3 4 5


I hope that gets you on track.
Yes that helped a lot monad! Thank you very much!
Topic archived. No new replies allowed.