# Rational arithmetic calculator

My program is a fraction calculator that is supposed to calculate the basic operations (+, -, *, /) and is supposed to exit when I enter % as my sentinel value. This is what I have so far; any feedback is highly appreciated.

#include <iostream>
#include <cstdlib>
using namespace std;

NONE      =0,
SUBTRACT  =2,
MULTIPLY  =3,
DIVIDE    =4,
QUIT      =5,
END       =6,

};

{

do  {

cout<<"2) Subtract"<<endl;
cout<<"3) Multiply"<<endl;
cout<<"4) Divide"<<endl;
cout<<"5) %"<<endl;

//
cout<<"Please pick a number from 1-5 as shown in the menu selection."<<endl;

{cout<<"You have chosen to exit the program"<<endl;

exit(1);

}

}

//

}

void addFractions(int num1, int num2, int den1, int den2, int &NUM_result, int    &DEN_result)
{        //
NUM_result= (num1* den2) +(num2*den1);
//
DEN_result= den1*den2;

}

void subFractions(int num1, int num2, int den1, int den2, int &NUM_result, int &DEN_result)
{   NUM_result = (num1*den2) - (num2*den1);
//

DEN_result= den1*den2;

//

}

void multiplyFractions(int num1, int num2, int den1, int den2, int &NUM_result, int &DEN_result)
{   NUM_result = num1*num2;
//

DEN_result= den1* den2;

}

void divideFractions(int num1, int num2, int den1, int den2, int &NUM_result, int &DEN_result)
{            //

NUM_result = num1*den2;
//
DEN_result = den1*num2;

}

void input_values_FROM_USER(int &num1, int &num2, int &den1, int &den2)

{

cout<<endl;
cout<<"Enter the numerator for first fraction-->";
cin>>num1;
cout<<"Enter denominator for first fraction--> ";
cin>>den1;
cout<<"Enter numerator for second fraction--> ";
cin>>num2;
cout<<"Enter denominator for second fraction--> ";
cin>>den2;
cout<<"-------------------------------------------\n\n\n";

}

void outputResults(int num1, int num2, int den1, int den2, int &NUM_result, int &DEN_result, int operation)
{
{ cout<<"The sum of the two is--> "<<num1<<'/'<<den1<<'+'<<num2<<'/'<<den2<<'='<<NUM_result<<'/'<<DEN_result<<endl;
}

if(operation== SUBTRACT)
{ cout<<"The difference of the two is--> "<<num1<<'/'<<den1<<'-  '<<num2<<'/'<<den2<<'='<<NUM_result<<'/'<<DEN_result<<endl;

}

if(operation== MULTIPLY)
{
cout<<"The product of the two is--> "     <<num1<<'/'<<den1<<'*'<<num2<<'/'<<den2<<'='<<NUM_result<<'/'<<DEN_result<<endl;
}

if(operation== DIVIDE)
{
cout<<"The quotient of the two is--> "  <<num1<<'/'<<den1<<'/'<<num2<<'/'<<den2<<'='<<NUM_result<<'/'<<DEN_result<<endl;
}
}

void main()
int num1=0, num2=0, den1=0, den2=0;
int NUM_result=0, DEN_result=0;

cout<<"Author: Jose Soto."<<endl;
cout<<"CSCI 110-- Jose Soto's Project 2 (Basic Fraction Arithmetic)\n\n"<<endl;
do

input_values_FROM_USER(num1,num2,den1,den2);

}

{ subFractions(num1,num2,den1,den2,NUM_result, DEN_result);

}

{ multiplyFractions(num1,num2,den1,den2,NUM_result,DEN_result);

}

{ divideFractions(num1,num2,den1,den2,NUM_result,DEN_result);

}

}

cin.get();

}

• Welcome! Please consider using proper, consistent indentation and avoiding excess whitespace. It's all over the place here and is hard to read. Jan 27, 2014 at 6:43
• Your original question didn't match the code you presented. We can't really review code that you intend to write, only code that you have already written, so I've edited the question accordingly. Jan 27, 2014 at 6:58
• I am supposed to have the number of correct out of the total answered. For example when I enter %, it is supposed to display something like "You answered 8 out of 10 questions correctly, you have 80.5% which is a letter grade of B." Then it's supposed to exit the program. I'm not too sure what to write in terms of that. Jan 27, 2014 at 7:02
• In Code Review, our minimum standard is that we review code that already works. (See help center.) Would you like a critique of the working features of your code, or would you prefer to withdraw the question until you have completed the code? Jan 27, 2014 at 7:11
• A critique would suffice. Jan 27, 2014 at 8:04

Your code doesn't accept a '%' to quit. Changing the menu to accept a '%' makes the rest of it inconsistent. so making all the input match the operation and changing the enum to reflect the character values instead of arbitrary ones helps with this.

You're parsing the menu input in 2 places. It's easier to maintain if you use the menu routine just to read the input and keep any parsing of the input in the calling routine.

A switch block improves the readability when you have to parse the user's input.

When parsing user input through a menu putting the input into a char instead of an int makes it harder for user input to break your code.

When you're sending literal strings to the console it's usually more efficient to to add a '\n' to the end of it rather than sending endl separately.

Here's some revised code which cleans up a number of issues:

#include <iostream>
#include <cstdlib>

using namespace std;

{
SUBTRACT  = 45,
MULTIPLY  = 42,
DIVIDE    = 47,
QUIT      = 37,
};
void input_values_FROM_USER(int &num1, int &num2, int &den1, int &den2)

{
cout<<endl;
cout<<"Enter the numerator for first fraction-->";
cin>>num1;
cout<<"Enter denominator for first fraction--> ";
cin>>den1;
cout<<"Enter numerator for second fraction--> ";
cin>>num2;
cout<<"Enter denominator for second fraction--> ";
cin>>den2;
cout<<"-------------------------------------------\n\n\n";
}
{

cout<<"-) Subtract\n";
cout<<"*) Multiply\n";
cout<<"/) Divide\n";
cout<<"%) Quit\n";
}
void addFractions(int num1, int num2, int den1, int den2, int &NUM_result, int    &DEN_result)
{
NUM_result= (num1* den2) +(num2*den1);
DEN_result= den1*den2;
}

void subFractions(int num1, int num2, int den1, int den2, int &NUM_result, int &DEN_result)
{
NUM_result = (num1*den2) - (num2*den1);
DEN_result= den1*den2;
}

void multiplyFractions(int num1, int num2, int den1, int den2, int &NUM_result, int &DEN_result)
{
NUM_result = num1*num2;
DEN_result= den1* den2;
}
void divideFractions(int num1, int num2, int den1, int den2, int &NUM_result, int &DEN_result)
{
NUM_result = num1*den2;
DEN_result = den1*num2;
}
void outputResults(int num1, int num2, int den1, int den2, int &NUM_result, int &DEN_result, int operation)
{
cout<<"The result of the operation is--> "<<num1<<'/'<<den1<<(char)operation<<num2<<'/'<<den2<<'='<<NUM_result<<'/'<<DEN_result<<endl;
}
void main()
{
int num1=0, num2=0, den1=0, den2=0;
int NUM_result=0, DEN_result=0;
cout<<"Author: Jose Soto."<<endl;
cout<<"CSCI 110-- Jose Soto's Project 2 (Basic Fraction Arithmetic)\n\n\n";
do
{
{
input_values_FROM_USER(num1,num2,den1,den2);
break;
case SUBTRACT:
input_values_FROM_USER(num1,num2,den1,den2);
subFractions(num1,num2,den1,den2,NUM_result, DEN_result);
break;
case MULTIPLY:
input_values_FROM_USER(num1,num2,den1,den2);
multiplyFractions(num1,num2,den1,den2,NUM_result,DEN_result);
break;
case DIVIDE:
input_values_FROM_USER(num1,num2,den1,den2);
divideFractions(num1,num2,den1,den2,NUM_result,DEN_result);
break;
case QUIT:
cout<<"\nYou have chosen to exit the program\n";
default:
break;
}
}
cin.get();
}


Since you have a C++ tag on this question, I assume this is eventually meant as a C++ program. If that is the case, you should have

class Fraction {
int denominator;
int numerator;
Fraction(int d, int n) : denominator(d), numerator(n) {}
// define operators for +, -, etc.
}


If you are still writing just in C—combine parsing the user input with dispatching the appropriate message to an object.

Some things not mentioned:

1. Please do not use using namespace std. Read this
2. void main() is not a part of the C++ standard.
3. Consider creating a class for representing fractions. The arithmetic functions should be implemented in operator overloads. Combining advice from the previous answers, your code would become easier to read and maintain.

Note: In general, operators that return a new value should be returned by value.

    class Fraction
{

public:
Fraction(int a,int b) : numerator(a), denominator(b){}
Fraction operator+(const Fraction&, const Fraction&); //Return a Fraction by value
Fraction operator-(const Fraction&, const Fraction&);
Fraction operator*(const Fraction&, const Fraction&);
Fraction operator/(const Fraction&, const Fraction&);

private:

int denominator;
int numerator;

};