Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Anonymous
Anonymous asked in Computers & InternetProgramming & Design · 2 weeks ago

Can't figure out why this program won't run... (C++ hw help)?

#include<iostream>

using namespace std;

void menu(char&, int&, int&, int&, int&);

void addFraction(int, int, int, int, int&, int&);

void subtractFractions(char, int, int, int, int, int&, int&);

void multiplyFractions(char, int, int, int, int, int&, int&);

void divideFractions(char, int, int, int, int, int&, int&);

int gethcf(int, int);

void simplify(int&, int&);

bool wishContinue(int&);

int main() //Main function

{

 int n1 = 0;

 int n2 = 0;

 int n3 = 0;

 int d1 = 0;

 int d2 = 0;

 int d3 = 0;

 int result = 0;

 char operation;

 char slash = '/';

 menu(operation, n1, n2, d1, d2);

 while (operation != 0)

 {

  if (operation == '1') // Add fractions

   addFraction(n1, n2, d1, d2, n3, d3);

  else if (operation == '2') // Subtract fractions

   subtractFractions(operation, n1, n2, d1, d2, n3, d3);

  else if (operation == '3') // Multiply fractions

   multiplyFractions(operation, n1, n2, d1, d2, n3, d3);

  else if (operation == '4') // Divide fractions

   divideFractions(operation, n1, n2, d1, d2, n3, d3);

  cout << n3 << slash << d3 << endl;

 }

  menu(operation, n1, n2, d1, d2);

  return;

 }

3 Answers

Relevance
  • Anonymous
    2 weeks ago

    #include <iostream>

    using namespace std;

    // first you need a number

    struct number {

     int numerator;

     int denominator;

     number(): 

      numerator(0), 

      denominator(0)

     {

     }

     void simplify();

     void in();

     void out();

    };

    void number::simplify() //Euclids algorithm

    {

     int a, b;

     a = numerator;

     b = denominator;

     while (a != b)

     {

      if (a > b)

       a -= b;

      else

       b -= a;

     }

     numerator /= a;

     denominator /= a;

    }

    void number::in(){

      cout << "Enter the numerator: " << endl;

     cin >> numerator;

     cout << "Enter the denominator: " << endl;

     cin >> denominator;

    }

    void number::out(){

     cout << numerator << '/' << denominator;

    }

    enum Operation { ADD=1, SUBTRACT, MULTIPLY, DIVIDE, EXIT=9};

    Operation menu()

    {

    char c = 0;

    Operation operation;

    do {

      cout << "This program performs on integer fractions. Enter: \n"

    "1: To add fraction,\n"

     "2: To subtract fraction,\n"

    "3: To multiply fraction,\n"

    "4: To divide fraction, and\n"

     "9: To exit the program" << endl;

      cin >> c;

      operation = (Operation)(c-'0');

     }

      while (operation != MULTIPLY && operation != DIVIDE && operation != SUBTRACT && operation != ADD 

     && operation != EXIT);

     return operation;

    }

    void addFraction(number & n1, number & n2, number& n3)

    {

    n1.out();

     cout << '+';

     n2.out();

    cout << endl;

     n3.numerator = (n1.denominator * n2.numerator) + (n2.denominator * n1.numerator);

     n3.denominator = (n1.denominator * n2.denominator);

     n3.simplify();

     n3.out();

     cout << endl;

    }

    void subtractFractions(number & n1, number& n2, number& n3)

    {

    // n3.numerator = (d2 * n1) - (d1 * n2);

    // n3.denominator = (d1 * d2);

    // n3.simplify();

    }

    void multiplyFractions(number & n1, number& n2, number& n3)

    {

    // n3 = (n2 * n1);

    // d3 = (d1 * d2);

    // simplify(n3, d3);

    }

    void divideFractions(number&n1, number& n2, number& n3)

    {

    // n3 = (d2 * n1);

    // d3 = (d1 * n2);

    // simplify(n3, d3);

    }

    void exitProgram(){

    // char q; cout << "\nPress any key to Exit.";

    // cin.ignore(2, '\n');

    // cin.get(q);

    }

    int main() {

     number n1, n2, n3;

    Operation operation = EXIT;

    do {

     Operation operation = menu();

     cout << "For fraction 1" << endl;

     n1.in();

     cout << "For fraction 2" << endl;

     n2.in();

      if (operation == ADD) // Add fractions

       addFraction(n1, n2, n3);

      else if (operation == SUBTRACT) // Subtract fractions

       subtractFractions(n1, n2, n3);

      else if (operation == MULTIPLY) // Multiply fractions

       multiplyFractions(n1, n2, n3);

      else if (operation == DIVIDE) // Divide fractions

       divideFractions(n1, n2, n3);

     } while (operation != EXIT);

     return 0;

    }

  • EddieJ
    Lv 7
    2 weeks ago

    menu(operation, n1, n2, d1, d2);

      return;

    Your main program is declared as returning an <int> but your <return> statement does not supply a return value.

    Just above that, your call to menu () needs to be inside the <while> loop, otherwise, <operation> never changes and you will have an infinite loop (unless the user enters zero the first time.

  • Anonymous
    2 weeks ago

    (cont...)

    void simplify(int& numerator, int& denominator) //Euclids algorithm

    {

     int a, b;

     a = numerator;

     b = denominator;

     while (a != b)

     {

      if (a > b)

       a -= b;

      else

       b -= a;

     }

     numerator /= a;

     denominator /= a;

    }

    void menu(int& operation, int& n1, int& n2, int& d1, int& d2)

    {

     while (operation != '1' && operation != '2' && operation != '3' && operation != '4' && operation != '9')

     {

      cout << "This program performs on integer fractions. Enter: " << endl;

      cout << "1: To add fraction," << endl;

      cout << "2: To subtract fraction," << endl;

      cout << "3: To multiply fraction," << endl;

      cout << "4: To divide fraction, and" << endl;

      cout << "9: To exit the program" << endl;

      cin >> operation;

     }

     cout << "For fraction 1" << endl;

     cout << "Enter the numerator: " << endl;

     cin >> n1;

     cout << "Enter the denominator: " << endl;

     cin >> d1;

     cout << "For fraction 2" << endl;

     cout << "Enter the numerator: " << endl;

     cin >> n2;

     cout << "Enter the denominator: " << endl;

     cin >> d2;

     return;

    }

    void addFraction(char operation, int n1, int n2, int d1, int d2, int& n3, int& d3)

    {

     cout << n1 << " " << d1 << " " << n2 << " " << d2 << endl;

     n3 = (d1 * n2) + (d2 * n1);

     d3 = (d1 * d2);

     simplify(n3, d3);

     cout << " " << n3 << "/" << d3 << endl;

     return;

    }

    void subtractFractions(char operation, int n1, int n2, int d1, int d2, int& n3, int& d3)

    {

     n3 = (d2 * n1) - (d1 * n2);

     d3 = (d1 * d2);

     simplify(n3, d3);

     return;

    }

    void multiplyFractions(char operation, int n1, int n2, int d1, int d2, int& n3, int& d3)

    {

     n3 = (n2 * n1);

     d3 = (d1 * d2);

     simplify(n3, d3);

     return;

    }

    void divideFractions(char operation, int n1, int n2, int d1, int d2, int& n3, int& d3)

    {

     n3 = (d2 * n1);

     d3 = (d1 * n2);

     simplify(n3, d3);

     return;

    }

    void exitProgram(char operation)

    {

     char q; cout << "\nPress any key to Exit.";

     cin.ignore(2, '\n');

     cin.get(q);

     return;

    }

Still have questions? Get your answers by asking now.