C Shell(cpp.sh,基于浏览器的编译器)正在运行Visual Studio不会的程序
C Shell (cpp.sh, browser based compiler) is running a program Visual Studio won't
所以我正在为一个学校项目编写一个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.nnn";
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.nn" : cout << "");
(angle > 90 && angle < 180 ? second = true : second = false);
(second == true ? cout << "The angle you entered is in the second quadrant.nn" : cout << "");
(angle > 180 && angle < 270 ? third = true : third = false);
(third == true ? cout << "The angle you entered is in the third quadrant.nn" : cout << "");
(angle > 270 && angle < 360 ? fourth = true : fourth = false);
(fourth == true ? cout << "The angle you entered is in the fourth quadrant.nn" : 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.nn" : "");
(xaxis == true ? cout << "This angle lies on the positive portion of the x axis.nn" : "");
(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.nn" : "");
(nyaxis == true ? cout << "This angle lies on the negative portion of the y axis.nn" : "");
}
system("pause");
return 0;
}
它在那里运行,但在VS
中不运行我找不到任何逻辑错误或错误原因,但它一直说这个
"1>------ Build started: Project: lab-projects, Configuration: Debug Win32 ------
1> project.cpp
1>c:userswishidocumentsvisual studio 2010projectslab-projectslab-projectsproject.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:userswishidocumentsvisual studio 2010projectslab-projectslab-projectsproject.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:userswishidocumentsvisual studio 2010projectslab-projectslab-projectsproject.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:userswishidocumentsvisual studio 2010projectslab-projectslab-projectsproject.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 =========="
这是一个初级编程课,所以我不知道这是什么意思。有人能帮忙吗?
你在滥用三元操作符,通常和更易读的使用方法是这样的:
xaxis = angle == 180 ? true : false;
你把它当作一个完整的控制结构,而不是if
s。
现在你得到的错误是有意义的,关于三元运算符的规则之一是,?
之后的两个表达式必须具有相同的类型。在这行中,例如:
(nxaxis == true ? cout << "This angle lies on the negative portion of the x axis.nn" : "");
第一个表达式(cout << "..."
)是ostream
,第二个表达式(""
)是const char *
。这就是错误消息试图告诉您的,它试图将第二个表达式转换为ostream
以匹配第一个表达式的类型。
要解决这个问题,您可以像前面几行那样编写表达式,尽管它很难看:
(nxaxis == true ? cout << "This angle lies on the negative portion of the x axis.nn" : cout << "");
或者你可以这样写:
cout << (nxaxis ? "This angle ..." : "");
或者更好:
if (nxaxis)
{
cout << "This angle ...";
}
这样如何:
#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;
}
相关文章:
- 如何在MS Visual Studio 2019中运行QT UI
- 编译并运行后,输出未出现在 Visual Studio 代码中
- Ctrl + 不适用于在 Visual Studio 中运行该程序
- Realloc 两次无法在 Visual Studio 上运行
- Visual Studio 2017 - 调试和 Relese 配置,设置运行时库
- 程序在 Visual Studio 中停止运行时终止
- MinGW是否需要在Visual Studio上运行C++
- 在 Visual Studio 中构建一个没有 C/C++ 运行时的静态库
- 在Visual Studio中运行/调试C ++时,如何在结束时等待输入
- 无法在 Visual Studio 代码中生成和运行 C++ 代码
- 从Visual Studio 2017运行MPI应用程序,每个进程在不同的cmd窗口中
- 如何在 Visual Studio C++ 11 中运行时定义二维数组?
- 为什么我的程序无法在GDB在线编译器/调试器或Visual Studio C++ 2019中运行
- 我可以在不创建项目的情况下在Visual Studio 2019中编译和运行HelloWorld.cpp吗?
- 选择特定版本的 Visual Studio 命令行工具包,并根据特定版本的C++运行时环境编译文件
- 如何在 Linux 中运行 Visual Studio 2015 托管 c++ 测试函数?
- 从命令行运行 Visual Studio Profiler
- 在运行Visual Studio中用C 编写的Oracle DB客户端时,我会收到错误32104
- C Shell(cpp.sh,基于浏览器的编译器)正在运行Visual Studio不会的程序
- 如何在另一台机器上运行Visual Studio 2015中构建的C++应用程序