I’m writing a fraction simplifier in C++. I am trying to compare two values in a struct to see which one is bigger, neither of the following work:
void simplify(struct Fraction* fraction) {
if (fraction->numerator) > (fraction->denominator)
{
cout << "test";
}
}
void simplify(struct Fraction* fraction) {
if (fraction.numerator) > (fraction.denominator)
{
cout << "test";
}
}
Struct:
struct Fraction {
int numerator;
int denominator;
};
errors:
w2.cpp: In function void simplify(Fraction*):
w2.cpp:40:15: error: request for member numerator in fraction, which is of non-class type Fraction*
w2.cpp:40:26: error: expected primary-expression before > token
w2.cpp:40:38: error: request for member denominator in fraction, which is of non-class type Fraction*
w2.cpp:41:2: error: expected ; before { token
How could I compare the values inside of a struct?
You have pointers, so you need to de-reference them. You also had bracket problems:
You can avoid all those pointer de-reference woes by passing references instead. Also, drop the needless
structs: