2017-06-02 11 views
0

場合、コードは他を実行したり、他にはありません。免責事項として、私が完了し、全くの初心者です、私は私のコードがひどく電気ショック療法をレイアウトされている知っていると私は今、私はそれをシンプルに保つよ...に関数を作成する必要があります。C++ - 文の場合。私は以下の私のコードを入力します

私の問題は私のif文は「場合」の部分を実行しますが、それは「他の場合」の部分を実行しないということです。どんな助けもありがとうございます。

マイコード:

// Coding Challenges.cpp : Defines the entry point for the console 
application. 
// 
#include "cstdlib" 
#include "stdafx.h" 
#include <stdio.h> 
#include <iostream> 
#include <string> 

using namespace std; 

char responses(char user_response) 
{ 
return user_response; 
} 

int main() 
{ 
char responses; 
char user_response; 
cout << "Welcome to the tempature conversion program." << endl; 
cout << "Please type F to convert from farenheit to celcius or C for celcius 
to farenheit." << endl; 
//getchar(user_response); 
cin >> user_response; 
getchar(); 
cout << "You entered: " <<user_response << endl; 


switch (user_response) { 
case 'F': 
    cout << "You have chosen to convert from farenheit to celcius." << endl; 
    break; 

case 'C': 
    cout << "You have chosen to convert from celcius to farenheit." << endl; 
    break; 

default: 
    cout << "Woops. It Looks like you haven't entered correctly." << endl; 
    break; 
} 
getchar(); 
cout << "Code should have ran " << endl; 
getchar(); 
cout << "You entered: " << user_response << endl; 
getchar(); 

if (user_response = 'F') { 
    double f_val = 0; 
    cout << "Please enter the value of farenheit you would like to convert: 
" << endl; 
    cin >> f_val; 
    cout << f_val; 
    getchar(); 
    getchar(); 


} else if (user_response = 'C') { 
    double c_val = 0; 
    cout << "Please enter the value of celcius you would like to convert: " 
<< endl; 
    cin >> c_val; 
    cout << c_val; 
    getchar(); 
    getchar(); 


} else { 
    cout << "Error" << endl; 
    return 0; 
    getchar(); 
    getchar(); 
} 
getchar(); 
getchar(); 

return 0; 
} 

は、私は明らかに、テスト/デバッグのために使用されている任意のコードを無視します。あなたは代入演算子(=)と等しくない比較1(==)を使用しているよう

おかげで、

テッド

+2

'USER_RESPONSE = F''「F''は' USER_RESPONSE ==べきである」そうでなければ、割り当てではなく、比較を行っています – CoryKramer

+0

ところで使用した機能は、それをシンプルに保つための一つの方法です。 100行の1ブロックと比較して約10行のコードの10倍の理由を考えるのははるかに簡単です – user463035818

+1

これは間違いなく「達成されたロック解除」です。誰もがその間違いを数回しなければなりません! '='は代入 '=='は比較ですが、どちらも 'if'条件で出現できますが、動作は非常に異なります。 – Persixty

答えて

1

あなたは、そこに任意の文字でuser_responseを比較していません。 user_responseは、あなたはそれに「F」が割り当てられた場合である、0と異なる場合、あなただけのテストしているように、あなたは常に、あなたの最初の条件を入力しています。値をテストするには、==とnot =を使用する必要があります。

if (user_response == 'F') { // == operator there, and not = 
    double f_val = 0; 
    cout << "Please enter the value of farenheit you would like to convert: 
" << endl; 
    cin >> f_val; 
    cout << f_val; 
    getchar(); 
    getchar(); 


} else if (user_response == 'C') { // Same there 
    double c_val = 0; 
    cout << "Please enter the value of celcius you would like to convert: " 
<< endl; 
    cin >> c_val; 
    cout << c_val; 
    getchar(); 
    getchar(); 


} else { 
    cout << "Error" << endl; 
    return 0; 
    getchar(); 
    getchar(); 
} 
+0

それはそれを修正しました。ありがとうございました! – TedLeveTT

1

あなたのラインが

if (user_response = 'F') { 

あなただけの 'F' にuser_responseを設定したよう

user_response = 'F'; 
if (user_response != 0) { 

に相当する

user_response = 'F'; 
if (user_response) { 

と同等であることは明らかではありません0、あなたは常にif-path。

比較したい場合、あなたは、コンパイラの警告レベル(常に良いアイデア)を設定し、慎重に警告を読み取ることによって、それらのエラーをキャッチすることができ、コンパイラによって

if (user_response == 'F') { 

を記述する必要があります。あなたがcomaringが、ここで割り当てされていません

+0

ああ、ありがとう!それはそれを修正した。私はおそらくcppの演算子を知っているはずです...助けてくれてありがとう。 – TedLeveTT

0

if (user_response = 'F') 
        ^------ This is assignment 
        v------ This is comparison 
if (user_response == 'F') 

割り当ての値はboolと利回りtrueに変換することができるものです。ほとんどのコンパイラはこれを警告します。コンパイラのエラーで、このようなタイプミスをキャッチする方法は、このようにそれを記述することです:

if ('F' = user_response) // -> compiler error !! 
    if ('F' == user_response) // -> ok 
関連する問題