Hi it's me again.
Well I've done this program where it converts roman numerals to numbers but I've used it with if else.
But I'm supposed to use switch.Code:/* wo0dy at home doing assignment. March 24th 2006 Friday 9am D-48-A Cyberia (5) Write a program that converts a C++ string representing a number in Roman numeral form to decimal form. The symbols used in the Roman numeral system and their equivalents are given below: I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, the following are Roman numbers: XII (12), CII (102), XL (40). The rules for converting a Roman number to a decimal number are as follows: a. Set the value of the decimal number to zero. b. Scan the string containing the Roman character from left to right. If the character is not one of the symbols in the numeral symbol set, the program must print an error message and terminate. Otherwise, continue with the following steps. (Note that there is no equivalent to zero in Roman numerals.) If the next character is the last character, add the value of the current character to the decimal value. If the value of the current character is greater than or equal to the value of the next character, add the value of the current character to the decimal value. If the value of the current character is less than the next character, subtract the value of the current character from the decimal value. Solve this problem using a switch statement. Some sample outputs are given below. */ //It works only for letters that correspond to Roman numerals. I think thats what Sir said it should work. #include <iostream> #include <cstring> using namespace std; int main () { char roman_num[10]; cout << "Enter a Roman Numeral: "; cin >> roman_num; int len = (strlen(roman_num)-1); //I put -1 cos I the lecturer said something about int counter = 0; //the program uses an extra space for memory or something int number = 0; int b4sum = 0; int sum = 0; for (counter = len; counter >= 0; counter--) { if (roman_num[counter] == 'M') number = 1000; else if (roman_num[counter] == 'D') number = 500; else if (roman_num[counter] == 'C') number = 100; else if (roman_num[counter] == 'L') number = 50; else if (roman_num[counter] == 'X') number = 10; else if (roman_num[counter] == 'V') number = 5; else if (roman_num[counter] == 'I') number = 1; else{ number = 0; cout<<"\nOne or more of the inputs entered is invalid. \a"<<endl; } if (b4sum > number) sum = b4sum - number; else sum = sum + number; b4sum = number; } cout << "\nThe Roman Numeral is: " << sum << endl; system ("PAUSE"); return 0; }
I've tried it and read my book but it kept saying that switch quantity is not an integer. The examples from the book were also given using integers.
Can anyone explain me the concept so I can understand how to apply this?



LinkBack URL
About LinkBacks


