Thread: case statement wrong?

  1. #1
    Registered User
    Join Date
    Mar 2003

    Question case statement wrong?

    I guess Dev-cpp/g++ doesn't like my case statement. Any ideas?

    Thanks ahead of time.

    #include <iostream>
    #include <cstdlib>
    #include <fstream>
    using namespace std;
    class Hanzi
    { public:
        Hanzi() { };
        Hanzi(const Hanzi &) { };
        ~Hanzi() { };
        int GetRadCat() { return itsRadCat; };
        void SetRadCat(int rc) { itsRadCat = rc - 1; }; //1 subtracted for use in array
        void SetRad(int rc) { };
        void GetRad(int rc) { };
        void SetROS(int theRest) { ros = theRest; }; // ros is rest of strokes
        void DisplayRads(int r);                    // r is radical category
        void DisplayRads(int radcat, int roStr);
        int itsRadCat,
            nrs,            // number of radical strokes
            radChoice,      // which radical you choose
            ros;            // "rest of strokes"...number of strokes in the rest of the char 
    void Hanzi::DisplayRads(int r)
      { case 12: ifstream infile("rad12.txt"); break;
        case 11: ifstream infile("rad11.txt"); break;
        case 10: ifstream infile("rad10.txt"); break;
        case  9: ifstream infile("rad9.txt"); break;
        case  8: ifstream infile("rad8.txt"); break;
        case  7: ifstream infile("rad7.txt"); break;
        case  6: ifstream infile("rad6.txt"); break;
        case  5: ifstream infile("rad5.txt"); break;
        case  4: ifstream infile("rad4.txt"); break;
        case  3: ifstream infile("rad3.txt"); break;
        case  2: ifstream infile("rad2.txt"); break;
        case  1: ifstream infile("rad1.txt"); break;
        default: cout << "Invalid choice!" ; break;  
      char arry[120]; 
      for(int a=0; a<120; a++)
      { infile.getline(arry[a], 120, '\n');
      for(int b=0; b<120; b++)
      { cout << "\t[" << b+1 << "] " << arry[b] << endl;
    int main()
    { Hanzi thisHanzi;
      cout << "How many strokes are there in the radical? ";
      cin >> nrs;
      cout << "The radical category is " << thisHanzi.GetRadCat() << "\n";
      cout << "Here are the radicals in radical category " 
           << thisHanzi.GetRadCat() << ". 1 subtracted for use with array.\n";
      cout << "Which of these is the radical you need?\n";
      cin >> radChoice;
      //cout << "You chose this radical: " << thisHanzi.GetRad(radChoice);
      cout << "How many strokes are there in the rest of the character? ";
      int theRest;
      cin >> theRest;
      cout << "Here are the characters with that radical and that number of strokes remaining: \n";
      //thisHanzi.DisplayRads(radChoice, ros);

    Compiler: Default compiler
    Executing g++.exe...
    g++.exe "D:\HanziClass.cpp" -o "D:\HanziClass.exe" -I"C:\DEVC\include" -I"C:\DEVC\include" -L"C:\DEVC\lib"

    D:\HanziClass.cpp: In method `void Hanzi::DisplayRads(int)':
    D:\HanziClass.cpp:30: case label `11' within scope of cleanup or variable array
    D:\HanziClass.cpp:29: warning: destructor needed for `class ifstream infile'
    D:\HanziClass.cpp:30: warning: where case label appears here

    Read something in one of my C++ books about not using a floating point somethin or other in a case statement. Can anyone clarify that for me?



  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Does the switch have a closing } ?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    You must use {} when declaring things inside case:

       case 1: { int number; }
    Nothing more to tell about me...
    Happy day =)

  4. #4
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    The compiler treats labels in switch case statements like goto labels. Declaring variables in switch() statements will generally mess everything up. You can surround these variables with brackets {} to make them go out of scope before the next label is hit.

    However, for your case, your variable needs to last longer than the switch statement will allow it. Declare it right before the switch statement, and use the open("file") function to open the file.

    void Hanzi::DisplayRads(int r)
      ifstream infile;
      { case 12:"rad12.txt"); break;
        case 11:"rad11.txt"); break;
        case 10:"rad10.txt"); break;
        case  9:"rad9.txt"); break;
        case  8:"rad8.txt"); break;
        case  7:"rad7.txt"); break;
        case  6:"rad6.txt"); break;
        case  5:"rad5.txt"); break;
        case  4:"rad4.txt"); break;
        case  3:"rad3.txt"); break;
        case  2:"rad2.txt"); break;
        case  1:"rad1.txt"); break;
        default: cout << "Invalid choice!" ; break;  
      char arry[120]; 
    // and so on...

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    But then the whole switch is a bit of an overhead. Maybe something like this would simplify the function.
      #include <iostream>
      #include <sstream>
      ostringstream Name;
      int i = 11;
      Name <<"rad" <<i <<".txt" <<flush;
      std::cout <<"Name is " <<Name.str() <<std::endl;
    // Output:
    Name is rad11.txt
    (just a thought!)
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. switch case statement
    By stanlvw in forum C++ Programming
    Replies: 3
    Last Post: 02-26-2008, 04:06 AM
  2. Strings and Switch Statement
    By Ajsan in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2004, 01:16 PM
  3. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 06:48 PM
  4. Printing weekday
    By sworc66 in forum C Programming
    Replies: 12
    Last Post: 09-13-2002, 07:03 AM