hello, I'm having issues understanding what to do in this exercise.
it"s a C++ using netbeans
Write a program that asks the user to enter 2 integers of 4 digits
and displays the different digits as X and * if they are the same. If
the user enters a number that is not of four digits, then a
convenient message is displayed.
For example, if the user enters
1234
1432
Then the output should be: *X*X
The easiest way to do this would be to input the data as strings, then check to make sure there aren't non-digits in the string, then check to make sure the length of each string is 4.
Then, it's a simple loop where you output '*' if strA[i] == strB[i], else output 'X'.
#include <iostream>
void shambles( int a, int b )
{
if ( a >= 10 ) shambles( a / 10, b / 10 );
std::cout << "X*"[ a % 10 == b % 10 ];
}
int main()
{
shambles( 1234, 1432 );
}
The easiest way, then, is to compare the integers as strings (Ganado's suggestion).
Otherwise you can peel digits off using / and % operators (there are actually quite a few ways of doing that if you know the integers have a specific number of digits) and compare them one by one.
ok then, I know how to peel off digits, but how to compare them after that, I mean how to make them variables to insert them in the (while, if, else) operation.