Q1: How do I reinitialize the number to original input value before each loop?
Q2: How do I test the do loop to repeat the entire on 'y', I am using do ... } while (int (answer) == 121);
Q3: How do I get it to count digits of numbers up to 2^32-1? I am using: int num; unsigned long original; My program will count digits of numbers only up to 2^31-1.
Q4: If we aren't allowed to use a goto statement, how do we have the program repeat itself?
Q5: If we're to use a loop, I understand that the condition to activate is if the variable holding the y/n is y, (answer == y),. what goes in the loop?
Q6: My computer won't let me logout or leave the program. I logged in again at a new terminal, and I seem to be able to enter okay, so I am assuming the other terminal has logged me out of my account by default.
Q7: I cannot figure out how to ask the for loop to calculate the number of iterations performed.
Q8: Will the autograder generate numbers with a leading zero?


Q1: How do I reinitialize the number to original input value before each loop?

A1: In lecture, I mentioned using a variable called "original" or "save". After the statement

cin >> number; // read input
original = number; // save the original input
while (number > 0) // reduce number with while loop
...
number = original; // reinitialize to original input
for ( ... ) // reduce number with for loop

Q2: How do I test the do loop to repeat the entire on 'y', I am using
do
...
} while (int (answer) == 121);

A2: Close, use char answer; // Hold single character 'y'
do
...
} while (answer == 'y'); // while true do again
Do not use the ASCII 121, that is NOT portable. This is not an integer, 'y', but a character.


Q3: How do I get it to count digits of numbers up to 2^32-1? I am using: int num; unsigned long original;
My program will count digits of numbers only up to 2^31-1.

A3: You need to have your variable "num" also unsigned long. When you make the assignment num = original; This assigns from an unsigned long (original) into an int (num). This decreases the number of bits for storage.


Q4: If we aren't allowed to use a goto statement, how do we have the program repeat itself?

A4: Use a loop, how about a do loop.


Q5: If we're to use a loop, I understand that the condition to activate is if the variable holding the y/n is y, (answer == y),. what goes in the loop?

A5: do .{
// Total program of while, for, do loop counting digits
cout << "Do again? ";
cin >> answer;
} while ( answer == 'y' );
This will repeat while true.


Q6: My computer won't let me logout or leave the program. I logged in again at a new terminal, and I seem to be able to enter okay, so I am assuming the other terminal has logged me out of my account by default.

A6: When all else fails in logging out, turn off the terminal. Do not leave your login acount open (not logged off).


Q7: I cannot figure out how to ask the for loop to calculate the number of iterations performed.

A7: number = original; // step 1
while ( number > 0 ) // step 2
{
count++; // step 3
number /= 10; // step 4
}
for ( step 1 ; step 2 ; step 4 )
step 3


Q8: Will the autograder generate numbers with a leading zero?

A8: No leading zero input.


HOME || Course Description || Course Outline || Lab Hours || FAQ