Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C++ program question

Posted on 2006-07-18
9
Medium Priority
?
432 Views
Last Modified: 2010-08-05
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
Comment
Question by:hannahdelacy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 17133650
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
 
LVL 7

Assisted Solution

by:nafis_devlpr
nafis_devlpr earned 500 total points
ID: 17136731
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
 
LVL 7

Expert Comment

by:nafis_devlpr
ID: 17136751
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Assisted Solution

by:marchent
marchent earned 500 total points
ID: 17138143
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
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 500 total points
ID: 17138289
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
 
LVL 7

Expert Comment

by:nafis_devlpr
ID: 17210734
>>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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question