-2

Need the code bellow to accept values in double and convert in int

using namespace std;

SymbolTable symbolTable;

void parseAssignments();

int main()

{

    Expression* expression;

    char paren, comma;

    cout << "Enter expression: ";

    cin >> paren;

    expression = SubExpression::parse();

    cin >> comma;

    parseAssignments();

    cout << "Value = " << expression->evaluate() << endl; 

    return 0;

}

void parseAssignments()

{

    char assignop, delimiter;

    string variable;
    double value;

    do

    {

        variable = parseName();

        cin >> ws >> assignop >> value >> delimiter;

        symbolTable.insert(variable, value);

    }

    while (delimiter == ',');

}
Scheff's Cat
  • 19,528
  • 6
  • 28
  • 56
rrtt
  • 1

1 Answers1

0

A quick way to convert from double to int is to set an int equal to a double. The int will store the truncated value, meaning it does not round after the decimal, merely ignores it. For example:

double pi = 3.14;
int piApprox = pi;

cout<<pi<<endl; // This will output 3.14
cout<<piApprox<<endl; //This will output 3

Hope this helps, your questions is very similar to this post here. That one goes into more detail about the engineering aspects of how floating point numbers work in computers and precautions in converting from float to int.