Thread: If else works but I can't do it with switch

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    4

    If else works but I can't do it with switch

    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.

    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;
    }
    But I'm supposed to use switch.

    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?

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Show us an attempt. I successfully changed your program to use switch(), so it's possible. (Your program works well, but doesn't handle odd input, ie, "IIIII" gets "5" back.) I think your error is coming from what you're putting inside the switch( ) statement, so check that. A char can be used in a switch(). So, you can use switch(roman_num[counter])...

    About strlen(roman_num) - 1: Say I entered the string "XVII" (roman for 17) strlen() on that will say 4. However, if you say roman_num[4], you won't get "I", you'll get the nul character. This is because in C, all strings are terminated, or end with, and nul character. (It's value is the number 0.) Your string is a character array. And in C, arrays start with index 0, not 1. So, using my example of "XVII", roman_num[0] is 'X', roman_num[1] is 'V', etc. Thus the last, or fourth character is roman_num[3].
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using a character array in a switch question.
    By bajanElf in forum C Programming
    Replies: 10
    Last Post: 11-08-2008, 08:06 AM
  2. Getting the switch statement to work.
    By mtymightymike in forum C Programming
    Replies: 7
    Last Post: 10-15-2008, 06:32 PM
  3. Returning from function - switch selects default
    By rkooij in forum C Programming
    Replies: 24
    Last Post: 03-17-2006, 07:27 AM
  4. Switch Case
    By FromHolland in forum C++ Programming
    Replies: 7
    Last Post: 06-13-2003, 03:51 AM