Looping Confusion

closed account (3voN6Up4)
"The program should have a loop that allows the user to continue entering numbers until an end sentinel of 0 is entered." (From the comment block)

I attempted to do it on line 16 with the do loop. I'm unsure of how I should attempt this.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/*Write a program that displays the roman numeral equivalent of any decimal number
between 1 and 20 that the user enters. The roman numerals should be stored in an array of
strings and the decimal number that the user enters should be used to locate the array element
holding the roman numeral equivalent. The program should have a loop that allows
the user to continue entering numbers until an end sentinel of 0 is entered.
Input validation: Do not accept scores less than 0 or greater than 20.*/
#include <iostream>
#include <string>
using namespace std;
int main()
{
    int user_input;
    string ROMAN_NUM[20] = {"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X",
    "XI", "XII", "XIII", "XIV", "XV", "XVI", "XVII", "XVIII", "XIX", "XX"};

    do
    {
        cout << "Enter a number between 1-20. I will convert it to roman numerals." << endl;
        cin >> user_input;
        while (user_input < 1 || user_input > 20)
            {
                cout << "BETWEEN 1-20." << endl;
                cout << "Enter a number between 1-20. I will convert it to roman numerals." << endl;
            }
        cout << "Roman numeral form of " << user_input << " is: " << ROMAN_NUM[user_input - 1] << endl;
        //"- 1" because the array pulls from 0 first, and the lowest number the user can enter is 1.
        return 0;
    }
}
Last edited on
To give you an idea:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <string>


int main()
{
    int user_input{ 0 };
    std::string ROMAN_NUM[20] = { "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X",
	   "XI", "XII", "XIII", "XIV", "XV", "XVI", "XVII", "XVIII", "XIX", "XX" };

    do
    {
	   std::cout << "Enter a number between 1-20. I will convert it to roman numerals." << std::endl;
	   std::cin >> user_input;

	   while (user_input < 0 || user_input > 20)
	   {
		  std::cout << "BETWEEN 1-20." << std::endl;
		  std::cout << "Enter a number between 1-20. I will convert it to roman numerals." << std::endl;
		  std::cin >> user_input;
	   }

	   if (user_input == 0) // If the user enters 0; then return to the OS to end the program.
		  return 0;
	   else
	   std::cout << "Roman numeral form of " << user_input << " is: " << ROMAN_NUM[user_input - 1] << std::endl;

    } while (user_input != 0);

    return 0;
}
closed account (3voN6Up4)
Thanks so much, I also have learned a new thing today. The "!".
Topic archived. No new replies allowed.