For Loop (i=1; i<10; i++)

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 ";

first_digit = double_digit / 10;
second_digit = double_digit % 10;

if (first_digit == 1);

switch (first_digit)

{
case 1:
if (double_digit == 10) cout << "ten.";
if (double_digit == 11) cout << "eleven.";
if (double_digit == 12) cout << "twelve.";
if (double_digit == 13) cout << "thirteen.";
if (double_digit == 14) cout << "fourteen.";
if (double_digit == 15) cout << "fifteen.";
if (double_digit == 16) cout << "sixteen.";
if (double_digit == 17) cout << "seventeen.";
if (double_digit == 18) cout << "eighteen.";
if (double_digit == 19) cout << "nineteen.";

cout << endl;
cin >> num;

return 0;

case 2:
cout << "twenty ";
break;

case 3:
cout << "thirty ";
break;

case 4:
cout << "forty ";
break;

case 5:
cout << "fifty ";
break;

case 6:
cout << "sixty ";
break;

case 7:
cout << "seventy ";
break;

case 8:
cout << "eighty ";
break;

case 9:
cout << "ninety ";
break;
}

switch (second_digit)

{
case 1:
cout << "one." << endl;
break;

case 2:
cout << "two." << endl;
break;

case 3:
cout << "three." << endl;
break;

case 4:
cout << "four." << endl;
break;

case 5:
cout << "five." <<endl;
break;

case 6:
cout << "six." <<endl;
break;

case 7:
cout << "seven." << endl;
break;

case 8:
cout << "eight." << endl;
break;

case 9:
cout << "nine." << endl;
break;
}

cin >> num;
return 0;

}
Last edited on
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.
Sorry, two wrongs make a right.

Your line

 
if( first_digit == 1 );


does nothing because of the semicolon at the end.

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?

No, it still won't make sense.

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.
Topic archived. No new replies allowed.