#include <iostream>
#include <cmath>
using namespace std;
int main(){
int a , b , c , D ;
double x1 , x2 ;
cout << " a = " ;
cin >> a;
cout << " b = " ;
cin >> b;
cout << " c = " ;
cin >>c;
D = pow(b,2) - 4 * a * c;
x1 = (-b + D ) / (2*a);
x2 = (- b - D) / (2*a);
cout << "D = " << D << endl;
D >= 0 ? ( x1,x2) : (cout << "nope . \n" , x1 = x2 = 0);
cout << x1 << endl;
cout << x2 << endl;
(D % 2) == 1 ? (D++) : (cout << "Number is even . \n" ); //check if number is uneven and if it is then add 1
cout << D << endl;
return 0;
}
它抛出错误 :operands to ?: have different types 'int' and 'std::basic_ostream' 。 在注释所在的行。 是否可以使用条件运算符 (?) 来修复它?
最佳答案
按照评论中的建议,将两个操作数都转换为 void
:
(D % 2) == 1 ? void(D++) : void(cout << "Number is even . \n" );
或者更好的是,使用常规的 if
:
if (D % 2 == 1)
D++;
else
cout << "Number is even . \n";
您需要为 的其他用途做同样的事情吗? :
也是。
关于c++ - 错误 : operands to ?:具有不同的类型 'int' 和 'std::basic_ostream<char>',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55228024/