Array and using Characters and not numbers

Hello,

I have an assignment that is similar to one I had before in that I have to convert decimal numbers to roman numeral using an array. When I did it before I had to use switch statement so I thought maybe I can combine the two but I'm getting all kinds of errors. I could use a little bit of nudge in the right direction. Also please note that I know my switch only goes to 10 and I was going to make it go to 20 once I got it to work if this is the way to go. If not I could use any assistance.

Thanks in advance

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
#include <iostream>
 using namespace std;

 int main()
 {
 int const ROMAN_NUM = 21;
 int count[ROMAN_NUM]; 

	count[0] = 0;
	count[1] = 1;
	count[2] = 2;
 	count[3] = 3;
	count[4] = 4;	
	count[5] = 5;
	count[6] = 6;
	count[7] = 7;	
 	count[8] = 8;
 	count[9] = 9;
 	count[10] = 10;
	count[11] = 11; 	
	count[12] = 12; 	
	count[13] = 13; 	
	count[14] = 14 ;	
	count[15] = 15 	;
 	count[16] = 16;
 	count[17] = 17;
 	count[18] = 18;
 	count[19] = 19;
 	count[20] = 20;
 	
 			
 			
 cout << "Enter a whole number between 1 and 20 to convert to roman numerals " ;
 
 cin >> count;
 
 
	 switch (count)
	 {
		 case 1:cout << "Your Roman Numeral equivalent is:  I \n";
		 break;
		 case 2:cout << "Your Roman Numeral equivalent is:  II \n";
		 break;
		 case 3:cout << "Your Roman Numeral equivalent is:  III \n";
		 break;
		 case 4:cout << "Your Roman Numeral equivalent is:  IV \n";
		 break;
		 case 5:cout << "Your Roman Numeral equivalent is:  V \n";
		 break;
		 case 6:cout << "Your Roman Numeral equivalent is:  VI \n";
		 break;
		 case 7:cout << "Your Roman Numeral equivalent is:  VII \n";
		 break;
		 case 8:cout << "Your Roman Numeral equivalent is:  VIII \n";
		 break; 
		 case 9:cout << "Your Roman Numeral equivalent is:  IX \n";
		 break;
		 case 10:cout << "Your Roman Numeral equivalent is:  X \n";
		 break;
		 default: cout << "Your number was not between 1 and 10. Rerun the program to try again.";
	 }
	 
 
 cout << "Your Roman Numeral is: " << ROMAN_NUM << endl;

 return 0;
}  
cin >> count;

Count is an array, so dunno what you're trying to do there. Create a new variable for the user input and the switch statement. The way it looks now, your array is practically useless. It doesnt do anything.
Last edited on
I was trying to pass the value of the count array to the switch so that it would obtain the correct value.

What need to do is have the user enter a number between 1-20 and have it output the equivalent roman numeral so I thought the switch would work. I also tried the array using the roman numerals but it didn't work either.
i.e.
count[0] = I;
count[1] = II;
etc.

Any ideas or guidance is appreciated. Unfortunately my teacher isn't very responsive and "doesn't work weekends".

Thanks
What need to do is have the user enter a number between 1-20 and have it output the equivalent roman numeral so I thought the switch would work.


Well sure, but where does the array come in? If you want to do it with switch statements, sure that works fine, but the array is practically useless.

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
int choice;
cin >> choice;

 switch (choice)
	 {
		 case 1:cout << "Your Roman Numeral equivalent is:  I \n";
		 break;
		 case 2:cout << "Your Roman Numeral equivalent is:  II \n";
		 break;
		 case 3:cout << "Your Roman Numeral equivalent is:  III \n";
		 break;
		 case 4:cout << "Your Roman Numeral equivalent is:  IV \n";
		 break;
		 case 5:cout << "Your Roman Numeral equivalent is:  V \n";
		 break;
		 case 6:cout << "Your Roman Numeral equivalent is:  VI \n";
		 break;
		 case 7:cout << "Your Roman Numeral equivalent is:  VII \n";
		 break;
		 case 8:cout << "Your Roman Numeral equivalent is:  VIII \n";
		 break; 
		 case 9:cout << "Your Roman Numeral equivalent is:  IX \n";
		 break;
		 case 10:cout << "Your Roman Numeral equivalent is:  X \n";
		 break;
	}


This will work perfectly fine.
Last edited on
The switch was from the first assignment and this assignment states specifically that I have to use an array. I was trying to combine them once I couldn't get the array to work using the roman numerals.

Here is the problem:
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.

My original code did not have the switch and I just tried to fill in the array values with the roman numerals but it didn't work.

Now that you know what I'm trying to do any suggestions?
The roman numerals should be stored in an array of
strings


It clearly tells you an array of strings, your is integer. This is how the array should look like.

string arr[20] = {"I", "II", "III", "IV", "V" ...};
WOW. I can't believe I read right over that.

So with doing that how does the user input correlate to the correct item in the array?
Now that you have the array of size 20. The roman letters are placed between 0-19. 0 being the roman letter for 1, and 19 being the last letter so 20. If that was too confusing I'll make a little example.

int arr[3] = {1,2,3};

if you want to access the number 1, you have to do arr[0]. Its stored between 0-2.

So if the user enters a number, say 5, he then wants the roman number for 5. And it is stored in the 4th index of the array, meaning arr[4]. So what you want to do is something like this

1
2
3
4
int x;
cin >> x;

cout << arr[x - 1] << endl;


This will print out the correct one.

I think I am beginning to see the light. I'll give it a try and if I run into any problems I will let you know.

Thank you VERY much for the help!!

Cheers
Thank you very much!! It totally works great and the most important thing is that I actually understand it! LOL

The final code is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 #include <iostream>
 using namespace std;

 int main()
 {
 int x;
 
string ROMAN_NUM[20] = {"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "XI", "XII", "XIII", "XIV", "XV", "XVI", "XVII", "XVIII", "XIX", "XX"};			
 			
 cout << "Enter a whole number between 1 and 20 to convert to roman numerals " ;
 
 cin >> x;
 
 
 cout << "Your Roman Numeral is: " << ROMAN_NUM[x - 1] << endl; 

 return 0;
}
Glad I could help :)
Topic archived. No new replies allowed.