Changing Filestream object based upon user Input

Hello all,

So I am working on a bit of a personal project, and at a stage where I need to display large bodies of text. Naturally hard coding each of this would be tedious and time consuming so I tried to develop the following code to simply cycle through the various .dat files and pirnt which ever one it was told.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void printMission(short missionNum)
{
  enum Missions{Mission1=1,Mission2,Mission3,Mission4,Mission5}; 
  bool gotIt=false; 
  short endPT; 
  string line;
  switch (missionNum){
         case Mission1: {ifstream filestream(".\\data\\MissionsIndex\\01.dat");gotIt=true;break;}
         case Mission2: {ifstream filestream(".\\data\\MissionsIndex\\02.dat");gotIt=true;break;}
         case Mission3: {ifstream filestream(".\\data\\MissionsIndex\\03.dat");gotIt=true;break;}
         case Mission4: {ifstream filestream(".\\data\\MissionsIndex\\04.dat");gotIt=true;break;}
         case Mission5: {ifstream filestream(".\\data\\MissionsIndex\\05.dat");gotIt=true;break;}
         default: {cout <<" Error: Mission Index not found...\n";gotIt=false;break;}
         }// end switch

         if (gotIt==true){
            filestream >> endPT;
            for (int lineno = 0; getline (testfile,line) && lineno < endPT; lineno++)
                {cout << line << endl;};
         }// end If
};


In case you are wondering this is a game yes. But is very much so in it's infancy. Now on to the error. What I expected it to do was display a different file based upon which case it jumped to. however when I tried to compile, it returns a 'filestream undeclared: first use in the function' error.

To try to correct, I tried declaring it without a specific file name. The compiler went on it's marry way but displayed no output, which should mean that there was either no file attached or it didn't hit the switch cases.

I implanted a few couts inside the cases to check and found that it does in fact
go through that code and to the correct case as well, but doesn't end up redefining 'filestream'.

Any suggestions on how to allow the cycling to actually define filestream so I could print effectively?

Thanks!
-Ty

* Note on 0X.dat format: the first line of the file contains a number which represents the linecount of the file. When used in the for loop it allows the file to be printed in its entirety.
doesn't end up redefining 'filestream'
Nope, it is creating another object (that has the same name and type) but in other scope. When the case ends, that object dies.
You could use ifstream::open() or
1
2
3
4
printMission(short missionNum){
  if( not cat(filename(missionNum)) )
    cerr << "Error: Mission Index not found\n";
}
Thanks ne555! the ifstream:: open was a great help!
But if I could I would love to learn about the second option you wrote. I sadly don't understand how to implement it...

namely the not cat(filename(missionNum)). Where would it go and what would I need to make it print out the various files?

The below is my corrected code using your suggestions, so that anyone else who needs it in the future can see as well.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void printMission(short missionNum)
{  
  ifstream file;   
  enum Missions{Mission1=1,Mission2,Mission3,Mission4,Mission5}; 
  bool gotIt=false; 
  short endPT; 
  string line;
  switch (missionNum){
         case Mission1: {file.open(".\\data\\MissionsIndex\\01.dat",ifstream::in);cout<<"got here";gotIt=true;break;}
         case Mission2: {file.open(".\\data\\MissionsIndex\\02.dat",ifstream::in);cout<<"didn't get here";gotIt=true;break;}
 
         default: {cout <<" Error: Mission Index not found...\n";gotIt=false;break;}
         }// end switch
         if (gotIt==true){
            file >> endPT;
            cout << endPT;
            for (int lineno = 0; getline (file,line) && lineno < endPT; lineno++)
                {cout << line << endl;};
         }// end If
};
cat: concatenate files and print on the standard output
Also if the file does not exists, cat returns false

filename was supposed to be a function that returns the name of the file that you want to open.
AFAICS you are just concatenating the path to a number.dat, but you could also maintain an array/map.
And you could want to handle another directories too.

"Write programs that do one thing and do it well."
Topic archived. No new replies allowed.