• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 326
  • Last Modified:

Promblem with switch structure in C++

I am having a promblem with a case statement. I am trying to pass an array of letters to this switch statement so that I can assign calues to it. Whenever I try to call the function that has the switch statement
it does nothing and theprogram ends. I would apperciate any advice that can be offered, Below is my code.
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;


void returnRoman(char roman[]);      
void returnArabic(char roman[], int arabicNum);
void checkInput(char roman[]);

class romanType
{
      // 0 < num < 3999
public:
      void returnRoman(char roman[16]);                              //Returns a roman numeral
      void returnArabic(char roman[16], int arabicNum);      //Returns a arabic number
      void checkInput(char roman[16]);                              //Checks to see if the roman numeral is in the proper
                                                                                    //form

private:
      int arabicNum; //The variable that stores the arabic number


}; //class romanType

void main()
{
      int index      = 0;
      int arabicNum = 0;
      char roman[16];
      romanType romanNumeral;

      for(index = 0; index<16; index++)
            {
                  roman[index] = 0;
            }
      
      
      cout << "Please enter a roman numeral that you would like to convert." << endl;
      cout << "Make sure that the numerals are in uper-case only." << endl;
      cout << "Enter in the upper-case 'Q' when finished entering numerals." << endl;
      
index = -1;      

      while(roman[index] != 'Q')
            {
                  index++;
                  cin.get(roman[index]);
                  
            }      
      
      
      //romanNumeral.returnArabic(roman, arabicNum);
            romanNumeral.returnRoman(roman);
      
      

      return;
}//function main

void romanType::checkInput(char roman[])
{
      


      return;
}//function checkInput

void romanType::returnRoman(char roman[])
{
      int index = 0;
      
            for(index = 0; index<16; index++)
      {
            cout << roman[index];
      }

      return;
}//function returnRoman

void romanType::returnArabic(char roman[], int arabicNum)
{
      int index = 0;
      int value = 0;
      
      
while(roman[index] != 'Q')
{
      switch (roman[index])
      {
                          case 'M': case 'm':
                              value = 1000;
                                          break;
                  case 'D': case 'd':
                              value = 500;
                                          break;
                  case 'C': case 'c':
                              value = 100;
                                          break;
                  case 'L': case 'l':
                              value = 50;
                            break;
                  case 'X': case 'x':
                              value = 10;
                                          break;
                  case 'V': case 'v':
                              value = 5;
                                          break;
                  case 'I': case 'i':
                              value = 1;
                                          break;
                         
                        arabicNum = value + arabicNum;
                        index++;
      
      }//end of switch structure
}//end of while loop
      

cout << arabicNum << endl;
      return;
}//function returnArabic

0
tbeck1982
Asked:
tbeck1982
3 Solutions
 
Infinity08Commented:
You never call returnArabic() ... is that intentional ?
0
 
Infinity08Commented:
A few more things :

1) main must return int :

        int main() {
            // ...

            return 0;
        }

2) These three function prototypes before the class definition shouldn't be there :

        void returnRoman(char roman[]);      
        void returnArabic(char roman[], int arabicNum);
        void checkInput(char roman[]);

    You can just remove them.

3) Taking a value outside of the array bounds is dangerous. For the first iteration of the following loop, you're taking the character just before the array :

        index = -1;      

        while(roman[index] != 'Q')

    Change the loop like this instead :

        index = -1;

        do {
              index++;
              cin.get(roman[index]);
        } while(roman[index] != 'Q');

3) For the arabicNum() method, you don't need to pass arabicNum like you do :

        void romanType::returnArabic(char roman[], int arabicNum)

    Use this method instead :

        void romanType::returnArabic(char roman[]) {
            // ... your code - it will use the arabicNum inside the class here !!
        }


This just after quickly scanning your code. Make the suggested modifications, and see if that helped.
0
 
joexzhuCommented:
The following two statements are never executed, regardless
of the value of roman[index].

arabicNum = value + arabicNum;
index++;

You should move them out of the switch. That is, place them
after the closing curly bracket of the switch statement:
 }//end of switch structure

Did you end up with infinite loop? Try what I said, and see if it workss
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Deepu AbrahamR & D Engineering ManagerCommented:
The while loop & switch should be like this: other wise it never incremented the index and the amount

while(roman[index] != 'Q')
{
      switch (roman[index])
      {
      case 'M': case 'm':
            value = 1000;
            arabicNum = value + arabicNum;
            index++;
            break;
      case 'D': case 'd':
            value = 500;
            arabicNum = value + arabicNum;
            index++;
            break;
      case 'C': case 'c':
            value = 100;
            arabicNum = value + arabicNum;
            index++;
            break;
      case 'L': case 'l':
            value = 50;
            arabicNum = value + arabicNum;
            index++;
            break;
      case 'X': case 'x':
            value = 10;
            arabicNum = value + arabicNum;
            index++;
            break;
      case 'V': case 'v':
            value = 5;
            arabicNum = value + arabicNum;
            index++;
            break;
      case 'I': case 'i':
            value = 1;
            arabicNum = value + arabicNum;
            index++;
            break;
            
            
            
      }//end of switch structure
}//end of while loop

Best Regards,
DeepuAbrahamK
0
 
Infinity08Commented:
DeepuAbrahamK, that's a bit overkill, isn't it ? You could have suggested something like this :

while(roman[index] != 'Q')
{
      switch (roman[index])
      {
      case 'M': case 'm':
            arabicNum += 1000;
            break;
      case 'D': case 'd':
            arabicNum += 500;
            break;
      case 'C': case 'c':
            arabicNum += 100;
            break;
      case 'L': case 'l':
            arabicNum += 50;
            break;
      case 'X': case 'x':
            arabicNum += 10;
            break;
      case 'V': case 'v':
            arabicNum += 5;
            break;
      case 'I': case 'i':
            arabicNum += 1;
            break;
      }//end of switch structure
      ++index;
}//end of while loop
0
 
itsmeandnobodyelseCommented:
Some more remarks:

A roman number may be subtracted rather than added if it is left of a higher letter, e. g.   XC = 90 or  IX == 9. It's always one letter only where that could happen.

A solution without checking valid ordering of roman numbers is (tested):

int romanNumber(const char* szRoman)
{
     int    arabic = 0;
     for (int i = 0; i < strlen(szRoman); ++i)
     {
           char cc = toupper(szRoman[i]);
           char cn = toupper(szRoman[i+1]);
           switch(cc)
           {
           case 'M': arabic += 1000; break;
           case 'D': arabic += 500; break;
           case 'C': arabic += (cn == 'M' || cn == 'D')? -100 : +100;    break;
           case 'L': arabic += 50; break;
           case 'X': arabic += (cn == 'C' || cn == 'L')? -10 : +10;  break;
           case 'V': arabic += 5; break;
           case 'I': arabic += (cn == 'X' || cn == 'V')? -1 : +1; break;
           default: return -1;  // error
           }
     }
     return arabic;
}

The test client is:

#include <string>
#include <iostream>
using namespace std;

int main()
{
    std::string roman;
     while (true)
     {
         cout << "Enter roman ==> ";
         if (!(cin >> roman))
             break;
         if (roman == "q" || roman.empty())
             break;
         cout << roman << " = " << romanNumber(roman.c_str()) << endl;
     }
     return 0;
}

Regards, Alex
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now