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

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




Arithmetic operations on fractions

 
Reply to this topicStart new topic

Arithmetic operations on fractions, Menu driven fraction calculator

alligarbage
3 Dec, 2006 - 11:13 PM
Post #1

New D.I.C Head
*

Joined: 3 Dec, 2006
Posts: 6


My Contributions
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).

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


CODE

#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);

//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);
}

Error messages:

QUOTE

------ 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 ==========


This post has been edited by Dark_Nexus: 4 Dec, 2006 - 12:06 AM
User is offlineProfile CardPM
+Quote Post

Xing
RE: Arithmetic Operations On Fractions
3 Dec, 2006 - 11:17 PM
Post #2

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 2 times
Dream Kudos: 1575
My Contributions
Could you also post your main function with code tags.
User is offlineProfile CardPM
+Quote Post

alligarbage
RE: Arithmetic Operations On Fractions
3 Dec, 2006 - 11:26 PM
Post #3

New D.I.C Head
*

Joined: 3 Dec, 2006
Posts: 6


My Contributions

Could you also post your main function with code tags.

I actually didn't write a main function. Do I have to write a main function? I guess I do. I'm sorry, my brain is just not working right now. What are code tags?
User is offlineProfile CardPM
+Quote Post

alligarbage
RE: Arithmetic Operations On Fractions
3 Dec, 2006 - 11:33 PM
Post #4

New D.I.C Head
*

Joined: 3 Dec, 2006
Posts: 6


My Contributions
QUOTE(alligarbage @ 4 Dec, 2006 - 12:26 AM) *

Could you also post your main function with code tags.

I actually didn't write a main function. Do I have to write a main function? I guess I do. I'm sorry, my brain is just not working right now. What are code tags?


Ok, I just wrote a main function:

CODE

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

User is offlineProfile CardPM
+Quote Post

alligarbage
RE: Arithmetic Operations On Fractions
4 Dec, 2006 - 12:15 AM
Post #5

New D.I.C Head
*

Joined: 3 Dec, 2006
Posts: 6


My Contributions
Here is my updated code and error messages:

I am pretty much getting the same error messages for each function I'm using. I need to validate the int data type.

Can someone please help me out?

CODE

#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);
    void exit();
    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), / (division), or x (exit)";
    cin >> operationType;    //get the request
    cout << endl;

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

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

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

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

    else if (operationType = 'x')
    {
        exit();
    }

    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 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 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 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
(92) : error C2062: type 'int' unexpected
(93) : error C2143: syntax error : missing ';' before '{'
(97) : error C2143: syntax error : missing ';' before 'string'
(97) : error C2064: term does not evaluate to a function taking 1 arguments
(119) : error C2062: type 'int' unexpected
(120) : error C2143: syntax error : missing ';' before '{'
(124) : error C2143: syntax error : missing ';' before 'string'
(124) : error C2146: syntax error : missing ')' before identifier '–'
(124) : error C2064: term does not evaluate to a function taking 1 arguments
(124) : error C2059: syntax error : ')'
(146) : error C2062: type 'int' unexpected
(147) : error C2143: syntax error : missing ';' before '{'
(151) : error C2143: syntax error : missing ';' before 'string'
(151) : error C2064: term does not evaluate to a function taking 1 arguments
(173) : error C2062: type 'int' unexpected
(174) : error C2143: syntax error : missing ';' before '{'
(178) : error C2143: syntax error : missing ';' before 'string'
(178) : error C2064: term does not evaluate to a function taking 1 arguments
Build log was saved at "Exercise\Debug\BuildLog.htm"
Exercise - 18 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 07:22PM

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