c++ - C Shell(cpp.sh,基于浏览器的编译器)正在运行 Visual Studio 不会运行的程序

标签 c++ visual-studio-2010 conditional-operator

所以我正在为一个学校项目开发一个 C++ 程序。

我使用了一堆条件运算符(我喜欢简写),它在 C Shell 上运行得很好(这是链接。程序在那里运行得很好... http://cpp.sh/7fsj ),但它无法在 Visual Studio 上运行C++,我很好奇我的代码中是否存在某种错误......

我的程序是:

#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;
}

它在那里运行,但不在 VS 中运行

我找不到任何逻辑错误或错误原因,但它一直这样说

"1>------ Build started: Project: lab-projects, Configuration: Debug Win32 ------
1>  project.cpp
1>c:\users\wishi\documents\visual studio 2010\projects\lab-projects\lab-projects\project.cpp(179): error C2446: ':' : no conversion from 'const char *' to 'std::basic_ostream<_Elem,_Traits>'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          No constructor could take the source type, or constructor overload resolution was ambiguous
1>c:\users\wishi\documents\visual studio 2010\projects\lab-projects\lab-projects\project.cpp(180): error C2446: ':' : no conversion from 'const char *' to 'std::basic_ostream<_Elem,_Traits>'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          No constructor could take the source type, or constructor overload resolution was ambiguous
1>c:\users\wishi\documents\visual studio 2010\projects\lab-projects\lab-projects\project.cpp(183): error C2446: ':' : no conversion from 'const char *' to 'std::basic_ostream<_Elem,_Traits>'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          No constructor could take the source type, or constructor overload resolution was ambiguous
1>c:\users\wishi\documents\visual studio 2010\projects\lab-projects\lab-projects\project.cpp(184): error C2446: ':' : no conversion from 'const char *' to 'std::basic_ostream<_Elem,_Traits>'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          No constructor could take the source type, or constructor overload resolution was ambiguous
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped =========="

这是一个初级编程类(class),所以我不知道这意味着什么。 有人可以帮忙吗?

最佳答案

您滥用了三元运算符,通常且更易读的使用方式如下:

xaxis = angle == 180 ? true : false;

您将其用作完整的控制结构,而不是 if s。

现在您遇到的错误是有道理的。关于三元运算符的规则之一规定 ? 之后的两个表达式必须具有相同的类型。以这一行为例:

(nxaxis == true ? cout << "This angle lies on the negative portion of the x axis.\n\n" : "");

第一个表达式 ( cout << "..." ) 是 ostream第二个( "" )是 const char * 。这就是错误消息试图告诉您的内容,它正在尝试将第二个表达式转换为 ostream以匹配第一个的类型。

要解决这个问题,您可以像前面几行中那样编写表达式,即使它很难看:

(nxaxis == true ? cout << "This angle lies on the negative portion of the x axis.\n\n" : cout << "");

或者你可以这样写:

cout << (nxaxis ? "This angle ..." : "");

或者更好:

if (nxaxis)
{
    cout << "This angle ...";
}

关于c++ - C Shell(cpp.sh,基于浏览器的编译器)正在运行 Visual Studio 不会运行的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39783701/

相关文章:

c++ - 了解 std::isnan 的编译结果

c++ - Fstream _Fgetc 访问冲突

c++ - 获取十六进制字符串的字节长度的替代方法

c# - 如何强制 VS 2010 跳过未更改的项目的 "builds"?

python - 三元 if-else 语句中的语法错误

python - Python中条件表达式的求值顺序是什么?

c++ - 我试过 : valgrind, _GLIBCXX_DEBUG,-fno-strict-aliasing;我该如何调试这个错误?

c++ - 在 MFC 中使用 WinINet 的问题

c++ - 应用程序后台时线程被杀死?

java - 为什么三元运算符会意外地转换整数?