Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Promblem with switch  structure in C++

Posted on 2007-03-19
8
320 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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 video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

828 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