The description of the problem as a whole might sound formidable, but you just need to break it down into bite-sized pieces.
For example, just start with:
write a program that reads a positive least 2 digit and at most 6 digit integer |
and
do not accept values less than 10 or greater than 999999. |
You're just getting a number from the user.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
#include <iostream>
#include <string>
using namespace std; // sue me
int main()
{
cout << "Enter a number between 10 and 999999: ";
int number;
cin >> number;
if (number < 10 || number > 999999)
{
cout << "value not accepted\n";
return 1;
}
cout << "Value entered: " << number << '\n';
return 0;
}
|
So then, the next part is
and then checks if whether the last digit is a correct checksum which is the remainder in the division by 10 of the sum of the digits except for the last digit |
Even this may sound like a lot, but let's focus on the first part: "check .. the last digit".
How do you get the last digit of a number? do
number % 10.
And then, you have
sum of the digits except for the last digit |
.
How do you get the sum of the digits of a number? Just focus on this as your problem.
For example, just make a test program that prints the digits of a number:
1 2 3 4 5 6 7 8 9 10 11 12
|
#include <iostream>
#include <string>
void print_digits(int number)
{
// ...
}
int main()
{
print_digits(1123);
}
|
Hint: number % 10 gets you the last digit of a number (1123 --> 3)
number / 10 divides the number by 10 to remove the last digit (1123 --> 112).