Quotient and remainder calculator

Dec 26, 2018 at 6:44pm


This is my source code which is meant to find the quotient and the remainder but it seems that it just enters the loop once and just outputs n and 0 all the time?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <cmath>

int main (){

int n, m ,q= 0 ;

std::cout << " please enter the two integers" << std::endl;
std:: cin >> n, m;

while(n>=m){
    n=n-m;
    q=q+1;

}

std::cout<< " the remaineder is " << n << " and the quotient is " << q << std::endl;

return 0;

}


I would appreciate any help. Thanks.
Last edited on Dec 26, 2018 at 6:44pm
Dec 26, 2018 at 6:57pm
Line 9 is misuse of the comma operator. My advice is to just not use it.
Do this instead:
cin >> n >> m;

PS: You might already know this, but this can be calculated much simpler using built-in operators.
 
    std::cout<< " the remainder is " << n%m << " and the quotient is " << n/m << std::endl; 
Last edited on Dec 26, 2018 at 7:00pm
Dec 26, 2018 at 7:12pm
That makes sense thank you. I am aware of this just trying different things to learn. Thanks.
Last edited on Dec 26, 2018 at 7:12pm
Topic archived. No new replies allowed.