fork download
  1. //Maxwell Brewer CS1A Chapter 4, P. 220, #2
  2. //
  3. /***************************************************************
  4.  *
  5.  * CONVERT ROMAN NUMERALS
  6.  * _____________________________________________________________
  7.  *
  8.  * This program will take a number between 1 and 10, then
  9.  * convert it to the equivalent Roman Numeral using an
  10.  * infinite while loop and switch statement.
  11.  * _____________________________________________
  12.  * INPUT
  13.  *
  14.  * number (1-10)
  15.  *
  16.  * OUTPUT
  17.  *
  18.  * number (I-X)
  19.  *
  20.  ***************************************************************/
  21.  
  22. #include <iostream>
  23. #include <iomanip>
  24. #include <string>
  25.  
  26. using namespace std;
  27.  
  28. int main() {
  29.  
  30. // Initialization
  31.  
  32. const char ENT_NUM[] = "Enter a number (1-10): ";
  33. const char MSG_ROM[] = "The Roman Numeral is: ";
  34. const char MSG_ERROR[] = "Invalid entry";
  35.  
  36. int number;
  37.  
  38. // Display input prompts
  39.  
  40. cout << ENT_NUM;
  41. cin >> number;
  42.  
  43. // Output
  44.  
  45. while (1)
  46. {
  47. cout << MSG_ROM;
  48.  
  49. switch (number)
  50. {
  51. case 1: cout << " I " << endl;
  52. break;
  53.  
  54. case 2: cout << " II " << endl;
  55. break;
  56.  
  57. case 3: cout << " III " << endl;
  58. break;
  59.  
  60. case 4: cout << " IV " << endl;
  61. break;
  62.  
  63. case 5: cout << " V " << endl;
  64. break;
  65.  
  66. case 6: cout << " VI " << endl;
  67. break;
  68.  
  69. case 7: cout << " VII " << endl;
  70. break;
  71.  
  72. case 8: cout << " VIII " << endl;
  73. break;
  74.  
  75. case 9: cout << " IX " << endl;
  76. break;
  77.  
  78. case 10: cout << " X " << endl;
  79. break;
  80.  
  81. default: cout << MSG_ERROR << endl;
  82. break;
  83. } break;
  84. }
  85.  
  86. return 0;
  87. }
Success #stdin #stdout 0.01s 5276KB
stdin
8
stdout
Enter a number (1-10): The Roman Numeral is:  VIII