# Struct HELP!

• 07-22-2003
-Beginners
Struct HELP!
im learning by my own how to code with C++, i got to the topic of Structures. The book has some exercises and i am atempting to complete them..

it asks for a program that would be able to add fractions..
i have somehow an idea of how it would be, but my here's my problem.

I ask for the fraction, i would like to have that fraction compared to a variable thus making it decimal, so i can perform any operation (+, -, x, /) with decimals, and if possible that most common fractions be displayed (1/2, 1/4, 1/8) but not like1/3453.

here's the code of what i been working..
__________________________________________________ __

#include <iostream>
using namespace std;
struct math //structure tag
{

float firstInteger;

};
void main()
{
float equivalent;
math fraction; //define the structure object

cout<<"Enter the first integer: ";
cin>>fraction.firstInteger;

if (fraction.firstInteger= 1/2)
equivalent=.5;
cout<<"___this is the equivalent" <<equivalent<<endl;

cout<<"Your 1st Integer is: \n["<<fraction.firstInteger<<" ]"<<endl;

//End of code
_______________________________________________

any help would be appreciated, THANK YOU!
• 07-22-2003
kuphryn
The logic looks correct. The problem has to do with I/O. One solution is to rend and entire line using std::getline() and a string object. Afterward, parse the input string accordingly.

Kuphryn
• 07-22-2003
Lynux-Penguin
for programming fractions try something like this:
Code:

```struct fraction {         int num;         int den; };```
and work from there.

and for parsing user input... (a very crude method:)
Code:

```char buf[256]; char num[256]; char den[256]; int j=0; cin.getline(buf); while(buf[j]!='/'&&j<strlen(buf)) {         strcat(num,buf[j]);         j++; } j++; while(buf[j]!='\0'&&j<strlen(buf)) {         strcat(den,buf[j]);         j++; }```
and then convert char num[] and den[] to the string.

At least this is a rough idea... I'm sure there are better methods.

-LC
• 07-22-2003
Can you be more specific with the

struct fraction
{
int num;
int den;
};

is there a function that can compare the string with a variable

if fraction == 1/2
then
half =0.5

half = fraction
• 07-22-2003
ygfperson
//edit:
istringstream in("3/4");
in >> num >> den;

This doesn't check for a "/", but that requires more sophisticated code.

//edit2:
To check to see if fractions equal, the easiest way is to
1) convert a fraction to num and den
2) divide the two and store the result in a float or double. (cast num or den to a float or double.)
3) compare the double or float with another one
• 07-22-2003
Placid
I'm working on representing fractions as well, but it seems that doing it with classes is easier, then you can overload the input (std::cin) function, and it works quite smoothly...

But when you want to learn structures:
Code:

```struct fraction {     int num; // numerator     int denom; // denominator };```
Basically, this declears the struct fraction to contain these two integers.

Then make some function to compare/add/multiply/divide two structures, something like this:
Code:

```bool compareFractions( fraction& firstFraction, fraction& secondFraction ) {     if( firstFraction.denom == secondFraction.denom )         if( firstFraction.num == secondFraction.num)             return true;             // They are totally equal!         else             return false;     else         // Multiply up to the common denominator         // Then compare the numerator }```
As you can see, compareFractions takes two fraction references and compares them.

As far as input is concerned, I have no idea of a good solution to that, other than creating classes, as I mentioned.

Hope some o' this helped :)

-- Placid
• 07-23-2003
laasunde
This is wrong,
Code:

`if (fraction.firstInteger= 1/2)`
use == when comparing and = when assigning values to variables.