gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 03:38:08 25 4
gpt4 key购买 nike

所以我正在为一个学校项目开发一个 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 =========="

这是一门编程入门课,所以我不知道这意味着什么。有人可以帮忙吗?

最佳答案

你在滥用三元运算符,通常和更易读的使用方式是这样的:

xaxis = angle == 180 ? true : false;

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

现在你得到的错误是有意义的。关于三元运算符的规则之一是 ? 之后的两个表达式必须具有相同的类型。例如在这一行:

(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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com