#Unexpected result (Printing double value as int)

10 messages · Page 1 of 1 (latest)

dusty ruin
#
#include <iostream> 


void calculate(double a, double b) {

    std::cout << "What mathmatical opeartion should the computer perform?\n";
    std::cout << "+ - * / %\n";

    char z{};
    std::cin >> z;

    switch (z) {

    case '+':
        std::cout << a << " + " << b << " equals: " << a + b;
        return; 

    case '-':
        std::cout << a << " - " << b << " equals: " << a - b;
        return;

    case '*':
        std::cout << a << " * " << b << " equals: " << a * b;
        return;

    case '/':
        std::cout << a << " / " << b << " equals: " << a / b;
        return;

    case '%':
        std::cout << a << " % " << b << " equals: Remainder " << static_cast<int>(a) % static_cast<int>(b);



    }

}

int main() {

    std::cout << "Enter a number: ";

    double x{};
    std::cin >> x;

    std::cout << "Enter another number: ";

    double y{};
    std::cin >> y;

    calculate(x, y);


    return 0;
}
ashen pivotBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

dusty ruin
#

Why it's 15.3 not 15

trim shadow
#

Because a is a double, not a int

dusty ruin
#

so I expect the fractional part to be dropped

#
#include <iostream> 



int main() {

    double a{ 5.3 };

    std::cout << static_cast<int>(a);


    return 0;
}

this program drops the fractional part unlike the one above for some reason

trim shadow
dusty ruin
#

ah

#

!solved