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

C Shell (cpp.sh, browser based compiler) is running a program Visual Studio won't

本文关键字:运行 Visual Studio 程序 编译器 sh cpp 浏览器 Shell      更新时间:2023-10-16

所以我正在为一个学校项目编写一个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;
}