Welcome to Dream.In.Code
Become a C++ Expert!

Join 150,025 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,540 people online right now. Registration is fast and FREE... Join Now!




Implementing Fractions

 
Reply to this topicStart new topic

Implementing Fractions

JMcoder
29 Nov, 2006 - 08:17 PM
Post #1

New D.I.C Head
*

Joined: 29 Nov, 2006
Posts: 3


My Contributions
Hey, I have got this progam to write and its just giving me a million errors. Need to compile in a day. Any suggestions. I am almost sure I am not doing the right thing.

QUESTION:
Define an object called Fraction which has two methods
CODE

a) float Addfraction (float,float)
B) float Subtractfraction (float,float)
c) float Multiplyfraction (float,float)

Put in a menu (remember use switch case)

Menu
1. Add fractions
2. Subtract fractions
3. Multiply fractions
4. Exit



ATTEMPT
CODE

#include<iostream.h>
#include<stdlib.h>
#include<conio.h>

class Fraction {

private:

    int num;

    int den;


public:

   Fraction();

   void menu();

   void add();

   void subtract();

   void multiply();

   float addfraction(float,float);

   float subtractfraction(float,float);

   float multiplyfraction(float,float);

   float num1,num2,num3;

   void main ()
  
   while(choice != -1)

   switch (choice)

   {

   menu();
  
   }

   void menu()
  
   {

    float choice;
    cout<<"1.add";cout<<endl;
    cout<<"2.subtract";cout<<endl;
    cout<<"3.multiply";cout<<endl;
    cout<<"4.exit";cout<<endl;
    cout<<"ENTER A CHOICE";
    cin>>choice;
    }

    {    

    case 1:
    add();
    break;

    case 2:
       subtract();
    break;

      case 3:
       multiply();
       break;

       case 4:
       exit();
       break;

  };

Fraction::Fraction(){

num=0;

den=1;

}

void Fraction::addfraction(){

float num1;

cout<<"Enter the first number";

cin>>num1;

addfraction=fraction+num1;

cout<<"Result after addfraction"<<addfraction;

cout<<endl;

}

void Fraction::subtractfraction(){

float num2;

cout<<"Enter a second number";

cin>>num2;

subtractfraction=num1-num2;

cout<<"Result after subtractfraction"<<subtractfraction;

cout<<endl;

}

void Fraction::multiplyfraction(){

float num3;

cout<<"Enter a third number";

cin>>num3;

multiplyfraction=num2*num3;

cout<<"Result after multiplyfraction"<<multiplyfraction;

cout<<endl;

}

void exit(){
}

void main(){

Fraction F;

F.addfraction();

F.subtractfraction();

F.multiplyfraction();

getch();}

User is offlineProfile CardPM
+Quote Post

sidharth
RE: Implementing Fractions
29 Nov, 2006 - 08:36 PM
Post #2

New D.I.C Head
Group Icon

Joined: 10 Nov, 2006
Posts: 38


Dream Kudos: 550
My Contributions
QUOTE(JMcoder @ 29 Nov, 2006 - 09:17 PM) *

Hey, I have got this progam to write and its just giving me a million errors. Need to compile in a day. Any suggestions. I am almost sure I am not doing the right thing.

QUESTION:
Define an object called Fraction which has two methods
CODE

a) float Addfraction (float,float)
B) float Subtractfraction (float,float)
c) float Multiplyfraction (float,float)

Put in a menu (remember use switch case)

Menu
1. Add fractions
2. Subtract fractions
3. Multiply fractions
4. Exit



ATTEMPT
CODE

#include<iostream.h>
#include<stdlib.h>
#include<conio.h>

class Fraction {

private:

    int num;

    int den;


public:

   Fraction();

   void menu();

   void add();

   void subtract();

   void multiply();

   float addfraction(float,float);

   float subtractfraction(float,float);

   float multiplyfraction(float,float);

   float num1,num2,num3;

   void main ()
  
   while(choice != -1)

   switch (choice)

   {

   menu();
  
   }

   void menu()
  
   {

    float choice;
    cout<<"1.add";cout<<endl;
    cout<<"2.subtract";cout<<endl;
    cout<<"3.multiply";cout<<endl;
    cout<<"4.exit";cout<<endl;
    cout<<"ENTER A CHOICE";
    cin>>choice;
    }

    {    

    case 1:
    add();
    break;

    case 2:
       subtract();
    break;

      case 3:
       multiply();
       break;

       case 4:
       exit();
       break;

  };

Fraction::Fraction(){

num=0;

den=1;

}

void Fraction::addfraction(){

float num1;

cout<<"Enter the first number";

cin>>num1;

addfraction=fraction+num1;

cout<<"Result after addfraction"<<addfraction;

cout<<endl;

}

void Fraction::subtractfraction(){

float num2;

cout<<"Enter a second number";

cin>>num2;

subtractfraction=num1-num2;

cout<<"Result after subtractfraction"<<subtractfraction;

cout<<endl;

}

void Fraction::multiplyfraction(){

float num3;

cout<<"Enter a third number";

cin>>num3;

multiplyfraction=num2*num3;

cout<<"Result after multiplyfraction"<<multiplyfraction;

cout<<endl;

}

void exit(){
}

void main(){

Fraction F;

F.addfraction();

F.subtractfraction();

F.multiplyfraction();

getch();}


what have u made huh2.gif
playing with it
do u know how to add fractions??
i shall try to correct it

User is offlineProfile CardPM
+Quote Post

okyup
RE: Implementing Fractions
29 Nov, 2006 - 09:17 PM
Post #3

D.I.C Head
Group Icon

Joined: 6 Nov, 2006
Posts: 207


Dream Kudos: 175
My Contributions
QUOTE

what have u made huh2.gif


A monster. blink.gif

I suggest you start over. Try to use wayyy less functions and variables, and make it more clear. Make sure you close your class and start main. You dont appear to do that...

CODE

class {
....
........
};

int main()
{
....
}


Put your menu in a loop inside the main function. You need only 1 function in the class per operation. Try to keep it organized not putting random pieces of code all over. (thats what it looks like at least) sleepy.gif
User is offlineProfile CardPM
+Quote Post

JMcoder
RE: Implementing Fractions
29 Nov, 2006 - 09:39 PM
Post #4

New D.I.C Head
*

Joined: 29 Nov, 2006
Posts: 3


My Contributions
biggrin.gif Thanks . I have been working on it .. sorry if it confused you. It was confusing me ... new to this stuff so practise practise smile.gif



User is offlineProfile CardPM
+Quote Post

JMcoder
RE: Implementing Fractions
30 Nov, 2006 - 11:30 AM
Post #5

New D.I.C Head
*

Joined: 29 Nov, 2006
Posts: 3


My Contributions
Found a solution .. what do you think? blink.gif

CODE

#include <iostream.h>
#include <stdlib.h>
#include <conio.h>

class Fraction {

private:

float x;
float y;

public:
Fraction ();
Fraction (float, float);
float addFraction();
float subFraction();
float mulFraction();
void setValues(float, float);

};

//constructor
Fraction::Fraction () {
x = 0;
y = 0;
}
//constructor
Fraction::Fraction (float a, float B) {
x = a;
y = b;
}
//add fraction
float Fraction::addFraction(){
return x+y;
}
//subtract fraction
float Fraction::subFraction(){
return x-y;
}
//multiply fraction
float Fraction::mulFraction(){
return x*y;
}
//changes the variables in the function
void Fraction::setValues(float var1, float var2){
x = var1;
y = var2;
}




//function to get the menu choice;
int menu (){
int choice;
cout << "1.add";cout<<endl;
cout << "2.subtract";cout<<endl;
cout << "3.multiply";cout<<endl;
cout<< "4.exit";cout<<endl;
cout<< "enter a choice";
cin>>choice;
return choice;

}

void main(){
//create fraction object
Fraction frac;
int selection;
float first, second;
float answer;
//call menu function and store result in selection
selection = menu();
switch(selection) {
case 1:
   cout<<"enter the first value" << endl;
   cin >> first;
   cout<<"enter the second value"<<endl;
   cin>>second;
   frac.setValues(first,second);
   answer = frac.addFraction();
    break;
case 2:
cout<<"enter the first value" << endl;
   cin >> first;
   cout<<"enter the second value"<<endl;
   cin>>second;
   frac.setValues(first,second);
   answer = frac.subFraction();
    break;
case 3:
cout<<"enter the first value" << endl;
   cin >> first;
   cout<<"enter the second value"<<endl;
   cin>>second;
   frac.setValues(first,second);
   answer = frac.mulFraction();
    break;
case 4:
   return;

}

cout << "the answer is" << answer<<endl;


}


edit: added [code] tags ~ jayman9
User is offlineProfile CardPM
+Quote Post

cipherence
RE: Implementing Fractions
30 Nov, 2006 - 12:48 PM
Post #6

D.I.C Regular
Group Icon

Joined: 1 Apr, 2006
Posts: 260



Thanked: 1 times
Dream Kudos: 650
My Contributions
Until you put it in there, JMcoder. i couldn' see your switch statement.
User is offlineProfile CardPM
+Quote Post

msg555
RE: Implementing Fractions
30 Nov, 2006 - 03:59 PM
Post #7

D.I.C Head
Group Icon

Joined: 4 May, 2006
Posts: 136



Thanked: 5 times
Dream Kudos: 25
My Contributions
How about simplifiying the fraction using Euclid's algorithm?
User is offlineProfile CardPM
+Quote Post

wreckingcru
RE: Implementing Fractions
30 Nov, 2006 - 08:51 PM
Post #8

New D.I.C Head
*

Joined: 20 Oct, 2006
Posts: 4


My Contributions
You may want to initialize your denominator to 1 and not 0 so as to not crash the program in case this portion is overlooked.

Also, probably add a check to overwrite denominator with 1 or spit out an error message in case the user tries to shove a 0 in the denominator.

If this is for college, I guarantee that your TA/Prof will try this test to check your program for completeness.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 09:26PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month