Solved

Promblem with switch  structure in C++

Posted on 2007-03-19
8
317 Views
Last Modified: 2008-01-09
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
Comment
Question by:tbeck1982
8 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 43 total points
ID: 18753240
You never call returnArabic() ... is that intentional ?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18753308
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
 

Assisted Solution

by:joexzhu
joexzhu earned 41 total points
ID: 18753575
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 11

Expert Comment

by:DeepuAbrahamK
ID: 18754062
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 18754804
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
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 41 total points
ID: 18754994
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now