Hello guys!
Basically I have this basic homework, the program must check if the digit sum of one of the 3 inputted integers is equal to the difference of 2 others. So if A = 56, B = 33 and C = 22, the program should output 56, cause 5+6 = 33 - 22. I think I have the code but I can`t find whats wrong, it always says that such number is not found even tho it should be. Thanks!

``````#include <iostream>
using namespace std;
int main()
{
int a, b, c, suma = 0, sumb = 0, sumc = 0, diga = 0, digb = 0, digc = 0, ok;
do
{
do
{
cout << "Input 3 integers" << endl;
cin >> a >> b >> c;
if (a <= 0 || b <= 0 || c <= 0)
{
cout << "You entered invalid data" << endl;
}
} while (a <= 0 || b <= 0 || c <= 0);
do
{
diga = a % 10;
suma+= diga;
a = a / 10;
} while (a > 0);
do
{
digb = b % 10;
sumb+= digb;
b = b / 10;
} while (b > 0);
do
{
digc = c % 10;
sumc+= digc;
c = c / 10;
} while (c > 0);
if (suma == b-c || suma == c-b)
{
cout << a << endl;
}
else if (sumb == a-c || sumb == c-a)
{
cout << b << endl;
}
else if (sumc == a-b || sumc == b-a)
{
cout << c << endl;
}
else
{
cout << "Does not exist" << endl;
}
cout << "Continue (1) or end (0)?" << endl;
cin >> ok;
} while (ok == 1);
return 0;
}``````

## Recommended Answers

You are changing the value of variables(a, b and c) while computing the sum of their digits.

``a=a/10;``

Then you compare them with the sum.

You just need to have another set of variables to keep the division. Do not reassign your variable a, b, or c at all.

``````// i.e.
int tmpA = a;
do {
diga = tmpA % 10;
suma+= diga;
tmpA = tmpA / 10;
} while (tmpA …``````

## All 7 Replies

You are changing the value of variables(a, b and c) while computing the sum of their digits.

``a=a/10;``

Then you compare them with the sum.

Any possible solutions? :(

You just need to have another set of variables to keep the division. Do not reassign your variable a, b, or c at all.

``````// i.e.
int tmpA = a;
do {
diga = tmpA % 10;
suma+= diga;
tmpA = tmpA / 10;
} while (tmpA > 0);``````

Also, this assumes that the rest of your algorithm is correct...

Oh, right! Thank you!

Hello, I recently came across a situation when 2 numbers are ok, for instance 9 14 and 23, 2+3 = 14-9 and also 9 = 23-14, so it should print out 23 and 9 but it doesn`t. Also if I change `else if`s to `if`s it prints out all the right numbers but always prints out `Does not exist` as well, is there a way to do this without huge repetition of code, which is the only way I know atm.

Do you know how to use functions? You could replace your evaluation with a function. Then, when you need an evaluation done, all you have to do is call the function. This eliminates the repetition by allowing you to re-use one piece of code.

Perhaps something like this:

``````bool evalDigitSum(int valToSum, int compare1, int compare2) {
//calc digit sum for "valToSum" parameter
//calculate difference between "compare#" parameters
//compare digit sum with calculated difference
//return appropriate bool value
}``````

Nope, I do not know how to use functions yet, so I guess I`ll have to repeat the code then.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.