开发者

modulus operator in c++ driving me mad

my names joe and im running into a few issues with the modulus in c++

heres the problem:

#include <iostream>
#include <string>开发者_C百科; 
using namespace std;

int main()
{
//In short, this is what i am trying to do:
//divide two numbers, and get both the quotient
//and the remainder
//however, as an example, this below produces a remainder
//of 10 
//110 \ 20 should equal 5 remainder 5
int firstInput =110, secondInput = 20;

int quotient = 0, remainder = 0;

quotient = firstInput / secondInput;

remainder = firstInput % secondInput;// i think the problem is here

cout << "The quotient is " << quotient << " and the remainder is "
    << remainder << endl;


system("pause");
return 0; 
}

basically its not computing the remainder correctly. any help of course would be much appreciated. cheers


I get the correct answer...

The quotient is 5 and the remainder is 10
Press any key to continue . . .  

I think the bug is probably located between the keyboard and the chair ... =P


110 = 5*20 + 10

the remainder is 10, not 5


110 = 5 * 20 + 10, therefore the quotient is 5, and remainder is 10. So it seems to calculate correctly.

If you want to get the "rest" 0.5, you need to calculate ((double)firstInput/secondInput) - (firstInput/secondInput).


If you divide 110 by 20, you should get a quotient of 5 and a remainder of 10... because:

5*20 + 10 = 110


try fmod to get what you want.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜