I don't get how to use structs properly to achieve my goal of calculating Fractions (it is required). Quite frankly I don't have much of an idea of what I'm doing, this is only my 3rd class in C++ and I feel lost...this was the task assigned to us
Your enter() function accepts a fraction from the user. Your simplify() function simplifies the fraction that it receives, if possible. Your display() function displays the fraction that it receives.
Your global functions use a Fraction type. A Fraction type holds the numerator and denominator of a fraction as separate data members.
This is my program, only the main EXCEPT the "cin" and "cout" and the GCF function was provided by the professor, all other functions and struct outside of the main i tried to do myself...
#include <iostream>
using namespace std;
void entry (int a, int b);
void simplify (double c);
void display(int x, int y)
int main()
{
struct Fraction fraction;
cout << "Enter a numerator: " << endl;
cin >> fraction.num;
cout << "Enter a denominator: " << endl;
cin >> fraction.den;
cout << "Fraction Simplifier" << endl;
cout << "===================" << endl;
enter(&fraction);
simplify(&fraction);
display(fraction);
}
struct Fraction {
int num;
int den;
}
struct Fraction fraction{
fraction.num;
fraction.den;
}
void display(int num, int den) {
cout << fraction.num << endl;
cout << fraction.den << endl;
}
// Great Common Factor (Euclid's Algorithm), provided by Professor
int gcf( int num1, int num2 )
{
int remainder = num2 % num1;
if ( remainder != 0 )
{
return gcf( remainder,num1 );
}
return num1;
}
these are my errors:
w2.cpp: In function 'int main()':
w2.cpp: 14: error: aggregate 'Fraction fraction' has incomplete type and cannot be defined
w2.cpp: 23: error: 'enter' was not declared in this scope
w2.cpp: At global scope: w2.cpp:35: error: function definition does not declare parameters
w2.cpp: In function 'void display(int, int)':
w2.cpp: 41: error: 'fraction' was not declared in this scope
I'm sorry for the really long post, but any and all help is greatly appreciated. AND if someone could point me to a helpful C++ book that I could read while at home and or in lectures (because of a language barrier i cannot understand my prof well enough) would also be appreciated
Let's walk through these:
error: aggregate 'Fraction fraction' has incomplete type and cannot be defined
Now, in main()
, you said struct Fraction fraction;
. At this point, you're forward-declaring your struct. It is not complete, so you can't use it as if it were.
You should have your whole Fraction
struct defined before main()
. Also note that the struct
in struct Fraction fraction;
is unnecessary, and left over from C.
error: 'enter' was not declared in this scope
Simple. You've declared entry()
up top, but you're trying to use enter()
. Not much more to be said.
At global scope: w2.cpp:35: error: function definition does not declare parameters
Now this is a bit more confusing. This is the offending line:
struct Fraction fraction{
How the compiler sees this is that it is a function returning a Fraction
, but it's missing its parameter list. I'm not exactly sure what you're trying to do with this block of code.
error: 'fraction' was not declared in this scope
Looks like you're trying to use an object declared somewhere else. If you want the one from main()
, you'll have to pass it in as an argument. If you want a global variable fraction
, all you need in the global space is:
Fraction fraction;
This should occur after the Fraction
struct. Also note that because this has the same object name as the one in main()
, the one in main()
shadows this one, and if you want to access the global one from main()
you need to use ::fraction
.
I hope that helps clear up some of the understanding.
Some other errors I see are:
enter(&fraction);
You're passing a Fraction *
to a function that takes two int
s. I think you'd want this one to take a Fraction &
. Then you can just call it like enter (fraction);
to have it modify the object passed in.
simplify(&fraction);
Similar, but this one takes a double
. I think you'd want it to take a Fraction &
as well.
entry
and simplify
functions never get defined, but you still try to use them.display
should take a Fraction
in order to print the parts of it.