C ++计算器程序。问题

时间:2014-09-23 19:18:19

标签: c++

如果有人有任何建议 - 我可以利用帮助。该程序编译,但不是 正确计算。我已经尝试了很多次重新测试,我感到很困惑。有什么建议吗? 我必须初始化所有这些变量?谢谢....

//This program mimics a calculator
//*****************************************

#include <iostream>
#include <iomanip>

using namespace std;

int main ()
{
double firstint, secint, ansrlt, ansrlt2, ansrlt3, ansrlt4;
char operat;

cout << fixed << showpoint;
cout << setprecision(4);

cout << "This program mimics a standard calculator and outputs a result accurate to 4 decimal   
places." <<endl;
cout << '\n';

cout << "Please enter 1st Integer to be calculated: ";
cin >> firstint;
cout << '\n';

cout << "Please enter operation to be performed in the form of *, /, +, - : ";
cin >> operat;
cout << '\n';

cout << "Please enter 2nd Integer to be calculated: ";
cin >> secint;
cout << '\n';

if (operat == '/' && secint == 0)
    cout << "Division by 0 not allowed enter again." << endl;

cin.get();

if (operat == '*')
    ansrlt = firstint * secint;
    cout << firstint << " " << operat << " " << secint << " = " << ansrlt << endl;

cin.get();

if (operat == '/')
    ansrlt2 = firstint / secint;
    cout << firstint << " " << operat << " " << secint << " = " << ansrlt2 << endl;

cin.get();

if (operat == '+')
    ansrlt3 = firstint + secint;
    cout << firstint << " " << operat << " " << secint << " = " << ansrlt3 << endl;

cin.get();

    ansrlt4 = firstint - secint;
    cout << firstint << " " << operat << " " << secint << " = " << ansrlt4 << endl;

cin.get();

return 0;
}

1 个答案:

答案 0 :(得分:2)

首先,你应该解释你做了什么,发生了什么,以及你期望发生什么 - 因为这通常会使事情变得容易 有人在读你的问题。

然而,你到处都在{@ p>错过{}。{/ p>

e.g。它应该是

if (operat == '*') { 
    ansrlt = firstint * secint;
    cout << firstint << " " << operat << " " << secint << " = " << ansrlt << endl;
}

(并删除cin.get()次来电,除非你将它们用于调试目的)