Solved

C++ program question

Posted on 2006-07-18
9
405 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 13

Assisted Solution

by:marchent
marchent earned 125 total points
Comment Utility
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
Comment Utility
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
Comment Utility
>>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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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.

763 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

12 Experts available now in Live!

Get 1:1 Help Now