Solved

C++ program question

Posted on 2006-07-18
9
407 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

919 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now