Issue when dividing/modulus?

The task is simple, ask the user for two integers, then display the sum, difference, product, quotient, and modulus. My issue is when I test my division and modulus code. If integerOne is bigger than integerTwo, the result is zero. (for both division and modulus.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "stdafx.h"
#include <iostream>
using namespace std;


int main()
{
	int integerOne;
	cout << "Please enter an integer:";
	cin >> integerOne;

	int integerTwo;
	cout << "Please enter another integer:";
	cin >> integerTwo;

	int diff;

	if (integerOne > integerTwo)
		diff = integerOne - integerTwo;
	else (integerOne < integerTwo);
		diff = integerTwo - integerOne;

	int sum;
	sum = integerOne + integerTwo;

	int product;
	product = integerOne * integerTwo;

	int quotient;
	if (integerOne > integerTwo)
		quotient = (integerOne / integerTwo);
	else (integerOne < integerTwo);
		quotient = (integerTwo / integerOne);

	int modulus;
	if (integerOne > integerTwo)
		modulus = (integerOne % integerTwo);
	else (integerOne < integerTwo);
	modulus = (integerTwo % integerOne);




    return 0;
}

Now try it
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
using namespace std;


int main()
{
	int integerOne;
	cout << "Please enter an integer:";
	cin >> integerOne;

	int integerTwo;
	cout << "Please enter another integer:";
	cin >> integerTwo;

	int diff;

	if (integerOne > integerTwo)
		diff = integerOne - integerTwo;
	else //(integerOne < integerTwo);
	diff = integerTwo - integerOne;

	cout << "Difference: " << diff << endl;

	int sum;
	sum = integerOne + integerTwo;

	cout << "Sum: " << sum << endl;

	int product;
	product = integerOne * integerTwo;

	cout << "Product: " << product << endl;

	int quotient;
	if (integerOne > integerTwo)
		quotient = (integerOne / integerTwo);
	else //(integerOne < integerTwo);
	quotient = (integerTwo / integerOne);

	cout << "Quotient: " << quotient << endl;

	int modulus;
	if (integerOne > integerTwo)
		modulus = (integerOne % integerTwo);
	else //(integerOne < integerTwo);
	modulus = (integerTwo % integerOne);

	cout << "Modulus: " << modulus << endl;


	return 0;
}


You are using if and else, else doest test for any condition. If you swapped the else statements with if else ones it would probably work.

if (something true)
do this
else if (something true)
do this
else
do this

Also, use brackets please it makes it easier to read and understand.
Last edited on
(almost) a duplicate of the later post.
Last edited on
@joe864864, I changed else to else if and nothing changed.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <cstdlib> // for std::abs(int)
using namespace std;

int main()
{
    int integerOne;
    cout << "Please enter an integer:";
    cin >> integerOne;

    int integerTwo;
    cout << "Please enter another integer:";
    cin >> integerTwo;

    std::cout << "\nintegerOne: " << integerOne
              << "\nintegerTwo: " << integerTwo << "\n\n" ;


    // http://en.cppreference.com/w/cpp/numeric/math/abs
    const int diff = std::abs( integerOne - integerTwo ) ;
    const int sum = integerOne + integerTwo;
    const int product = integerOne * integerTwo;
    std::cout << "diff: " << diff << '\n'
              << "sum: " << sum << '\n'
              << "product: " << product << '\n' ;

    const int dividend = std::abs(integerOne) > std::abs(integerTwo) ? integerOne : integerTwo ;
    const int divisor = dividend == integerOne ? integerTwo : integerOne ;
    std::cout << "dividing " << dividend << " by " << divisor << '\n' ;

    if( divisor != 0 )
    {
        const int quotient = dividend / divisor ;
        const int remainder = dividend % divisor ;

        std::cout << "quotient: " << quotient << '\n'
                  << "remainder: " << remainder << '\n' ;
    }

    else std::cout << "error: can't divide by zero\n" ;
}

http://coliru.stacked-crooked.com/a/873dfd9b200721c1
Last edited on
Topic archived. No new replies allowed.