View Full Version: Structure program

C++ Learning Community > C++ Help > Structure program


Title: Structure program


homeslice - February 9, 2007 12:30 AM (GMT)
Some wierd errors...

CODE
#include <iostream>

using namespace std;

struct Rational
{
      int numerator;
      int denominator;
      char slash;
};

Rational read();

Rational sum(Rational frac1, Rational frac2);

Rational product(Rational frac1,Rational frac2);

void print(Rational frac);

int main()
{
   Rational frack1, frack2, sfrac, pfrac;
   char again;
   
   do
   {
         frack1 = read();
         frack2 = read();
         sfrac = sum(frack1, frack2);
         pfrac = product(frack1, frack2);
         
         cout << "Your sum is: ";
         print(sfrac);
         cout << endl << "Your product is: ";
         print(pfrac);
         cout << "\n\n\n Would you like to run the program again?";    
         cin >> again;    
         
   }while(again == 'y' || again == 'Y');
   
   return 0;
}

Rational read()
{
        Rational frak1;
       
        cout << "Please enter in the fraction!\n";
        cin >> frak1.numerator >> frak1.slash >> frak1.denominator;
       
        while (frak1.denominator == 0 || frak1.slash != '/')
        {
            if (frak1.denominator == 0)
            {
                cout << frak1.denominator << "Cannot be 0, please "
                     << "enter in an acceptable fraction:\n";
                cin >> frak1.numerator >> frak1.slash >> frak1.denominator;
            }
            else
            {
                cout << frak1.slash << "is not a \"/\", please enter in "
                     << "an acceptable fraction:\n";
                cin >> frak1.numerator >> frak1.slash >> frak1.denominator;
        }    
        return frak1;
}

Rational sum(Rational frac1, Rational frac2)
{
        Rational sumfrak
       
        sumfrak.numerator = (frac1.numerator*frac2.denominator) +
                            (frac2.numerator*frac1.denominator);
       
        sumfrak.slash = '/';
       
        sumfrak.denominator = frac1.denominator * frac2.denominator;
                                     
        sumfrak.slash = '/';
       
        return sumfrak;
}

Rational product(Rational frac1, Rational frac2)
{
        Rational prodmyfrac;
       
        prodmyfrac.numerator = frac1.numerator * frac2.numerator;
       
        prodmyfrac.slash = '/';
       
        prodmyfrac.denominator = frac1.denominator * frac2.denominator;
       
        return prodmyfrac;
}    
       
void print(Rational frac)
{
   cout << frac.numerator << frac.slash << frac.denominator;
}


The compiler seems to be telling me to put semi-colons on the end of the function definitions, which last time I checked, was wrong =\

P.S. No comments yet, I usually put them on after the program works.

Rmstn1580 - February 9, 2007 12:43 AM (GMT)
I don't think you can assign a Rational variable. I think you have to access one of it's things. Like frack1.numerator or something like that. I know you can over-ride that with a class, but I don't know about a struct.

Zaqufant - February 9, 2007 12:59 AM (GMT)
try...
CODE
typedef struct Rational
{
     int numerator;
     int denominator;
     char slash;
};

I think thats the only way I got my structs to work.

EDIT:
I was wrong, It's...
CODE
typedef struct Rational
{
     int numerator;
     int denominator;
     char slash;
}Rational;


That's how I got my structs to work.

Shonoby - February 9, 2007 02:06 AM (GMT)
Umm, well i really don't know what is wrong but i have three word of advise: don't use struc, but if u are learning struc than that is reasonable. But if u playing around with stucs, than i really don't know what is wrong here, cuz i dont use strucs, they are not OOD ( object oriented design ) enough for me to use and they dont have default values so it is easy to make something a wrong value when it should not be in that range of values, not taking into account all other stuff that is not efficient in this.

homeslice - February 9, 2007 02:32 AM (GMT)
Yeah this program is supposed to use structures, but the next program (and the rest of chapter 6 programs) will use classes.

I just forget if our teacher told us to make read void or not =\

adeyblue - February 9, 2007 02:34 AM (GMT)
The only problems with the code are that you're missing a closing brace for the else in read() and a semi-colon after the sumfrac declaration in sum().

@Shonoby: In C++ structs and classes are exactly the same except structs have public access by default and classes have private. They are no less part of OOP, you can give them functions, constructors and everything else classes can have.

Shonoby - February 9, 2007 02:52 AM (GMT)
Homeslice if u don't mind me asking what book are u reading??

homeslice - February 9, 2007 04:31 AM (GMT)
"Problem Solving With C++: The Object of Programming - Fourth Edition" by Walter Savitch.

Shonoby - February 9, 2007 04:35 AM (GMT)
Ohh, well our books do have something in common. They both have fourth edition, but i have a different book, and unlike u i have to teach my self, but thats better for me since ( one of my philosophies ) The best answers we find on our own. Thats me i learn best myself LOL.

homeslice - February 9, 2007 05:30 AM (GMT)
0_o Well we have a teacher but he doesnt do much except tell us what chapters to read and what programs to do (and sometimes helps if you need it, although it's closer to insulting you in front of the class than actually helping =P). 95% of every class is lab time pretty much.

myork - February 9, 2007 01:14 PM (GMT)
QUOTE (homeslice @ Feb 8 2007, 07:30 PM)
CODE
            else
            {
                cout << frak1.slash << "is not a \"/\", please enter in "
                     << "an acceptable fraction:\n";
                cin >> frak1.numerator >> frak1.slash >> frak1.denominator;
        }    


You are missing a simicolon after the else.




Hosted for free by InvisionFree