fork(1) download
  1. //Nicolas Ruano CS1A Chapter 4, Pp. 220, #2
  2. //
  3. /******************************************************************************
  4.  *
  5.  * CONVERTING TO ROMAN NUMERALS
  6.  * ____________________________________________________________________________
  7.  * The program converts numbers n a set of 1-10 into a set of roman numerals,
  8.  * based on the set of numbers listed
  9.  *
  10.  *_____________________________________________________________________________
  11.  * INPUT
  12.  * NumVal :number value enter by the user
  13.  *
  14.  * OUTPUT
  15.  * number roman of numbers in a set of 1-10
  16. ******************************************************************************/
  17. #include <iostream>
  18. using namespace std;
  19.  
  20. int main() {
  21. int number;
  22. char choice; // For menu option
  23.  
  24. cout << "Welcome to the Roman Numeral Converter!\n";
  25.  
  26. do {
  27. // Step 1: Ask user for a number
  28. cout << "\nPlease enter a number from 1 to 10: ";
  29. cin >> number;
  30.  
  31. // Step 2: Input validation with nested if statements
  32. if (number < 1) {
  33. cout << "Oops! The number is too small. Try a number between 1 and 10.\n";
  34. } else if (number > 10) {
  35. cout << "Oops! The number is too big. Try a number between 1 and 10.\n";
  36. } else {
  37. // Step 3: Use switch to convert number to Roman numeral
  38. cout << "The Roman numeral for " << number << " is ";
  39. switch(number) {
  40. case 1: cout << "I\n"; break;
  41. case 2: cout << "II\n"; break;
  42. case 3: cout << "III\n"; break;
  43. case 4: cout << "IV\n"; break;
  44. case 5: cout << "V\n"; break;
  45. case 6: cout << "VI\n"; break;
  46. case 7: cout << "VII\n"; break;
  47. case 8: cout << "VIII\n"; break;
  48. case 9: cout << "IX\n"; break;
  49. case 10: cout << "X\n"; break;
  50. default: cout << "Error\n"; // Safety net
  51. }
  52. }
  53.  
  54. // Step 4: Ask if the user wants to convert another number
  55. cout << "\nDo you want to convert another number? (Y/N): ";
  56. cin >> choice;
  57.  
  58. // Use conditional operator to display a friendly message
  59. cout << ((choice == 'Y' || choice == 'y') ? "Great! Let's do another one." : "Okay! Thanks for using the converter.\n");
  60.  
  61. } while(choice == 'Y' || choice == 'y');
  62.  
  63. cout << "Goodbye!\n";
  64. return 0;
  65. }
  66.  
Success #stdin #stdout 0.01s 5328KB
stdin
Standard input is empty
stdout
Welcome to the Roman Numeral Converter!

Please enter a number from 1 to 10: Oops! The number is too big. Try a number between 1 and 10.

Do you want to convert another number? (Y/N): Okay! Thanks for using the converter.
Goodbye!