1

So i am trying to make one of my first very simple math programs in c++. the problem is that i can't seem to get the function potodds to do what i want. There is no problem in getting it to multiple the two variables (x,y) that works perfectly fine. the problem occurs when i try to replace return x*y*100 with return (x/y)*100. in this case it always return the value 0?

Hopes that someone out there can help me pinpoint my mistake.

The code looks like this:

#include "stdafx.h"
#include <iostream>
int x;
int y;
int potodds(int x, int y) {
   return x * y * 100; //(x/y)*100;
}
int main() {
   using namespace std;

   cout << "what's the size of the pot?" << endl;
   cin >> y;

   cout << "what's the size of the bet?" << endl;
   cin >> x;

   cout << "your potodds are:" << endl;
   cout << potodds(x, y) << endl;

   return 0;
}

Thanks to Ebyrob i got the solution.

the problem that I was having was that I was trying to divide an integer, that was assigned a decimal value and by definition an integer can only contain whole numbers. The result was that the integer was rounded down to zero.

Useless
  • 64,155
  • 6
  • 88
  • 132
user2987949
  • 29
  • 1
  • 5
  • 1
    change `int` to `double`. You're seeing truncation on `int` divide. –  Mar 11 '14 at 18:42
  • Or change the order, like `100 * x / y`. – Fred Larson Mar 11 '14 at 18:43
  • @FredLarson Yes, assuming this is a good time to tackle the complexities of operator precedence and order of execution. Also that rounding isn't more appropriate than truncation... –  Mar 11 '14 at 18:46
  • @ebyrob: I don't know that it's any worse than tackling the complexities of floating point. My solution still truncates, but comes a lot closer than always zero. It's the best you can do for an integer solution. – Fred Larson Mar 11 '14 at 18:51
  • @FredLarson You're entitled to your opinion but integer arithmetic **can** round. (Fixed-point for instance) `int r = 1000 * x / y; r += r % 10 >= 5 ? 10 : 0; r /= 10;` –  Mar 11 '14 at 18:57

3 Answers3

1

x and y are defined as integral values. The integral division returns only the quotient. So if x is less than y then x /y will be equal to 0.

So it would be better to substitute expression ( x /y ) * 100 for ( 100 * x ) / y

Otherwise use float numbers

For example

( double )x / y * 100
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

Changing int to double should solve the problem but also "using namespace std;" is always outside of main when I program. I don't know if this affects anything but you might consider putting it above your main function because it might be causing a problem now or it might cause a problem in another program you make. I think it is a matter of it being global or private but I would have it outside of main so that any other functions you use can use it ( assuming I'm right ).

0

If you divide two integers, the result will be an integer (the quotient: the remainder is discarded).

So, 1 / 2 -> 0 instead of 0.5.

Note also that (1 / 2) * 100 is therefore 0 * 100 -> 0, while, as Vlad says, 100*1/2 -> 100/2 -> 50, which is what you want.

Useless
  • 64,155
  • 6
  • 88
  • 132