The ternary ?:
operator requires both output operands to either be the same data type, or at least convertible to a common data type. A char*
cannot be implicitly converted to an int
, but a 0
literal can be implicitly converted to a char*
.
Try this instead:
#include <iostream>
int main()
{
int test = 0;
std::cout << ((test) ? "A String" : "") << std::endl;
return 0;
}
Or:
#include <iostream>
int main()
{
int test = 0;
std::cout << ((test) ? "A String" : (char*)0) << std::endl;
return 0;
}
If you are trying to actually output 0
when test
is 0, then you have to do this instead:
#include <iostream>
int main()
{
int test = 0;
std::cout << ((test) ? "A String" : "0") << std::endl;
return 0;
}
Otherwise, get rid of the ?:
operator since you cannot use it to mix different incompatible data types for output:
#include <iostream>
int main()
{
int test = 0;
if (test)
std::cout << "A String";
else
std::cout << 0;
std::cout << std::endl;
return 0;
}
6
solved Why does this program not output anything? [closed]