Put my code in a function

Hey guys this is my code i want to put the part where i have it do multiplication and addition into functions. and then call them so that it can run the addition and multiplication. Heres 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
# include <iostream>

using namespace std;

int main(){
    
    int num1,num2,num3,num4; 
    int numerator,denominator; 
    int choice;
    char again = 'y';
    int array[]={2,3,5,7,11,13,17,19,23,29,31}; 
    
    while (again == 'Y' || 'y') {
        
    
    cout << "Enter numerator of 1st fraction: ";
    cin >> num1;
    cout << "Enter denominator of 1st fraction: ";
    cin >> num2;
        while(num2==0){
            cout << "Denominator cannot be 0. Enter a new number: ";
            cin >> num2;
        }
    cout << "Enter numerator of 2nd fraction: ";
    cin >> num3;
    cout << "Enter denominator of 2nd fraction: ";
    cin >> num4;
       
        while(num4==0){
            cout << "Denominator cannot be 0. Enter a new number: ";
            cin >> num2;
        }
    cout << "Would you like to multiply or add your fractions?(1=Multiply,0=Add): ";
    cin >> choice;
    
        if(choice==1){
            
            numerator = (num1 * num4) + (num2 * num3);
            denominator = num2 * num4;
        }
    
    if (choice==0){
    numerator= (num1*num4) + (num2*num3);
    denominator = num2*num4;
    }     
        
        cout << "The multiple/addition of your fractions is: " <<numerator;
        cout << "/" << denominator << endl;
        
        for(int i=0;i<10;i++){
            if(numerator % array[i]==0 && denominator % array[i]==0)
            {
                numerator/=array[i];
                denominator/=array[i];
                i=0;
            }
        }
        
	cout << "The reduced multiple/addition of your fractions is: " <<numerator;
	cout << "/" << denominator << endl;
	
        cout << " Would you like to go again (Y or N): ";
        cin >> again;
	
    return 0;
}
}


i tried doing this for functions

void sum(int a, int b, int c, int d){
int numerator,denominator;
numerator = (num1 * num4) + (num2 * num3);
denominator = num2 * num4;
}

void mult(int a, int b,int c,int d){
int numerator,denominator;
numerator= (num1*num4) + (num2*num3);
denominator = num2*num4;
}

but this doesn't work. any help would be appreciated thanks
it doesnt work because you redeclared numerator and denominator as locals.

try it like this
1
2
3
4
5
void sum(int a, int b, int c, int d, int& numerator, int& denominator)
{
    numerator = (num1 * num4) + (num2 * num3);
    denominator = num2 * num4;
}
Further more on line 13:

while (again == 'Y' || 'y') {

Does not do what you think it does. You need to write it like so:

while (again == 'Y' || again == 'y') {
Topic archived. No new replies allowed.