a program that will allow the user to input two fractions and one of the four operations of +, - , *, and / . Once this information has been entered, the program should compute the operation on the two fractions and then output the answer appropriate labeled. Reducing the fractions using the gcd algorithm. The program should then ask the user if another number is desired (y or n) and continue requesting pairs of fractions until the user answers no (n). My problem is encorporating the switch statement for the operations and also the gcd algorithm. Here is what I have, I only used addition and sub. because I couldn't figure out how to use the switch.

--------------------------------------------------------------------------------------------------

#include <iostream>
using namespace std;

void EnterOperator(char &opchar)
{
    cout << "Enter an operation to perform { + - / * } ";
    cin >> opchar;
}

void ReadFraction(int &Num, int &Denom, int &Num2, int &Denom2)
/* This function will allow the user to enter two fraction. */
{
        cout << "Enter the numerator and denominator for the first fraction; include a space: ";
        cin >> Num >> Denom;
        cout << endl;
        cout << "Enter the numerator and denominator for the second fraction; include a space: ";
        cin >> Num2 >> Denom2;
        cout << endl;

}

//-------------------------------------------------------------------



void AddFraction(int &Num, int &Denom, int &Num2, int &Denom2)
/* This function is called after Reduce. This function adds the two 
   fractions Reduce() reduced
   Pre: Two Fractions
   Post: One reduced fraction  */
{
        if (Denom != Denom2) 
        {


                Num = Num * Denom2;
                Num2 = Num2 * Denom;
                Denom = Denom * Denom2;
                Denom2 = Denom2 * Denom;
                Num = Num + Num2;
        }
        else
        {
                Num = Num + Num2;
        }


}

void SubFraction(int &Num, int &Denom, int &Num2, int &Denom2)
/* This function is called after Reduce. This function adds the two 
   fractions Reduce() reduced
   Pre: Two Fractions
   Post: One reduced fraction  */
{
        if (Denom != Denom2) 
        {


                Num = Num * Denom2;
                Num2 = Num2 * Denom;
                Denom = Denom * Denom2;
                Denom2 = Denom2 * Denom;
                Num = Num - Num2;
        }
        else
        {
                Num = Num - Num2;
        }

}


void DisplayFraction(int &Num, int &Denom)
/* This function displays the reduced and added fraction. This 
   function is called after AddFraction()
   Post: Prints fraction */
{
        cout << "The fraction value is: " << Num << "/" << Denom << endl;
}


int main()
{
        char an;
        int op;


        do
        {
                int Num, Denom, Num2, Denom2 = 0;
        char opchar;
        EnterOperator(opchar);
                ReadFraction(Num, Denom,Num2,Denom2);
        AddFraction(Num, Denom, Num2, Denom2);
        SubFraction(Num, Denom, Num2, Denom2);
                DisplayFraction(Num, Denom);
                cout << endl;

    switch (opchar) {
    case '+':
        op = ;
        break;
    case '-':
        op = ;
        break;

    default: // If operator is illegal shut program down
        cout << "Invalid operator." << endl;

    int gcd(int Num, int Denom ) {
    assert(Denom != 0);
    int rem = Num % Denom;

    while(rem !=0 ){
    Num = Denom;
    Denom = rem;

    rem = Num % Denom;

    }

    return Denom;

}   
    }

                cout <<"Would you like to do another fraction? ";
                cin >> an;
                cout << endl;
        } while ((an == 'y') || (an == 'Y'));


        return(0);
}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

>because I couldn't figure out how to use the switch
The switch is fine, it's the illegal nested function that's your problem. Functions cannot be defined within other functions.

I omitted the gcd algorithum, if someone could explain to me how i can call the functions to the switch statement I would appreciate it. Thanks

#include <iostream>
using namespace std;

void EnterOperator(char &opchar)
{
    cout << "Enter an operation to perform { + - / * } ";
    cin >> opchar;
}

void ReadFraction(int &Num, int &Denom, int &Num2, int &Denom2)
/* This function will allow the user to enter two fraction. */
{
        cout << "Enter the numerator and denominator for the first fraction; include a space: ";
        cin >> Num >> Denom;
        cout << endl;
        cout << "Enter the numerator and denominator for the second fraction; include a space: ";
        cin >> Num2 >> Denom2;
        cout << endl;

}

//-------------------------------------------------------------------



void AddFraction(int &Num, int &Denom, int &Num2, int &Denom2)

{
        if (Denom != Denom2) 
        {


                Num = Num * Denom2;
                Num2 = Num2 * Denom;
                Denom = Denom * Denom2;
                Denom2 = Denom2 * Denom;
                Num = Num + Num2;
        }
        else
        {
                Num = Num + Num2;
        }


}

void SubFraction(int &Num, int &Denom, int &Num2, int &Denom2)

{
        if (Denom != Denom2) 
        {


                Num = Num * Denom2;
                Num2 = Num2 * Denom;
                Denom = Denom * Denom2;
                Denom2 = Denom2 * Denom;
                Num = Num - Num2;
        }
        else
        {
                Num = Num - Num2;
        }

}

void MultFraction(int &Num, int &Denom, int &Num2, int &Denom2)

{
        if (Denom != Denom2) 
        {

                Num = Num * Num2;
                Denom = Denom * Denom2;
        }
       else
       {
            Num = Num * 0;
       }

}

void DivFraction(int &Num, int &Denom, int &Num2, int &Denom2)
/* This function is called after Reduce. This function adds the two 
   fractions Reduce() reduced
   Pre: Two Fractions
   Post: One reduced fraction  */
{
        if (Denom != Denom2) 
        {


                Num = Num * Denom2;
                Denom = Denom * Num2;

        }
         else
        if (Denom < 0)
        {
               Num *= -1;
           Denom *= -1;
        }

}

void DisplayFraction(int &Num, int &Denom)
/* This function displays the reduced and added fraction. This 
   function is called after AddFraction()
   Post: Prints fraction */
{
        cout << "The fraction value is: " << Num << "/" << Denom << endl;
}


int main()
{
        char an;
        int op;


        do
        {
                int Num, Denom, Num2, Denom2 = 0;
        char (opchar);
        EnterOperator(opchar);
                ReadFraction(Num, Denom,Num2,Denom2);
        AddFraction(Num, Denom, Num2, Denom2);
        SubFraction(Num, Denom, Num2, Denom2);
        MultFraction(Num, Denom, Num2, Denom2);
        DivFraction(Num, Denom, Num2, Denom2);
                DisplayFraction(Num, Denom);
                cout << endl;

    switch (opchar) {
    case '+':
        op = ; //Call Function
        break;
    case '-':
        op = ; //Call Function
        break;
    case '*':
        op = ; //Call Function
        break;
    case '/':
        op = ;
        break; //Call Function

    default: // If operator is illegal shut program down
        cout << "Invalid operator." << endl;
}


                cout <<"Would you like to do another fraction? ";
                cin >> an;
                cout << endl;
        } while ((an == 'y') || (an == 'Y'));


        return(0);
}

Edited 3 Years Ago by Dani: Formatting fixed

as far as i can see, u wanna call the functions to an int (op), but ur functions are not returning anything. try to return the final value to an integer at the end of each function. that mite work.
(eg. for subfraction function u mite wanna return 'num')

a switch statement is a mechanism of flow control that can be translated to something like this (provided you return/break after each case),

int cond;

// case 1:
if(cond == 1 {
// case 2:
} else if (cond == 2) {
// case 3:
} else if (cond == 3) {
// default:
} else {
}

Two important distinctions, however, must be made; both implementation details. The first is that a switch statement only handles int/char cases (conditions) and the other is that you may fall through the case (condition) quite easily (ie: as mentioned early if you don't return/break after a case you fall through to the next case).

So, anyway, for your purpose you might have this function:

bool calc(int & n1, int & d1, int & n2, int & d2, char sign) {
  switch(sign) {
    case '+':
      AddFraction(n1, d1, n2, d2);
      break;
    case '-':
      SubFraction(n1, d1, n2, d2);
      break;
    case '*':
      MultFraction(n1, d1, n2, d2);
      break;
    case '/':
      DivFraction(n1, d1, n2, d2);
      break;
    default:
      return false;
    }
    return true;
}

Or something to that effect

as far as i can see, u wanna call the functions to an int (op), but ur functions are not returning anything. try to return the final value to an integer at the end of each function. that mite work.
(eg. for subfraction function u mite wanna return 'num')

He's passing references to integers (a type of pointer), not integers themselves.

void foo(int & n) {
  n++;
}

int main() {
  int a = 0;
  cout << a << endl;
  foo(a);
  cout << a << endl;
  return 0;
}

Will return,

0
1

int gcd(int a, int b){
 assert(b != 0);
 int rem = a % b;
 while(rem !=0 ){
 a = b;
 b = rem;
 rem = a % b;
 }
 return b;
}
This article has been dead for over six months. Start a new discussion instead.