Solved

Promblem with switch  structure in C++

Posted on 2007-03-19
8
319 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

776 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