-
case statement wrong?
I guess Dev-cpp/g++ doesn't like my case statement. Any ideas?
Thanks ahead of time.
Code:
#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);
private:
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)
{
switch(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;
thisHanzi.SetRadCat(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";
thisHanzi.DisplayRads(itsRadCat);
cout << "Which of these is the radical you need?\n";
cin >> radChoice;
//thisHanzi.SetRad(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;
//thisHanzi.SetROS(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?
Thanks,
Swaine777
-
Does the switch have a closing } ?
-
You must use {} when declaring things inside case:
Code:
switch(val){
case 1: { int number; }
default...
-
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.
ie:
Code:
void Hanzi::DisplayRads(int r)
{
ifstream infile;
switch(r)
{ case 12: infile.open("rad12.txt"); break;
case 11: infile.open("rad11.txt"); break;
case 10: infile.open("rad10.txt"); break;
case 9: infile.open("rad9.txt"); break;
case 8: infile.open("rad8.txt"); break;
case 7: infile.open("rad7.txt"); break;
case 6: infile.open("rad6.txt"); break;
case 5: infile.open("rad5.txt"); break;
case 4: infile.open("rad4.txt"); break;
case 3: infile.open("rad3.txt"); break;
case 2: infile.open("rad2.txt"); break;
case 1: infile.open("rad1.txt"); break;
default: cout << "Invalid choice!" ; break;
};
char arry[120];
// and so on...
-
But then the whole switch is a bit of an overhead. Maybe something like this would simplify the function.
Code:
#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!)