C Shell(cpp.sh,基于浏览器的编译器)正在运行Visual Studio won';T 所以我正在为一个学校项目做C++程序。
C Shell(cpp.sh,基于浏览器的编译器)正在运行Visual Studio won';T 所以我正在为一个学校项目做C++程序。,c++,visual-studio-2010,conditional-operator,C++,Visual Studio 2010,Conditional Operator,我使用了一组条件操作符(我喜欢速记),它在C shell上运行良好(这是链接。程序在那里工作得很好)但是它不会在VisualStudioC++中运行,我很好奇在我的代码中是否存在某种错误……/P> 我的计划是: #include <iostream> using namespace std; int main() { cout << "Please enter an angle and I will tell you which quadrant it is in.\
我使用了一组条件操作符(我喜欢速记),它在C shell上运行良好(这是链接。程序在那里工作得很好)但是它不会在VisualStudioC++中运行,我很好奇在我的代码中是否存在某种错误……/P> 我的计划是:
#include <iostream>
using namespace std;
int main()
{
cout << "Please enter an angle and I will tell you which quadrant it is in.\n\n\n";
double angle;
cin >> angle;
cout << endl;
bool first, second, third, fourth, xaxis, yaxis, nxaxis, nyaxis;
if (angle < 0 || angle > 360)
cout << "Invalid input.";
else
{
(angle > 0 && angle < 90 ? first = true : first = false);
(first == true ? cout << "The angle you entered is in the first quadrant.\n\n" : cout << "");
(angle > 90 && angle < 180 ? second = true : second = false);
(second == true ? cout << "The angle you entered is in the second quadrant.\n\n" : cout << "");
(angle > 180 && angle < 270 ? third = true : third = false);
(third == true ? cout << "The angle you entered is in the third quadrant.\n\n" : cout << "");
(angle > 270 && angle < 360 ? fourth = true : fourth = false);
(fourth == true ? cout << "The angle you entered is in the fourth quadrant.\n\n" : cout << "");
(angle == 0 || angle == 360 ? nxaxis = true : nxaxis = false);
(angle == 180 ? xaxis = true : xaxis = false);
(nxaxis == true ? cout << "This angle lies on the negative portion of the x axis.\n\n" : "");
(xaxis == true ? cout << "This angle lies on the positive portion of the x axis.\n\n" : "");
(angle == 90 ? yaxis = true : yaxis = false);
(angle == 270 ? nyaxis = true : nyaxis = false);
(yaxis == true ? cout << "This angle lies on the positive portion of the y axis.\n\n" : "");
(nyaxis == true ? cout << "This angle lies on the negative portion of the y axis.\n\n" : "");
}
system("pause");
return 0;
}
#包括
使用名称空间std;
int main()
{
cout>角度;
cout(360)
cout 0和角度<90?第一个=真:第一个=假);
(first==true?cout像这样的东西怎么样:
#include <iostream>
using namespace std;
int main() {
cout << "Please enter an angle and I will tell you which quadrant it is in.\n";
double angle;
cin >> angle;
int quadrant = -1;
if (angle >= 0 && angle < 90) {
quadrant = 1;
cout << "The angle you entered is in the first quadrant.\n";
} else if (angle >= 90 && angle < 180) {
quadrant = 2;
cout << "The angle you entered is in the second quadrant.\n";
} ...
} else {
cout << "Illegal value!\n";
...
}
cin.ignore();
return 0;
}
xaxis = angle == 180 ? true : false;
#包括
使用名称空间std;
int main(){
cout>角度;
整数象限=-1;
如果(角度>=0和角度<90){
象限=1;
cout=90和角度<180){
象限=2;
cout您正在滥用三元运算符,使用它的通常和更可读的方式如下:
#include <iostream>
using namespace std;
int main() {
cout << "Please enter an angle and I will tell you which quadrant it is in.\n";
double angle;
cin >> angle;
int quadrant = -1;
if (angle >= 0 && angle < 90) {
quadrant = 1;
cout << "The angle you entered is in the first quadrant.\n";
} else if (angle >= 90 && angle < 180) {
quadrant = 2;
cout << "The angle you entered is in the second quadrant.\n";
} ...
} else {
cout << "Illegal value!\n";
...
}
cin.ignore();
return 0;
}
xaxis = angle == 180 ? true : false;
您使用它就像一个完整的控制结构,而不是if
s
现在,您得到的错误是有意义的。关于三元运算符的一条规则说,?
后面的两个表达式必须具有相同的类型。例如,在这一行:
(nxaxis == true ? cout << "This angle lies on the negative portion of the x axis.\n\n" : "");
或者你可以这样写:
cout << (nxaxis ? "This angle ..." : "");
cout
if (nxaxis)
{
cout << "This angle ...";
}