Strings, if, else, while loops, Roman Numerals...

I have to make a program that converts numbers into Roman Numerals, but I have to use strings, and while loops. I am very new to programming and I am not sure how to do this exactly. How do I actually use these strings in the program? This is what I have so far, but I don't think it is correct and I don't know how to continue or incorporate strings into my program. (idk if I even set them up correctly here). Any advice would be great! Thanks!

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include<iostream>
#include<climits>
#include<cmath>
#include<cctype>
#include<cstdlib>
#include<string>

using namespace std;

int main(void)

{
    string str1 = "I";
    string str1 = "V";
    string str1 = "X";
    string str1 = "L";
    string str1 = "C";
    string str1 = "D";
    string str1 = "M";
    unsigned short number;

    char yes_no

          
    cout << "\nWould you like to convert numbers into Roman Numerals?";
    cin >> yes_no;
    cin.ignore(INT_MAX, '\n');
    while ( toupper(yes_no) == 'Y' )
        
    {

        cout<< "\n\t Excelent! Let's get started!\n";
        
        cout<< "\n\t Please enter your number:\n";
        
        
        
        
        cin >>ws;
        if ( number >= 4000 || number <= 0 )
        {
            cout << "\nError.  Please enter a number greater than 0 and less than
                    4000.\n";
        
        }
        else
        {
            cout << "Thank you.\n";
        }
        cin >> number;
        
This is very much the same kind of program as getting the number of quarters, dimes, nickels, and pennies in some change.

Each Roman letter has an associated numeric value.

I = 1
V = 5
X = 10
L = 50
C = 100
D = 500
M = 1000

For example, 24 = 0*1000 + 0*500 + 0*100 + 0*50 + 2*10 + 0*5 + 4*1.
Getting rid of zero terms, that's:

4*1 + 2*10

And in Roman numerals, it is:

XXIIII

You now have a valid Roman number. If you wish, you can now do some search and replace on the string:

DCCCC --> CM
CCCC --> CD
LXXXX --> XC
XXXX --> XL
VIIII --> IX
IIII --> IV

The order in which you do that is important. Starting at the top and working our way down, the only match we find is IIII-->IV.

XXIIII --> XXIV

Tada!



To implement this, you'll need a lookup array for values and an associated Roman letter array:

1
2
int values[] = { 1000, 500, 100, 50, 10, 5, 1 };
char letters[] = { 'M', 'D', 'C', 'L', 'X', 'V', 'I' };

You'll also want to have a string where you can put the letters you get from the number:

1
2
char roman[100] = {0};
int count = 0;

(To add a letter, append it to the string: roman[count++] = a roman letter;.)

You now have everything you need to write a loop to decompose your number into letters.

Hope this helps.
I understand everything up to the "You'll also want to have a string where you can put the letters you get from the number" and the code that follows to the end. This is my first time using strings and while loops. Can you explain what exactly is happening there?
@OP, @Duoas is suggesting for you to use a c_string, which is basically C's old way of creating a string. Think about it. A character is a letter or what have you. A string is a collection of characters. Aka an array of characters.

If you're in any way familiar with arrays or vectors, you can call similarly to c_strings in that you can tell your program to access certain places within the string (like the .size() function, also found in <array> and <vector>). Let's say I have a string "stuff" and I wanted to insert a random character, say...'5'. I can just insert() into whatever position I want.

http://www.cplusplus.com/reference/string/string/insert/
http://www.cplusplus.com/reference/string/string/size/
http://www.cplusplus.com/reference/cstring/?kw=cstring
Topic archived. No new replies allowed.