• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 439
  • Last Modified:

C++ program question

I have written my program, but after one loop it terminates, how do I make it a continuous looping program?


#include <iostream>

using namespace std;

enum functMenu(char& function);
void addFractions(int num1, int den1, int num2, int den2, int& resultNum, int& resultDen);
void subtFractions(int num1, int den1, int num2, int den2, int& resultNum, int& resultDen);
void multiFractions(int num1, int den1, int num2, int den2, int& resultNum, int& resultDen);
void divFractions(int num1, int den1, int num2, int den2, int& resultNum, int& resultDen);

int main()
{
      int num1, den1, num2, den2, resultNum, resultDen;
      char function;
      
      functMenu (function);
      
      if (function == 'x')
      {
            return 0;
      
      }
      else if (function != 'a' && function != 's' && function != 'm' && function != 'd' && function != 'x')
      {
            cout << "Invalid Choice" << endl;
            functMenu (function);
      }
      cout << "For fraction 1" << endl;
      cout << "Enter the numerator: ";
      cin  >> num1;
      cout << endl;
      
      cout << "Enter the denominator: ";
      cin  >> den1;
      cout << endl;
      cout << endl;
      
      while (den1 == 0)
      {
            cout << "The denominator must be a nonzero" << endl;
            cout << "Enter the denominator: " << endl;
            cin  >> den1;
      }
      
      cout << "For fraction 2" << endl;
      cout << "Enter the numerator: ";
      cin  >> num2;
      cout << endl;
      
      cout << "Enter the denominator: ";
      cin  >> den2;
      cout << endl;
      cout << endl;
      
      while (den2 == 0)
      {
            cout << "The denominator must be a nonzero" << endl;
            cout << "Enter the denominator: " << endl;
            cin  >> den2;
      }
      
      if (function == 'a')
      {      
            function = '+';
            addFractions (num1, den1, num2, den2, resultNum, resultDen);
      }
      
      else if (function == 's')
      {
            function = '-';
            subtFractions (num1, den1, num2, den2, resultNum, resultDen);
      }
      
      else if (function == 'm')
      {
            function = '*';
            multiFractions (num1, den1, num2, den2, resultNum, resultDen);
      }
      
      else if (function == 'd')
      {
            function = '/';
            divFractions (num1, den1, num2, den2, resultNum, resultDen);
      }
      
      cout << num1 << "/" << den1 << " " << function << " " << num2 << "/" << den2 << " " << "=" << " " << resultNum << "/" << resultDen << endl;
      functMenu (function);
}

functMenu (char& function)
{
      cout << "This program performs functions of a fraction.  Enter" << endl;
      cout << "a : To add fraction" << endl;
      cout << "s : To subtract fration" << endl;
      cout << "m : To multiply fraction" << endl;
      cout << "d : To divide fraction" << endl;
      cout << "x : To exit the program" << endl;
      cin  >> function;
      return function;
}

void addFractions (int num1, int den1, int num2, int den2, int& resultNum, int& resultDen)
{
      if (den1 == den2)
      {
            resultNum = num1 + num2;
            resultDen = den1;
      }
      else
      {
            resultDen = den1 * den2;
            resultNum = (num1 * den2) + (num2 * den1);
      }
}

void subtFractions (int num1, int den1, int num2, int den2, int& resultNum, int& resultDen)
{
      if (den1 == den2)
      {
            resultNum = num1 - num2;
            resultDen = den1;
      }
      else
      {
            resultDen = den1 * den2;
            resultNum = (num1 * den2) - (num2 * den1);
      }
}

void multiFractions (int num1, int den1, int num2, int den2, int& resultNum, int& resultDen)
{
      resultNum = num1 * num2;
      resultDen = den1 * den2;
}

void divractions (int num1, int den1, int num2, int den2, int& resultNum, int& resultDen)
{
      resultNum = num1 * den2;
      resultDen = den1 * num2;
}

0
hannahdelacy
Asked:
hannahdelacy
4 Solutions
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
simply put a

while (true)
{
  <main sub body>
}

like this:

<...>

int main()
{
     int num1, den1, num2, den2, resultNum, resultDen;
     char function;
     
  while (true)
  {
     functMenu (function);
     
     if (function == 'x')
     {
          return 0;
     
     }
     else if (function != 'a' && function != 's' && function != 'm' && function != 'd' && function != 'x')
     {
          cout << "Invalid Choice" << endl;
          functMenu (function);
     }
     cout << "For fraction 1" << endl;
     cout << "Enter the numerator: ";
     cin  >> num1;
     cout << endl;
     
     cout << "Enter the denominator: ";
     cin  >> den1;
     cout << endl;
     cout << endl;
     
     while (den1 == 0)
     {
          cout << "The denominator must be a nonzero" << endl;
          cout << "Enter the denominator: " << endl;
          cin  >> den1;
     }
     
     cout << "For fraction 2" << endl;
     cout << "Enter the numerator: ";
     cin  >> num2;
     cout << endl;
     
     cout << "Enter the denominator: ";
     cin  >> den2;
     cout << endl;
     cout << endl;
     
     while (den2 == 0)
     {
          cout << "The denominator must be a nonzero" << endl;
          cout << "Enter the denominator: " << endl;
          cin  >> den2;
     }
     
     if (function == 'a')
     {    
          function = '+';
          addFractions (num1, den1, num2, den2, resultNum, resultDen);
     }
     
     else if (function == 's')
     {
          function = '-';
          subtFractions (num1, den1, num2, den2, resultNum, resultDen);
     }
     
     else if (function == 'm')
     {
          function = '*';
          multiFractions (num1, den1, num2, den2, resultNum, resultDen);
     }
     
     else if (function == 'd')
     {
          function = '/';
          divFractions (num1, den1, num2, den2, resultNum, resultDen);
     }
     
     cout << num1 << "/" << den1 << " " << function << " " << num2 << "/" << den2 << " " << "=" << " " << resultNum << "/" << resultDen << endl;
  }

}

<...>
0
 
nafis_devlprCommented:
you can do it this way, in this way the program will not exit until you enter 'x', which according to your code should be done:

#include <iostream>

using namespace std;

enum functMenu(char& function);
void addFractions(int num1, int den1, int num2, int den2, int& resultNum, int& resultDen);
void subtFractions(int num1, int den1, int num2, int den2, int& resultNum, int& resultDen);
void multiFractions(int num1, int den1, int num2, int den2, int& resultNum, int& resultDen);
void divFractions(int num1, int den1, int num2, int den2, int& resultNum, int& resultDen);

int main()
{
     int num1, den1, num2, den2, resultNum, resultDen;
     char function;
     
     functMenu (function);
     while(function != 'x')
     {
     else if (function != 'a' && function != 's' && function != 'm' && function != 'd')
     {
          cout << "Invalid Choice" << endl;
          functMenu (function);
         continue;
     }
     cout << "For fraction 1" << endl;
     cout << "Enter the numerator: ";
     cin  >> num1;
     cout << endl;
     
     cout << "Enter the denominator: ";
     cin  >> den1;
     cout << endl;
     cout << endl;
     
     while (den1 == 0)
     {
          cout << "The denominator must be a nonzero" << endl;
          cout << "Enter the denominator: " << endl;
          cin  >> den1;
     }
     
     cout << "For fraction 2" << endl;
     cout << "Enter the numerator: ";
     cin  >> num2;
     cout << endl;
     
     cout << "Enter the denominator: ";
     cin  >> den2;
     cout << endl;
     cout << endl;
     
     while (den2 == 0)
     {
          cout << "The denominator must be a nonzero" << endl;
          cout << "Enter the denominator: " << endl;
          cin  >> den2;
     }
     
     if (function == 'a')
     {    
          function = '+';
          addFractions (num1, den1, num2, den2, resultNum, resultDen);
     }
     
     else if (function == 's')
     {
          function = '-';
          subtFractions (num1, den1, num2, den2, resultNum, resultDen);
     }
     
     else if (function == 'm')
     {
          function = '*';
          multiFractions (num1, den1, num2, den2, resultNum, resultDen);
     }
     
     else if (function == 'd')
     {
          function = '/';
          divFractions (num1, den1, num2, den2, resultNum, resultDen);
     }
     
     cout << num1 << "/" << den1 << " " << function << " " << num2 << "/" << den2 << " " << "=" << " " << resultNum << "/" << resultDen << endl;
     functMenu (function);
}
}

functMenu (char& function)
{
     cout << "This program performs functions of a fraction.  Enter" << endl;
     cout << "a : To add fraction" << endl;
     cout << "s : To subtract fration" << endl;
     cout << "m : To multiply fraction" << endl;
     cout << "d : To divide fraction" << endl;
     cout << "x : To exit the program" << endl;
     cin  >> function;
     return function;
}

void addFractions (int num1, int den1, int num2, int den2, int& resultNum, int& resultDen)
{
     if (den1 == den2)
     {
          resultNum = num1 + num2;
          resultDen = den1;
     }
     else
     {
          resultDen = den1 * den2;
          resultNum = (num1 * den2) + (num2 * den1);
     }
}

void subtFractions (int num1, int den1, int num2, int den2, int& resultNum, int& resultDen)
{
     if (den1 == den2)
     {
          resultNum = num1 - num2;
          resultDen = den1;
     }
     else
     {
          resultDen = den1 * den2;
          resultNum = (num1 * den2) - (num2 * den1);
     }
}

void multiFractions (int num1, int den1, int num2, int den2, int& resultNum, int& resultDen)
{
     resultNum = num1 * num2;
     resultDen = den1 * den2;
}

void divractions (int num1, int den1, int num2, int den2, int& resultNum, int& resultDen)
{
     resultNum = num1 * den2;
     resultDen = den1 * num2;
}


hope this helps

Nafis
0
 
nafis_devlprCommented:
take a deep look at the following code inside the loop

if (function != 'a' && function != 's' && function != 'm' && function != 'd')
     {
          cout << "Invalid Choice" << endl;
          functMenu (function);
         continue;
     }

this code enables you to restart the loop after an invalid Choice input, you dont have to check for 'x' as it has been done on the while(function != 'x') condition.

Nafis
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
marchentCommented:
since here is already two solutions, i don wanna put my kode here, but i just wanna remind u one thing hannahdelacy. better add a feature inside your kode for saftey. check untill non zero number for the numerator. cause your doing division operation with the fractions. so ur denominator may comes UNDEFINED for input = 0; you shouldn't put any single bug inside inside your kode.
---------------------->have a nice time
---------------------->>>>>marchent
0
 
itsmeandnobodyelseCommented:
Nafis, your code works .... after removing some typos and adding some prototypes.

Hannah, did you succeed compiling Nafis' code?

You should add a function that reduces a fraction given or calculated. You might call it after each input and at end of any calculation.

Regards, Alex

void reduceFraction (int& num, int& den)
{
   int n = abs(num);
   int d = abs(den);

   if (n == 1 || n == 0 || d == 1 || d == 0 )
       return;

   // determine minimum of n and d
   int m = ( n >= d ) ? d : n;

   // step from 2 to 3 and then all odd numbers
   for (int i = 2, s = 1; i <= m; i += s, s = 2)
   {
        while ( m >= i && n % i == 0 && d % i == 0 )
        {
            n /= i;
            d /= i;
            m /= i;
        }
   }

   num = (num < 0)= - n : n;
   den = (den < 0)= - d : d;
}



 
0
 
nafis_devlprCommented:
>>Nafis, your code works .... after removing some typos and adding some prototypes.

what are the errors and typos?? i didn't get a chance to compile the code just coded them in the submit box and copied most of the original codes.

>>You should add a function that reduces a fraction given or calculated. You might call it after each input and at end of any calculation.

I think what you want to say is to divide both of them with their GCD, yeah thats a nice & clever thing to do, but the code you showed don't you think a bit slow?? you can use the code below

int GCD(int a, int b)
      {
            int rem;
            if(b > a)
                  {
                        rem=a;
                        a=b;
                        b=rem;
                  }

            rem=a%b;
            while(rem)
                  {
                        a=b;
                        b=rem;
                        rem=a%b;
                  }
            return b;
      }

void reduceFraction (int& num, int& den)
{
   int n = abs(num);
   int d = abs(den);

   if (n == 1 || n == 0 || d == 1 || d == 0 )
       return;
   int m=GCD(n,d);
   n/=m;
   d/=m;

   num = (num < 0)= - n : n;
   den = (den < 0)= - d : d;
}


Nafis
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now