first I'd like to apologize for my bad English and thank you in advance for reading and helping.
Basically I have two structs that hold 3 ints: numbers fractionAnswers
I then have a class called addSubFractions that takes two numbers struct type as parameters and returns a fractionAnswers struct type. My goal is to assign the return from the addSubFractions class to a numbers type struct variable called answers .
I am fairly new to C++ and I thought that I would be able to do this logically, I googled as much as I could without any good info.
Heres a part of the code that is not working for me:
int main() {
numbers first, second, answer ;
cout << "Enter fraction" ;
cin >> first.wholeNumber ;
cin >> first.numerator ;
cin >> first.denominator ;
cout << "Enter second fraction" ;
cin >> second.wholeNumber ;
cin >> second.numerator ;
cin >> second.denominator ;
answer = addSubFractions(first, second) ;
cout << "Your answer is:" << answer.numerator << "/" << answer.denominator ;
return 0 ;}
Here is the other class I was talking about
fractionAnswers addSubFractions(numbers firstFraction, numbers secondFraction) {
numbers answerFraction, holdFraction1, holdFraction2 ;
fractionAnswers someAnswers ;
//Multiply to get same denominator
holdFraction1.denominator = firstFraction.denominator * secondFraction.denominator ;
holdFraction2.denominator = firstFraction.denominator * secondFraction.denominator ;
//Numerator multiplication to match new denominator
holdFraction1.numerator = firstFraction.numerator * secondFraction.denominator ;
holdFraction2.numerator = secondFraction.numerator * firstFraction.denominator ;
//Reduce fractions first
while (true) {
if(!reduceFractions(holdFraction1))
break;
}
//Reduce fractions second
while (true) {
if(!reduceFractions(holdFraction2))
break;
}
someAnswers.numeratorAns = holdFraction1.numerator + holdFraction2.numerator ;
someAnswers.denominatorAns = holdFraction2.denominator ;
return someAnswers ;
}
this is reduceFractions
bool reduceFractions(numbers Fractions) {
bool answer ;
for (int i = Fractions.numerator ; i >= 2 ; i--) { //This will keep running until i = the GFC for both numerator & denominator
if ((((Fractions.numerator % i) == 0) && (Fractions.denominator % i) == 0)) {
Fractions.numerator /= i ;
Fractions.denominator /= i ;
answer = true ;
}
}
answer = false ;
return answer ;
}
These are the structs
struct numbers {
int numerator ;
int denominator ;
int wholeNumber ;
};
struct fractionAnswers {
int numeratorAns ;
int denominatorAns ;
int wholeNumberAns ;
};
This code shouldn't compile, is that correct? You can't assign a value of type fractionAnswers
to a variable of type numbers
, even though they have the same layout of members. Some options are:
Change the return type of addSubFractions
to numbers
.
Change answer = addSubFractions(first, second) ;
to fractionAnswers temp = addSubFractions(first, second); answer.numerator = temp.numeratorAns; answer.denominator = temp.denominatorAns; answer.wholeNumber = temp.wholeNumberAns;
fractionAnswers temp = addSubFractions(first, second); answer.numerator = temp.numeratorAns; answer.denominator = temp.denominatorAns; answer.wholeNumber = temp.wholeNumberAns;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.