Help With C++ Assignment

Hi everyone, I need help with this assignment my teacher gave me. I'm very lost right now. If someone could help me out and explain how to do this code, I would highly appreciate it! Thanks in advance!

Here is the assignment:


Write a program to read one 3-digit integer number (from 100 to 999) from the console.
If you consider each digit of the number represented by the letters A, B, and C; we can label the number
as ABC.
Your program should:
 Make sure the user enters the appropriate type of number
o i.e. an integer
o And fits in the right range
o You must implement this with a wrapper function (hint: it could be helpful for your
wrapper function to return success or failure)
 Strip each digit A, B, and C from the number
o Example: User enters 452: A should be 4; B should be 5; C should be 2;
o Hint#1: what’s the integer division of 452/100?
o Hint#2: the modulo operator (%) can also be quite handy!
 Form the numbers ABC, BCA, and CAB and store them to variables named accordingly.
o From example above: ABC should be 452; BCA should be 524; CAB should be 254;
 Find the remainders of each of the numbers ABC, BCA, and CAB when divided by 11. Store these
remainders to variables named X, Y, and Z respectively.
 Find and store the sums X+Y, Y+Z, and Z+X. For discussion sake, we’ll refer to each of these sums
as P, R, and Q respectively.
o If any of these sums (P, R or Q) is an odd number, add 11 to it unless that would make
the result (after adding 11) larger than 20-- in that case, decrease the number by 11.
 Basically, the final sum must be an even number but less than 20
o If any of P, R, or Q are already even, leave it be.
 Finally, divide each of the final sums (P, R, and Q after they’ve been pushed into the 0-20 range
and made even) in half.
 Display the output of P, R, and Q
o What values do you get? Anything special/interesting about these numbers?
Your program should provide a formatted printout of all the computed numbers and sums. Your
program should display a welcome message and prompt the user to enter the required numeric input.
Your program should also prevent the user from entering anything but a 3-digit number, and continue to
ask the user for an appropriate input until they enter one.
I would be happy to help but I need a bit more information. Is there a specific place you are having trouble with? Have you written any code?
I'm having trouble with the whole assignment.. :/ I don't really know what to do... But here is my code as of right now:

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main() {
double ABC;


cout << "Welcome! Please enter a 3-digit number:\n";

while (!(cin >> ABC) || (ABC >= 100) || (ABC <= 999)) {

cout << "ERROR! Please enter a 3-digit number!\n";
cin.clear();
cin.ignore();

}

cout << "Thank you! You entered: " << ABC;

system("pause");

return 0;
OK so lets start out with the first goal of the program
 Make sure the user enters the appropriate type of number
o i.e. an integer
o And fits in the right range
o You must implement this with a wrapper function (hint: it could be helpful for your
wrapper function to return success or failure)

Your while loop almost accomplishes this but you must put a ! operator in front of every condition like so while (!(cin >> ABC) || !(ABC >= 100) || !(ABC <= 999))

Now you can move on to the next goal:
 Strip each digit A, B, and C from the number
o Example: User enters 452: A should be 4; B should be 5; C should be 2;
o Hint#1: what’s the integer division of 452/100?
o Hint#2: the modulo operator (%) can also be quite handy!

This one gives you some hints that you should use to try and solve it. The first hint helps you get the first digit
1
2
int A = 452/100
cout << A // A = 4 

So if the program divides the 3 digit number by 100 you will get the first digit.
For the second and third digits try using the % operator which will return the remainder after dividing e.g. 452 % 10 = 2. The second and third digit can be found with the % operator. Let me know if you can get this working.
Last edited on
Sorry about the late reply... So I have been working on this assignment for a day now and I'm almost done. Thank you McNo for the help! However, I still need some help with the last few parts! Here is my code as of right now.

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main() {
int ABC;


cout << "Welcome! Please enter a 3-digit number:\n";

cin >> ABC;

while ((ABC < 100) || (ABC > 999)) {

cout << "Error! Enter a 3-digit number please!\n";
cin >> ABC;

}

cout << "Thank you! You entered: " << ABC << endl;

int C = ABC % 10;
int B = ((ABC - C) % 100) / 10;
int A = (((ABC - C) - (B * 10)) / 100);

int BCA = B * 100 + C * 10 + A;
int CAB = C * 100 + A * 10 + B;

cout << "A (first digit) = " << A << "\nB (second digit) = " << B << "\nC (third digit) = " << C << endl;
cout << "ABC = " << ABC << "\nBCA = " << BCA << "\nCAB = " << CAB << endl;

int X = ABC % 11;
int Y = BCA % 11;
int Z = CAB % 11;

cout << "When each number is divided by 11, the remainders are: \nABC(X)= " << X << "\nBCA(Y)= " << Y
<< "\nCAB(Z)= " << Z << endl;

int P = X + Y;
int Q = Y + Z;
int R = Z + X;

cout << "When the remainders are added, the numbers are: \nP(X+Y)= " << P << "\nQ(Y+Z)= " << Q << "\nR(Z+X)= " <<
R << endl;
int D = P % 2;
int E = Q % 2;
int F = R % 2;

if (D == 1) {
cout << "Since P is odd, we add 11 to it: \nP = " << P + 11 << endl;
}

else if (D == 0) {
cout << "Since P is even, we leave it be! \nP = " << P << endl;
}

if (E == 1) {
cout << "Since Q is odd, we add 11 to it: \nQ = " << Q + 11 << endl;
}

else if (E == 0) {
cout << "Since Q is even, we leave it be! \nQ = " << Q << endl;
}

if (F == 1) {
cout << "Since R is odd, we add 11 to it: \nR = " << R + 11 << endl;
}

else if (F == 0) {
cout << "Since R is even, we leave it be! \nR = " << R << endl;
}


system("pause");

return 0;

}
Hello, sorry for the late reply. What parts are you stuck on? I would be happy to help if you still need it.
Topic archived. No new replies allowed.