大家好,我不是这方面的专家,所以请原谅我的倾倒技巧。我完成了我的程序,它工作正常(计算器)。问题是,现在我不知道在哪里找到while循环与布尔函数相结合,一旦完成任务就重复该过程(一旦程序完成数学运算)。任何帮助,评论或建议将不胜感激。谢谢。!!
#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;
}
答案 0 :(得分:0)
所以这里很少。
致电
using namespace std;
只相信 - 这是个坏主意;
在if()或while()之类的条件下,使用operator ==而不是=。因为&#34; =&#34; - 是assigne操作员,返回值取决于操作的成功。并且&#34; ==&#34;是比较运算符。
噢,又想到了一个误区。使用bool rezult = true;是错的。你应该使用rezult = true;因为每次编写类型说明符时都会在case的上下文中创建局部变量,这不会影响在main中声明的rezult
我对你的问题的看法几乎没有变化: 从:
do{
int x;
...
case 0:
cout << "Ending the program" << endl;
return 0;}
break;
bool repeat = true;
}
} while (repeat = true );
到
do{
int x;
...
case 0:
cout << "Ending the program" << endl;
repeat = false;}
break;
}
} while (repeat == true);
如果你需要更多的计算,你可以把它包装成新的cicle,如:
while(new_condtion == true) {
do {
...
} while(repeat == true);
//change new_condtion
}
答案 1 :(得分:0)
请勿在切换案例中重新定义repeat
。这会创建一个名为repeat
的不同变量,虽然它具有相同的名称,但它不是在循环之前定义的名为repeat
的变量。这是您将表单bool repeat = true;
的定义复制到多个位置时获得的结果。
循环的继续条件(repeat = true
)也将永远循环。比较是两个=
符号,而不是一个。