2016-09-30 3 views
1

ではありませんので、私は学校プロジェクトのC++プログラムで作業しています。Cシェル(cpp.sh、ブラウザベースのコンパイラ)がプログラムを実行しています。Visual Studioは

私は条件付き演算子の束を使用しました(私は省略形が好きです)、それはCシェルでうまく動作します(これはリンクです)... 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 ==========" 

これは初心者向けプログラミングクラスなので、私はこれが何を意味するのか分かりません。 誰でも助けることができますか?

答えて

0

どのようにこのようなものについて:あなたはそれを使用している

xaxis = angle == 180 ? true : false; 

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

あなたは三項演算子、それを使用して通常の、より読みやすい方法を乱用している。このようなものですifの代わりに完全な制御構造のようなものです。

ここで、あなたはエラーを起こしています。三元演算子に関する規則の1つは、?の後の両方の式が同じ型でなければならないと言います。例えば、このライン上:

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

最初の式(cout << "...")はostreamであり、2番目の("")がconst char *あります。これは、エラーメッセージがあなたに伝えようとしていることです。つまり、2番目の式を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 ..."; 
} 
関連する問題