Solved

C++ program question

Posted on 2006-07-18
9
408 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
9 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 125 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 125 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 13

Assisted Solution

by:marchent
marchent earned 125 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 125 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

775 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