Here is code that includes two switch statements to print out a two digit number entered, 1-99. I now need to add a FOR loop so it repeats up to 10 times. After each loop it should prompt the user cout << "Would you like to enter another number? (Y/N)"; If the user enters Y, y, Yes, YES, or yes. it should do the loop again, if user enters N, n, No, NO or no. it should break out of loop and end. I cannot figure out where in the code the FOR loop should be placed and how it should be coded for it to work?
#include <iostream>
using namespace std;
int main()
{
int num, double_digit, first_digit, second_digit;
cout << "Enter a two-digit number: ";
cin >> double_digit;
cout << endl;
cout << "You entered the number ";
Perhaps you should make the above code work first; it has several logic errors in it which will be apparent if you run the program with various inputs.
I have tried numerous inputs and the output is always correct. all exceptions 10-19 also work fine. I am a beginner and I am sure the above code isn't perfect, but it does work.
But you've accounted for this by adding a "return 0;"
at the end of case 1 inside the first switch instead of having a break;
Essentially you want the entire body of your program so far to run multiple times until the user
enters 'no'. A while loop is probably a little more understandable, but you might be forced to
use a for loop by your instructor.
1 2 3 4 5 6
for( int repetition_no = 0; repetition_no < 10; ++repetition_no ) {
// do your program
// ask if user wants to continue and get answer
// if user does not want to continue, break;
}
Thanks for responding. So if i remove the semicolon and add a break instead of return 0 at the end of case 1 in the first switch, is that the correct way to code this?
Basically the numbers 1 through 9 are special. So make some if() statements for those.
11 through 19 have to be treated specially. So make some if() statements for those.
All other numbers can be handled in a fashion similar to how you are handling them now.