Solved

Promblem with switch  structure in C++

Posted on 2007-03-19
8
316 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
Comment Utility
You never call returnArabic() ... is that intentional ?
0
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 11

Expert Comment

by:DeepuAbrahamK
Comment Utility
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
Comment Utility
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
Comment Utility
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 improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

743 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

11 Experts available now in Live!

Get 1:1 Help Now