を取得しておくと、私はキロメートルとマイルコンバータアプリケーションを作成しようとしています。コードにコードブロックを使用しています。選択肢2は結果として0を与え続けます。ここで私が入力しています正確なコードです:私のC++コードで何が問題になっていますか?私はプログラミングの初心者だ間違った結果
#include <iostream>
using namespace std;
double choice, value1, result;
// I'm sure I messed up somewhere after this:
double value2 = .621371;
double value3 = 1.609344;
// Are these two lines^supposed to be here?
int main()
{
while (true) {
cout << "1. Kilometers to Miles" << endl;
cout << "2. Miles to Kilometers" << endl;
cout << "3. Exit the Application" << endl;
cout << "Please enter a choice: ";
cin >> choice;
if (choice == 3)
break;
cout << "Please enter the first value: ";
cin >> value1;
// This if statement keeps giving me 0:
if (choice == 2)
result = value1 * value2;
// I believe this part here is okay:
else if (choice == 3)
result = value1/value3;
cout << "The result is: " << result << endl;
}
}
ええと? 'choice'が' 3'なら2回、 'choice'が' 1'ならば何もチェックしませんか?だから、 'switch'が' else else'のすべてのチェーンに勝っているのです。 – SJuan76
また、あなたの選択に 'double'を使わないでください。これは 'int'(または他の整数型)が非常に適切であるところです。それはあなたのケースでエラーを引き起こすことはありませんが、道路のトラブルに向かっているように倍を比較する。浮動小数点の不正確さについて学ぶとき、それについて学びます。 –
私はswitch文をまだ学んでいません。私はすぐにそれを学ぶつもりです。 – MrAckills