简体   繁体   中英

While loop and boolean function

Hello guys I'm not an expert on the subject so please excuse my pour skills. I finished my program and it works fine (calculator). The problem is that now I don't know where to locate the while loop in conjunct with the Boolean function to repeat the process once it is done with a task (once the program completes a math operation). Any help, comment or suggestion will be greatly appreciated. Thank you.!!

 #include <iostream>  
 #include <math.h> 
 #include <cmath>


 int main()
 {

 double a=0.0;
 double b=0.0;
 double c=0.0;
 bool repeat = true;


do {
using namespace std;
int x;

cout << "**********************************" << endl;
cout << "|                                |" << endl;
cout << "| 0 - Quit                       |" << endl;
cout << "| 1 - Add                        |" << endl;
cout << "| 2 - Subtract                   |" << endl;
cout << "| 3 - Divide                     |" << endl;
cout << "| 4 - Multiply                   |" << endl;
cout << "| 5 - Raise X to the power Y     |" << endl;
cout << "| 6 - Sine ( x )                 |" << endl;
cout << "| 7 - Cosine ( x )               |" << endl;
cout << "| 8 - Tangent ( x )              |" << endl;
cout << "**********************************" << endl;
cout << "Enter a selection, please: " << endl; 
cin >> x;




switch (x)
{


{
case 1:
cout << " Enter the first value" <<endl;
cin >> a ;
cout << " Enter second value " << endl;
cin >> b; 
c=a+b;
cout << "The addition of " << a << " and "<< b << "is" << c << endl;

break;
bool repeat = true;
 }



 {
case 2:
cout << " Enter the first value" << endl;
cin >> a ;
cout << " Enter the second value " << endl;
cin >> b; 
c=a-b;
cout << "The subtraction of " << a << " and " << b << " is: " << c << endl;

break;
bool repeat = true;
}


{
case 3:
cout << " Enter the first value" <<endl;
cin >> a ;
cout << " Enter the second value " << endl;
cin >> b; 
c=a/b;
cout << " The division os " << a << " and " << b << "is" << c << endl;

break;
bool repeat = true;
 }


 {
case 4:
cout << " Enter the first value" <<endl;
cin >> a ;
cout << " Enter the second value " << endl;
cin >> b; 
c=a*b;
 cout << " The product of " << a << " times " << b << " is " << c << endl;
 break;
 bool repeat = true;
 }

  {
  case 5:
 cout << " Enter the value to be exponentiated " <<endl;
 cin >> a ;
 cout << " Enter the exponent" << endl;
 cin >> b; 
 c= pow(a,b);
 cout << a << " Rased to the power of " << b << " is: " << c << endl;

 break;
 bool repeat = true;
  }


  { 
  case 6:
  cout << " Enter the value that you want the sine to be taken of" <<endl;
  cin >> a ;
  c=sin(a);
  cout << " The sine of " << a << " is: " << c << endl ;

  break;
  bool repeat = true;
   }

   {
  case 7:
  cout << " Enter the value that you want the cosine to be taken of" <<endl;
  cin >> a ;
  c=cos(a);
  cout << " The cosine of " << a << " is: " << c << endl ;
   break;
  bool repeat = true;
   }


   {
  case 8:
   cout << " Enter the value that you want the tangent to be taken of"                           <<endl;
  cin >> a ; 
  c=tan(a);
  cout << " The tangent of " << a << " is: " << c << endl ;

  break;
  bool repeat = true;
   } 


   {
   case 0:
   cout << "Ending the program" << endl;
   return 0;}

   break;
   bool repeat = true;
     }
    } while (repeat = true );
     return 0;  
     }

So here is few moments.

Call

using namespace std;

just believe - is bad idea;

In conditions like if() or while() use operator == instead of =. Because "=" - is assigne operator, and return value depended on success of operation. And "==" is compare operator.

Ow and figure one more missunderstanding. Using bool rezult = true; is wrong. You should use rezult = true; Because every time when you write type specifer you create local variable in context of case, and this don`t affect rezult declared in main

My opinion for your question is little change: from:

do{ 
int x;
...
case 0:
   cout << "Ending the program" << endl;
   return 0;}

   break;
   bool repeat = true;
     }
    } while (repeat = true );

to

do{ 
 int x;
 ...

 case 0:
  cout << "Ending the program" << endl;
  repeat = false;}
  break;
  }
} while (repeat == true);

and if you need a bit more calculations you can wrapped it to new cicle something like:

while(new_condtion == true) {
  do {
  ...
  } while(repeat == true);
  //change new_condtion
}

Don't redefine repeat within switch case. This creates a different variable named repeat which, although it has the same name, is not the variable named repeat defined before the loop. This is what you get when you copy a definition of the form bool repeat = true; into multiple places.

The continuation condition for the loop ( repeat = true ) will also loop forever. Comparison is two = signs, not one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM