Am so stuck in this Question ! c++

i have hw and i dont know how to solve the Question !!


Roman numbers system has the symbols

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

Roman Numbers are formed by combining the above symbols together and adding the values. For example, MMVI is 1000 + 1000 + 5 + 1 = 2006. Generally:
• Symbols are placed in order of value, starting with the largest values.
• When smaller values precede larger values, the smaller values are subtracted from the larger values e.g. IV is 4, IX is 9, and CM is 900.
• When smaller values follow larger values, the smaller values are added to the larger values e.g. VIII is 8, DX is 510, and LXX is 70.
• The symbols I, X, C, and M can be repeated three times in succession, but no more. (They may appear more than three times if they appear non-sequentially, such as XXXIX.)
• The symbols V, L, and D can never be repeated.
• Only one small-value symbol may be subtracted from any large-value symbol according to the following:
o I can be subtracted from V and X only.
o X can be subtracted from L and C only.
o C can be subtracted from D and M only.
o V, L, and D can never be subtracted.

Examples:

MCMXLIV 1000 + (1000 − 100) + (50 − 10) + (5 − 1) 1944
MCMLIV 1000 + (1000 – 100) + 50 + (5 – 1) 1954
MCMXC 1000 + (1000 – 100) + (100 – 10) 1990
LXXVIII 50 + 10 + 10 + 5 + 1 + 1 + 1 78
CCCLXIX 100 + 100 + 100 + 50 + 10 + (10 – 1) 369
MMDCCLI 1000 + 1000 + 500 + 100 + 100 + 50 + 1 2751
DCCCXC 500 + 100 + 100 + 100 + (100 – 10) 890

Write a program that prompts the user to enter a string of symbols representing a Roman number. The program converts that roman number to its equivalent decimal number, and then prints both numbers.

If roman is a variable of type string, then roman[0] stores the first character in the string, roman[1] stores the second character in the string, roman[2] stores the third character in the string, and so on.

Sample input / output:

Enter the Roman Numerial : CCCLXIX
The decimal value of CCCLXIX is : 369

!
Try walking through the conversion with a pencil and paper and see what information you use to make your decisions.

Do you mean a std::string when you say string? (Have you done STL?)

Have you covered error handling?
Yes string
no we didnt cover error handling

i tried to solve it with Array but didnt work with me
How did you try to use an array?
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
    int value[5];
    char str[5];
    int total = 0;
    cout<<"Enter a Roman value: ";
    cin.get(str,5,'\n');
    for(int i = 0; i<strlen(str); i++)
    {
            switch (str[i])
            {
                   case 'I':
                   value[i] = 1;
                   break;
                   case 'V':
                   value[i] = 5;
                   break;
                   case 'X':
                   value[i] = 10;
                   break;
                   case 'L':
                   value[i] = 50;
                   break;
                   case 'C':
                   value[i] = 100;
                   break;
                   case 'D':
                   value[i] = 500;
                   break;
                   case 'M':
                   value[i] = 1000;
                   break;
                   case 'i':
                   value[i] = 1;
                   break;
                   case 'v':
                   value[i] = 5;
                   break;
                   case 'x':
                   value[i] = 10;
                   break;
                   case 'l':
                   value[i] = 50;
                   break;
                   case 'c':
                   value[i] = 100;
                   break;
                   case 'd':
                   value[i] = 500;
                   break;
                   case 'm':
                   value[i] = 1000;
                   break;
                   }
            total = total + value[i];
    }
    for(int i=0; i<(strlen(str)-1); i++)
    {
         if(value[i] < value[i+1])
         total = total - 2 * value[i];
                             

         
     }
    cout << endl << total;
    getch();
    return 0;
}
Topic archived. No new replies allowed.