Can anyone help me find a bug in few lines of code?

When i type in ( 2 5) and ( 10 20) the answer is 40/0. I have something wrong in my denominator but I can't see it.
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
  #include <iostream>

using namespace std;

struct Fract {
    
    int num;
    int denom;
};

Fract sum(Fract f1, Fract f2){
    Fract result={((f1.num * f2.denom) + (f2.num * f1.denom)), (f1.denom * f2.denom)};
    return result;
}

int main(){


    int num1,num2, denom1, denom2;
    
    cout<<"Enter numerator and denominator: ";
    cin>> num1>>denom2;
    cout<<"Enter another numerator and denominator: ";
    cin>> num2>>denom2;
    
    Fract f1= {num1, denom1};
    Fract f2= {num2, denom2};
    Fract result= sum(f1,f2);
    cout<<"The result is "<<result.num<<"/"<<result.denom<<endl;
    
    
    
}
Hi,

In function 'int main()':
12:50: warning: 'denom1' is used uninitialized in this function [-Wuninitialized]
19:20: note: 'denom1' was declared here


This is a clue, but the change needs to happen on line 22.

Make the compiler do work for you, set the warning levels high. I used cpp.sh the gear icon, and turned on all 3 warning levels :+)
Topic archived. No new replies allowed.