0

I am trying to learn C++, and I have only just started, but I have written the following:

#include "stdafx.h"
#include <iostream>
#include <iomanip> // for setPrecision()
#include <cmath>


int getVal() 
{
    using namespace std;
    int value;
    cin >> value;
    return value;
}

char getChar() 
{
    using namespace std;
    char mathOperator;
    cin >> mathOperator;
    return mathOperator;
}
double doCalc(int a, int b, char mO)
{
    using namespace std;
    cout << a << mO << b << " = "; 
    double result;
    switch(mO) 
    {
        case '+': result = a+b; break;
        case '-': result = a-b; break;
        case '*': result = a*b; break;
        case '/':  result = a/b; break;
    }
    cout << setprecision(20);
    cout << result << endl;
    return result;
}

bool isEven(double x)
{
    if(fmod(x,2))  {
        return false;
    } else {
        return true;
    }
}


int main() {
    using namespace std;



    cout << "Playing with numbers!" << endl << endl;
    cout << "Enter a value: ";
    int a = getVal();
    cout << "Enter another value: ";
    int b = getVal();
    cout << "Enter one of the following: (+, -, *, /)";
    char mathOperator = getChar();
    double result;
    result = doCalc(a,b,mathOperator);

    switch(isEven(result)) 
    {
        case true: cout << "Your number is even." << endl; break;
        case false: cout << "Your number is odd." << endl; break;
    }
    return 0;
}

It's pretty simple I know, but for some reason in the function doCalc() I cannot seem to output decimal places. I've used setprecision but it makes no difference. The numbers I test with are 100 / 3, which should be 33.33333333333333333333333333. I simply get 33.

Can anyone tell me why?

Chud37
  • 4,907
  • 13
  • 64
  • 116
  • 4
    int / int always gives you an int back. Try to cast to double/float before the calculation. – Ra1nWarden Aug 31 '14 at 14:27
  • dividing an int by an int will give me an int? – Chud37 Aug 31 '14 at 14:28
  • 1
    Please read about [minimal examples](http://stackoverflow.com/help/mcve). – Tom Zych Aug 31 '14 at 14:28
  • @Ra1nWarden thanks, that seems to have done it! I am surprised by that actually, since the variable I am pushing the calculation to is a double. But that seems to have done it. – Chud37 Aug 31 '14 at 14:29
  • @Chud37 yes. Try divide 1 by 3. You will not get 0.3333 :) – Ra1nWarden Aug 31 '14 at 14:29
  • 1
    It computes the division as an int first. Only afterward does it promote it to a double, to store it. – Tom Zych Aug 31 '14 at 14:30
  • 2
    @Chud37 it does not matter what you assign the result to -- this is not Perl ;) -- as the calculation is done before. In any case it would probably be best to buy a book on C++ as there are so many things (slightly) different in C++ compared to over languages. – mfuchs Aug 31 '14 at 14:31

1 Answers1

2

Let's look at some simple code:

std::cout <<   4 / 3 << std::endl; // Outputs the integer 1
std::cout << 4.0 / 3 << std::endl; // Outputs the double 1.3333333333

Integer / Integer gives a integer result rounded towards zero.

If you pass a float or a double (note the 4.0, which is a double), then you get decimal places.

In your particular case, I'd recommend:

    case '/':  result = static_cast<double>(a) / b; break;

or:

    case '/':  result = (double) a / b; break;
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173