場合、コードは他を実行したり、他にはありません。免責事項として、私が完了し、全くの初心者です、私は私のコードがひどく電気ショック療法をレイアウトされている知っていると私は今、私はそれをシンプルに保つよ...に関数を作成する必要があります。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(==)を使用しているよう
おかげで、
テッド
'USER_RESPONSE = F''「F''は' USER_RESPONSE ==べきである」そうでなければ、割り当てではなく、比較を行っています – CoryKramer
ところで使用した機能は、それをシンプルに保つための一つの方法です。 100行の1ブロックと比較して約10行のコードの10倍の理由を考えるのははるかに簡単です – user463035818
これは間違いなく「達成されたロック解除」です。誰もがその間違いを数回しなければなりません! '='は代入 '=='は比較ですが、どちらも 'if'条件で出現できますが、動作は非常に異なります。 – Persixty