私は単純な電卓を作成する課題がありますが、プログラムを終了できません。それは連続ループのままです。私は間違って何をしていますか?簡単な計算機C + +、どのように正しくループし、終了する?
割り当ては単純です: 加算、減算、乗算、除算の演算を実行する関数を使って基本的な電卓をシミュレートするC++プログラムを作成します。
実行可能な操作のリストのメニューを表示し、ユーザーからの計算の選択についての入力を取得します。ユーザーの操作の選択に基づいて、ユーザーからの番号の入力を受け取ります。すべての入力値がdouble型であるとします。計算は、double値を返す関数を使用して実装する必要があります。計算結果は画面に表示されます。この
#include <iostream>
#include <stdlib.h>
using namespace std;
double add(double num1, double num2)
{
return num1 + num2;
}
double subtract(double num1, double num2)
{
return num1 - num2;
}
double multiply(double num1, double num2)
{
return num1 * num2;
}
double divide(double num1, double num2)
{
if (num2 != 0)
return num1/num2;
else
cout << "Invalid entry";
}
int main()
{
double num1, num2;
char operation, q;
cout << "Calculator functions:" << endl;
cout << "Enter 'm' for Multiplication" << endl;
cout << "Enter 'd' for Division" << endl;
cout << "Enter 'a' for Addition" << endl;
cout << "Enter 's' for Subtraction" << endl;
cout << "Select operation or 'q' to quit program" << endl;
cin >> operation;
do
{
if (operation == 's' || operation == 'S')
{
cout << "Enter first number" << endl;
cin >> num1;
cout << "Enter first number" << endl;
cin >> num2;
cout << "The difference is " << subtract(num1, num2) << endl;
}
if (operation == 'a' || operation == 'A')
{
cout << "Enter first number" << endl;
cin >> num1;
cout << "Enter first number" << endl;
cin >> num2;
cout << "The sum is " << add(num1, num2) << endl;
}
if (operation == 'm' || operation == 'M')
{
cout << "Enter first number" << endl;
cin >> num1;
cout << "Enter first number" << endl;
cin >> num2;
cout << "The product is " << multiply(num1, num2) << endl;
}
if (operation == 'd' || operation == 'D')
{
cout << "Enter numerator" << endl;
cin >> num1;
cout << "Enter denominator" << endl;
cin >> num2;
cout << "The quotient is " << divide(num1, num2) << endl;
}
cout << "Select operation or 'q' to quit program" << endl;
cin >> operation;
} while (operation != 'q' || operation != 'Q');
cout << "Goodbye!" << endl;
system("pause");
return 0;
}
'しばらく(!操作= 'Q' ||操作= 'Q');' => 'しばらく(!操作= 'Q' &&操作= 'Q');' –
'operation!= 'q' || operation!= 'Q''は任意の値に当てはまります。あなたは@πάνταῥεadviceのアドバイスに従うか、常に入力文字を小文字(または大文字、あなたの選択)に変換してから、一文字だけをチェックする必要があります。 –
サイドノート - 最初に 'operation'を大文字または小文字に変換し、すべてのORをスキップすると考えましたか? –