0

I'm learning C++ through a great tutorial at http://www.learncpp.com/cpp-tutorial/210-comprehensive-quiz As part of the quiz I need to create a calculator however the code always seems to drop the decimal. I believe that this occurs during the return statement. Anyways, any help for a newbie learning C++ is greatly appreciated. My code:

#include "stdafx.h"
#include <iostream>
int input(float);
int math(double, double, char);
void print(double);

int _tmain(int argc, _TCHAR* argv[])
{
    double fNumberOne = input(0);
    double fNumberTwo = input(1);
    char Operator = input(2);
    print(math(fNumberOne, fNumberTwo, Operator));
    return 0;
}

int input(float x)
{
    using namespace std;
    if (x == 0)
    {   
        cout << "Please enter the first number" << endl;
    double input;
    cin >> input;
    return input;
    }
    if (x == 1)
    {
        cout << "Please enter the second number" << endl;
        double input;
        cin >> input;
        return input;
    }
    if (x == 2)
    {
            cout << "Please enter the operator" << endl;
        char op;
        cin >> op;
        return op;
    }
}

int math(double x, double y, char op)
{
    double result(0);
    if (op == '+')
        result = x + y;
    if (op == '-')
        result = x - y;
    if (op == '/')
        result = x / y;
    if (op == 'x')
        result = x *y;
        return result;
}
void print(double x)
{
    using namespace std;
    cout << x << endl;
}
4
  • 3
    You are returning all of your values as integers. Try using floats or doubles. Sep 22, 2014 at 5:09
  • I declared all my variables as double or char how do I make the return statement return double rather than int? Sep 22, 2014 at 5:12
  • Errr... declare the function as returning one instead of an int? Sep 22, 2014 at 7:15
  • hey! All your return values are ints, you have to return them as double or they will be cast and loose their decimals!
    – Stígandr
    Sep 22, 2014 at 7:50

2 Answers 2

3

When you return from the function input you are returning an int:

int input(float x)
{
    ...
    double input;
    cin >> input;
    return input; // A cast happens here to turn the double into an int. 

You will need to return a double ie double input(float x).

0
1

Note that your double is being implicitly cast to an int. Which is resulting in the following behavior:

  • A prvalue of floating-point type can be converted to prvalue of any integer type. The fractional part is truncated, that is, the fractional part is discarded. [...]

All you have to do is modify the return values of your functions to be a double rather than an int.

double input(float);
double math(double, double, char);

double input(float x)
{
  //...
}

double math(double x, double y, char op)
{
  //...
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.