I'm a student taking an intro to programming course and I'm having some trouble with a recent assignment.
The question asks for a program that will take in integers between 0 and 99 and output their, i guess, "English language equivalent". This is what I have.
int main()
{
int num;
string msg = "The number you entered is ";
cout << "Enter a number between 0 and 99 inclusive: ";
cin >> num;
cout << endl;
int first_digit = num / 10, second_digit = num % 10;
if (num > 0 && num < 100)
{
switch (num)
{
if (num < 20)
{
case 0: cout << msg << "zero.\n";
break;
case 1: cout << msg << "one.\n";
break;
case 2:cout << msg << "two.\n";
break;
case 3: cout << msg << "three.\n";
break;
case 4: cout << msg << "four.\n";
break;
case 5: cout << msg << "five.\n";
break;
case 6: cout << msg << "six.\n";
break;
case 7: cout << msg << "seven.\n";
break;
case 8: cout << msg << "eight.\n";
break;
case 9: cout << msg << "nine.\n";
break;
case 10: cout << msg << "ten.\n";
break;
case 11: cout << msg << "eleven.\n";
break;
case 12: cout << msg << "twelve.\n";
break;
case 13: cout << msg << "thirteen.\n";
break;
case 14: cout << msg << "fourteen.\n";
break;
case 15: cout << msg << "fifteen.\n";
break;
case 16: cout << msg << "sixteen.\n";
break;
case 17: cout << msg << "seventeen.\n";
break;
case 18: cout << msg << "eighteen.\n";
break;
case 19: cout << msg << "nineteen.\n";
break;
}
}
switch (first_digit)
{
if (first_digit > 1 && second_digit > 0)
case 2: cout << msg << "twenty-";
break;
case 3: cout << msg << "thirty-";
break;
case 4: cout << msg << "forty-";
break;
case 5: cout << msg << "fifty-";
break;
case 6: cout << msg << "sixty-";
break;
case 7: cout << msg << "seventy-";
break;
case 8: cout << msg << "eighty-";
break;
case 9: cout << msg << "ninety-";
break;
}
switch (second_digit)
{
if (first_digit > 1 && second_digit > 0)
case 1: cout << "one.\n";
break;
case 2: cout << "two.\n";
break;
case 3: cout << "three.\n";
break;
case 4: cout << "four.\n";
break;
case 5: cout << "five.\n";
break;
case 6: cout << "six.\n";
break;
case 7: cout << "seven.\n";
break;
case 8: cout << "eight.\n";
break;
case 9: cout << "nine.\n";
break;
}
}
else
cout << "Sorry, that is not a number between 0 and 99.\n";
cout << "Thank you for using David's Number Interpreter!\n";
}
Not the most concisely written code ever, but done to the best of my elementary knowledge. EITHER WAY, I'm having an issue where if the user inputs, for example, 2, it will output both "The number you have entered is two" AND "two.", the latter of which is meant to only be printed if the number is two digits long.
I tried uploading the source file to make things easier but it wont permit me.
The reason is very simple. You need to break out of the first switch loop after it is complete instead of continuing on to the next two. You can test this by making a "twoa" and "twob" message string.
1 2 3
switch (num)
etc etc
case 2:cout << msg << "twoA.\n";
1 2 3
switch (second_digit)
etc etc
case 2:cout << msg << "twoB.\n";
There are probably many ways but one that immediately springs to mind is a cascade that only uses the 'num switch' for numbers between 0 and 9 anything bigger will be handled by the others. This entails a small modification including to line 12.