Question turning my code into a Function

My code works perfectly but needs to be a function. I'm just not sure if I could turn the whole code into one function or if I need to break it down into individual functions for each answer? It is supposed to take a range of numbers from the user and square and cube them and output them back to the user in a list.

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
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;



int num;
int num2;
int numsquard;
int numcubed;



int main(){
	
	cout << "Enter a number for the starting value" << endl;
	cin >> num;
		while (num < 1 || num > 50 ){
		cout << " Number must be between 1 and 50. Please try agian." << endl;
		cin >> num;}
			if(num >= 1 && num <= 50){
			cout << "Enter a number for the ending value." << endl;
			cin >> num2;}
				while (num2 <= num || num2 > 100){
					cout << "Please enter a number greater that the starting value and less than 101." << endl;
					cin >> num2;}
	cout << setw(10) <<"Number" << setw(10) << "Squared" << setw(10) << "Cubed" << endl;
	for(num;num <= num2;num++){
		numsquard = pow(num,2);
		numcubed = pow(num,3);
		cout << setw(10) << num << setw(10) <<numsquard << setw(10) <<numcubed << endl;}
	
	
	return 0;
	
}
closed account (2UD8vCM9)
You could do something like this
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
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int GetInt(int min, int max)
{
	int returnval;
	cin >> returnval;
	while (returnval < min || returnval > max)
	{
		cout << "Number must be between " << min << " and " << max << ". Please try again." << endl;
		cin >> returnval;
	}
	return returnval;
}

void PrintSquareCube(int start, int end)
{
	int numsquared;
	int numcubed;
	cout << setw(10) << "Number" << setw(10) << "Squared" << setw(10) << "Cubed" << endl;
	for (int i = start; i < (end+1); i++) {
		numsquared = pow(i, 2);
		numcubed = pow(i, 3);
		cout << setw(10) << i << setw(10) << numsquared << setw(10) << numcubed << endl;
	}
}


int main() {

	int start, end;
	cout << "Enter a number for the starting value." << endl;

	start = GetInt(0, 50);

	cout << "Enter a number for the ending value." << endl;
	end = GetInt(start, 100);

	PrintSquareCube(start, end);
	
	system("pause");
	return 0;

}


If you want to use functions instead of just having it all in main
Something like this, perhaps:

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
#include <iostream>
#include <iomanip>

int get_number( int min_value, int max_value ) ;
void print_powers_in_range( int number_from, int number_to, int width ) ;

int main() {

    const int min_start = 1 ;
    const int max_start = 50 ;
    const int max_end = 100 ;
    const int width = 10 ;

    std::cout << "starting value [" << min_start << "..." << max_start << "]? " ;
    const int start_value = get_number( min_start, max_start ) ;

    std::cout << "ending value [" << start_value << "..." << max_end << "]? " ;
    const int end_value = get_number( start_value, max_end ) ;

    print_powers_in_range( start_value, end_value, width ) ;
}

int get_number( int min_value, int max_value ) {

    int number ;
    if( std::cin >> number && number >= min_value && number <= max_value ) return number ;

    std::cout << "enter a number in [" << min_value << "..." << max_value << "]: " ;

    // you may want to ignore these two lines for now
    std::cin.clear() ; // clear a possible failed state (for eg. if letters were entered instead of digits)
    std::cin.ignore( 1000, '\n' ) ; // throw the bad input away

    return get_number( min_value, max_value ) ; // try again (repeat the same function)
}

void print_powers( int number, int width ) {

    const int nsquared = number * number ;
    const int ncubed = nsquared * number ;
    std::cout << std::setw(width) << number << std::setw(width) << nsquared << std::setw(width) << ncubed << '\n' ;
}

void print_powers_in_range( int number_from, int number_to, int width ) {

    std::cout << std::setw(width) << "Number" << std::setw(width) << "Squared" << std::setw(width) << "Cubed\n" ;
    for( int number = number_from ; number <= number_to ; ++number ) print_powers( number, width ) ;
}
Thanks, They both really helped. Starting to understand functions
Topic archived. No new replies allowed.