C++计算器程序.问题

C++ Calculator Prog. Issue

本文关键字:问题 程序 计算器 C++      更新时间:2023-10-16

如果有人有什么建议,我可以利用帮助。程序编译,但不编译
正确计算。我已经尝试了无数次重述,但我陷入了困境。有什么建议吗
我必须初始化所有这些变量?非常感谢。

//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;
}

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

然而,你的if的身体周围到处都是你失踪的{}。

例如,它应该是

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

(并且去掉cin.get()调用,除非您将它们用于调试目的)