473,503 Members | 1,735 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Functions In Fraction Calculator

91 New Member
hello everyone,i'm writing this csalculator proogram that is using functions to perform functions in a fraction calculator.this is what i did and my problem now the readInFraction does not take the values to the function execute to perform the calculations that i want.my problem is that the values i'm reading in readInFraction are not transferred to execute.

//program that that allows the user to perform arithmetic operations on fractions
#include<iostream>
using namespace std;


void execute(char&,int&,int&,int&,int&,int&,int&);
void readInFraction(int,int,int,int);
void menuAndGetOP(char);//function prototype that receives and takes nothing
void division (int,int,int,int,int,int);//function prototype for dividing
void addition (int,int,int,int,int,int);//function prototype for adding
void subtraction (int,int,int,int,int,int);//function prototype for subtraction
void multiplication (int,int,int,int,int,int);//function prototype for multipling


int main()
{
int N1;//declaration of N1
int N2;//declaration of N2
int D1;//declaration of D1
int D2;//declaration of D2
char OP;//declaration of OP
int Dr=0;
int Nr=0;


menuAndGetOP(OP);
readInFraction(N1,D1,N2,D2);
execute(OP,N1,D1,N2,D2,Dr,Nr);
return 0;
}
void menuAndGetOP(char OP)//function defination
{

//prints massage
cout<<" "<<"********************************************** ************\n";
cout<<" "<<"* To perform arithmetic operations on fractions: *\n";
cout<<" "<<"* Multiplication: press '*' *\n";
cout<<" "<<"* Subtraction: press '-' *\n";
cout<<" "<<"* Addition: press '+' *\n";
cout<<" "<<"* Division: press `/` *\n";
cout<<" "<<"* Enter operation (+ , -, *, / ):";
cin>>OP;
cout<<" "<<"********************************************** ************\n";



}//end of function menuAndGetOP '

void readInFraction(int n1,int d1,int n2,int d2)

{
char jack;

cout<<"enter the first fraction\n";
cin>>n1>>jack>>d1;
cout<<"enter second fraction\n";
cin>>n2>>jack>>d2;

}

void addition(int n1,int d1,int n2,int d2,int dr,int nr)//function prototype for adding
{

nr=(n1*d2)+(n2*d1);//performs adding calculations
dr=d1*d2;//performs adding calculations

cout<<nr<<"/"<<dr;
}
void division(int n1,int d1,int n2,int d2,int dr,int nr)

{

nr=n1*d2;//performs divide calculations
dr=n2*d1;//performs divide calculations

cout<<nr<<"/"<<dr;

}
void multiplication(int n1,int d1,int n2,int d2,int dr,int nr)
{

nr=n1*n2;//performs multiplication calculations
dr=d1*d2;//performs multiplication calculations

cout<<nr<<"/"<<dr;

}
void subtraction(int n1,int d1,int n2,int d2,int dr,int nr)

{
nr=(n1*d2)-(n2*d1);//performs subtraction calculations
dr=d1*d2;//performs subtraction calculations

cout<<nr<<"/"<<dr;
}
void execute(char&OP,int& n1,int& d1,int& n2,int& d2,int& dr,int&nr)


{



switch (OP)//switch statement that allows user to enter a choice
{
case '-' :subtraction(n1,d1,n2,d2,dr,nr);//if user entered '-' operator
break;

case '*':multiplication(n1,d1,n2,d2,dr,nr);
break;

case '+':addition(n1,d1,n2,d2,dr,nr);
break;

case '/' :division(n1,d1,n2,d2,dr,nr);
break;

default:
cout<<"wrong operation\n";
break;
}
}
Aug 26 '07 #1
1 5364
ilikepython
844 Recognized Expert Contributor
hello everyone,i'm writing this csalculator proogram that is using functions to perform functions in a fraction calculator.this is what i did and my problem now the readInFraction does not take the values to the function execute to perform the calculations that i want.my problem is that the values i'm reading in readInFraction are not transferred to execute.

//program that that allows the user to perform arithmetic operations on fractions
#include<iostream>
using namespace std;


void execute(char&,int&,int&,int&,int&,int&,int&);
void readInFraction(int,int,int,int);
void menuAndGetOP(char);//function prototype that receives and takes nothing
void division (int,int,int,int,int,int);//function prototype for dividing
void addition (int,int,int,int,int,int);//function prototype for adding
void subtraction (int,int,int,int,int,int);//function prototype for subtraction
void multiplication (int,int,int,int,int,int);//function prototype for multipling


int main()
{
int N1;//declaration of N1
int N2;//declaration of N2
int D1;//declaration of D1
int D2;//declaration of D2
char OP;//declaration of OP
int Dr=0;
int Nr=0;


menuAndGetOP(OP);
readInFraction(N1,D1,N2,D2);
execute(OP,N1,D1,N2,D2,Dr,Nr);
return 0;
}
void menuAndGetOP(char OP)//function defination
{

//prints massage
cout<<" "<<"********************************************** ************\n";
cout<<" "<<"* To perform arithmetic operations on fractions: *\n";
cout<<" "<<"* Multiplication: press '*' *\n";
cout<<" "<<"* Subtraction: press '-' *\n";
cout<<" "<<"* Addition: press '+' *\n";
cout<<" "<<"* Division: press `/` *\n";
cout<<" "<<"* Enter operation (+ , -, *, / ):";
cin>>OP;
cout<<" "<<"********************************************** ************\n";



}//end of function menuAndGetOP '

void readInFraction(int n1,int d1,int n2,int d2)

{
char jack;

cout<<"enter the first fraction\n";
cin>>n1>>jack>>d1;
cout<<"enter second fraction\n";
cin>>n2>>jack>>d2;

}

void addition(int n1,int d1,int n2,int d2,int dr,int nr)//function prototype for adding
{

nr=(n1*d2)+(n2*d1);//performs adding calculations
dr=d1*d2;//performs adding calculations

cout<<nr<<"/"<<dr;
}
void division(int n1,int d1,int n2,int d2,int dr,int nr)

{

nr=n1*d2;//performs divide calculations
dr=n2*d1;//performs divide calculations

cout<<nr<<"/"<<dr;

}
void multiplication(int n1,int d1,int n2,int d2,int dr,int nr)
{

nr=n1*n2;//performs multiplication calculations
dr=d1*d2;//performs multiplication calculations

cout<<nr<<"/"<<dr;

}
void subtraction(int n1,int d1,int n2,int d2,int dr,int nr)

{
nr=(n1*d2)-(n2*d1);//performs subtraction calculations
dr=d1*d2;//performs subtraction calculations

cout<<nr<<"/"<<dr;
}
void execute(char&OP,int& n1,int& d1,int& n2,int& d2,int& dr,int&nr)


{



switch (OP)//switch statement that allows user to enter a choice
{
case '-' :subtraction(n1,d1,n2,d2,dr,nr);//if user entered '-' operator
break;

case '*':multiplication(n1,d1,n2,d2,dr,nr);
break;

case '+':addition(n1,d1,n2,d2,dr,nr);
break;

case '/' :division(n1,d1,n2,d2,dr,nr);
break;

default:
cout<<"wrong operation\n";
break;
}
}
First of all, you have all these integer values, why not just use a class.

Your functions are useless because they don't bring back anything to do the program. I don't know why you declared execute with pass by reference. Only the functions that return more than one value should use pass by reference. And they should only do it on the variables that you want to return:
Expand|Select|Wrap|Line Numbers
  1. void add(int N1, int D1, int N2, int D2, int &dr, int &nr);
  2.  
Aug 27 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
4206
by: Luis | last post by:
can you look at my code, and tell my wy the section of the client program which says : const Fraction f3(12, 8); const Fraction f4(202, 303); result = f3.MultipliedBy(f4); cout << "The product...
13
9796
by: Steve | last post by:
I am having trouble finding the answer to this question, I'm thinking the solution must be blindingly obvious, so therefore of course I cannot see it. I wish to take a fraction in DEC say .4 and...
0
2271
by: amarok | last post by:
Hello all. I'm a Software Engineering student, and I'm attempting to write a program in Java that does as follows: UML for the class: Fraction() Fraction(numerator: int) ...
6
13380
evilmonkey
by: evilmonkey | last post by:
I am very new to programming as well as Java and this is my first post so please forgive me if this is not quite posted correctly. My Problem is that I have only been using scanner to get user input...
1
2188
by: d0ugg | last post by:
Hi, I'm did a fraction program for one of my programming classes and it did compile, however when I'm running the program it crashes for some reason that I do not know. // fraction.cpp ...
4
14430
by: d0ugg | last post by:
Hello everyone, I'm creating a program that it is suppose to add, subtract, multiply and also divide fractions and after that, the result has to be reduced to the lowest terms. However, I'm not...
3
3172
by: Stolas | last post by:
Hello all, I'm attempting to create a Fraction Calculator. But for some reasons on my RationalTest program it is giving me an error that getAdd in Rational cannot be applied to (Rational). I'm very...
39
2096
by: vlsidesign | last post by:
Operators seem similar to functions. They both do something to either arguments or operands, but are different in their syntax. Operators seem to be like built-in C functions. It would seem that...
135
4154
by: robinsiebler | last post by:
I've never had any call to use floating point numbers and now that I want to, I can't! *** Python 2.5.1 (r251:54863, May 1 2007, 17:47:05) on win32. *** 0.29999999999999999 0.29999999999999999
0
7199
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7076
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7274
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7323
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6984
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7453
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4670
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3151
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1507
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.