We help IT Professionals succeed at work.
Get Started

C++ Arithmetic operations on fractions - VERY URGENT

alligarbage
alligarbage asked
on
2,539 Views
Last Modified: 2011-10-03
I need to write a program that allows the user to perform arithmetic operations on fractions. There are two fractions that will look like this a/b and c/d. The values of b and d cannot equal 0 and all values must be integers. The program must be menu driven, allowing the user to select the operation (+, -, *, /) and input the numerator and denomintor of each fraction. The program must consist of the functions menu (informs user about the program's purpose, explains how to enter data and allows the user to select the operation), addFractions (take input, do the math, and display the result), subtractFractions (take input, do the math, and display the result), multiplyFractions(take input, do the math, and display the result), and divideFractions(take input, do the math, and display the result).

The program must also handle exceptions such as division by zero and non-integer values.  Allow the user to immediately re-enter any values which have thrown an exception.  Allow the user to continue to use the program until they quit via a menu option.  I can't remember how to allow the user to exit the program and also I'm getting a lot of error messages.

Here is what I have and the error messages I am getting:

//CS218 Week Five Exercise
//Allison Potter

#include <fstream>
#include <iostream>
#include <string>
#include <iomanip>
#include <math.h>

using namespace std;


//declaring variables
      int a;
      int b;
      int c;
      int d;
      char operation;

//calling functions
      void menu(char operationType);
      int addFractions(int a, int b, int c,int d);
      int subtractFractions(int a, int b, int c,int d);
      int multiplyFractions(int a, int b, int c,int d);
      int divideFractions(int a, int b, int c,int d);
      int main();

//function menu
void menu(char operationType)
{
      cout << "Welcome to the Fraction Calculator.";
      cout << "This program allows you to enter in two fractions";
      cout << "and will perform whatever operation you choose.";
      cout << "Please enter in integer values for the variables a, b, c, and d.";
      cout << "The values of b and d cannot equal zero.";
      cout << "Please choose the operation you would like to perform:";
      cout << "+ (addition), - (subtraction), * (multiplication), or / (division)";
      cin >> operationType;      //get the request
      cout << endl;

      if (operationType = "+")
      {
            goto int addFractions(int a, int b, int c,int d);
      }

      else if (operationType = -)
      {
            goto int subtractFractions(int a, int b, int c,int d);
      }

      else if (operationType = *)
      {
            goto int multiplyFractions(int a, int b, int c,int d);
      }

      else if (operationType = /)
      {
            goto int divideFractions(int a, int b, int c,int d);
      }

      else
      {
            cout << "Your input was invalid, please try again";
      }
}

int addFractions(int a, int b, int c,int d)
{
      cout << "Please enter the numerator for the first equation: ";
      cin >> a;

      cout << "Please enter the denominator for the first equation: ";
      cin >> b;

      cout << "Please enter the numerator for the second equation:: ";
      cin >> c;

      cout << "Please enter the denominator for the second equation:: ";
      cin >> d;

      if (b,d = 0)
      {
            cout << "You have entered zero, which is invalid, please try again.";
      }

      if (a,b,c,d != int)
      {
            cout << "You have entered a non-integer value, please try again.";
      }

      cout << a/b + c/d "=" (a*d +b*c)/(b*d);
}

int void subtractFractions(int a, int b, int c,int d)
{
      cout << "Please enter the numerator for the first equation: ";
      cin >> a;

      cout << "Please enter the denominator for the first equation: ";
      cin >> b;

      cout << "Please enter the numerator for the second equation:: ";
      cin >> c;

      cout << "Please enter the denominator for the second equation:: ";
      cin >> d;

      if (b,d = 0)
      {
            cout << "You have entered zero, which is invalid, please try again.";
      }

      if (a,b,c,d != int)
      {
            cout << "You have entered a non-integer value, please try again.";
      }

      cout << a/b + c/d "=" (a*d – b*c)/(b*d);
}

int void multiplyFractions(int a, int b, int c,int d)
{
      cout << "Please enter the numerator for the first equation: ";
      cin >> a;

      cout << "Please enter the denominator for the first equation: ";
      cin >> b;

      cout << "Please enter the numerator for the second equation:: ";
      cin >> c;

      cout << "Please enter the denominator for the second equation:: ";
      cin >> d;

      if (b,d = 0)
      {
            cout << "You have entered zero, which is invalid, please try again.";
      }

      if (a,b,c,d != int)
      {
            cout << "You have entered a non-integer value, please try again.";
      }

      cout << a/b + c/d "=" (a*c)/(b*d);
}

int void divideFractions(int a, int b, int c,int d)
{
      cout << "Please enter the numerator for the first equation: ";
      cin >> a;

      cout << "Please enter the denominator for the first equation: ";
      cin >> b;

      cout << "Please enter the numerator for the second equation:: ";
      cin >> c;

      cout << "Please enter the denominator for the second equation:: ";
      cin >> d;

      if (b,d = 0)
      {
            cout << "You have entered zero, which is invalid, please try again.";
      }

      if (a,b,c,d != int)
      {
            cout << "You have entered a non-integer value, please try again.";
      }

      cout << a/b + c/d "=" (a*d)/(b*c);
}

int main()
{
      void menu(char operationType);
}

Error messages:

------ Build started: Project: Exercise, Configuration: Debug Win32 ------
Compiling...
code.cpp
(44) : error C2440: '=' : cannot convert from 'const char [2]' to 'char'
There is no context in which this conversion is possible
(46) : error C2062: type 'int' unexpected
(49) : error C2059: syntax error : ')'
(50) : error C2143: syntax error : missing ';' before '{'
(51) : error C2062: type 'int' unexpected
(54) : error C2181: illegal else without matching if
(54) : error C2059: syntax error : ')'
(55) : error C2143: syntax error : missing ';' before '{'
(56) : error C2062: type 'int' unexpected
(59) : error C2181: illegal else without matching if
(59) : error C2059: syntax error : '/'
(60) : error C2143: syntax error : missing ';' before '{'
(61) : error C2062: type 'int' unexpected
(64) : error C2181: illegal else without matching if
(89) : error C2062: type 'int' unexpected
(90) : error C2143: syntax error : missing ';' before '{'
(94) : error C2143: syntax error : missing ';' before 'string'
(94) : error C2064: term does not evaluate to a function taking 1 arguments
(97) : error C2632: 'int' followed by 'void' is illegal
(116) : error C2062: type 'int' unexpected
(117) : error C2143: syntax error : missing ';' before '{'
(121) : error C2143: syntax error : missing ';' before 'string'
(121) : error C2146: syntax error : missing ')' before identifier '–'
(121) : error C2064: term does not evaluate to a function taking 1 arguments
(121) : error C2059: syntax error : ')'
(124) : error C2632: 'int' followed by 'void' is illegal
(143) : error C2062: type 'int' unexpected
(144) : error C2143: syntax error : missing ';' before '{'
(148) : error C2143: syntax error : missing ';' before 'string'
(148) : error C2064: term does not evaluate to a function taking 1 arguments
(151) : error C2632: 'int' followed by 'void' is illegal
(170) : error C2062: type 'int' unexpected
(171) : error C2143: syntax error : missing ';' before '{'
(175) : error C2143: syntax error : missing ';' before 'string'
(175) : error C2064: term does not evaluate to a function taking 1 arguments
Week Five Exercise - 35 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Comment
Watch Question
CERTIFIED EXPERT
Top Expert 2006
Commented:
This problem has been solved!
Unlock 1 Answer and 77 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE