Hi guys, im still learning some programming, and am confused on the subject of file i/o. I am getting this error but cant figure out why. Any help is appreciated, thanks

Keep in mind i have obviously not finished the case scenarios.

In function `int main()':
error: jump to case label
error: crosses initialization of `std::ofstream cList'
error: jump to case label
error: crosses initialization of `std::ofstream cList'
error: `repeat' undeclared (first use this function)
error: (Each undeclared identifier is reported only once for each function it appears in.)
warning: destructor needed for `cList'
warning: where case label appears here
warning: (enclose actions of previous case statements requiring destructors in their own scope.)
warning: destructor needed for `cList'
warning: where case label appears here

Execution terminated

#include <iostream>
#include <fstream>
#include <windows.h>
#include <string>

using namespace std;

string catEnter;
string itEnter;


int main()
{
    SetConsoleTitle("ItemIzer");
    
    cout<<"\n\n\n\n";
    cout<<"\t1: View Catagory"<<endl;
    cout<<"\t2: Append new item"<<endl;
    cout<<"\t3: Delete an item"<<endl;
    cout<<"\t4: Create Catagory"<<endl;
    cout<<"\t5: Delete Catagory"<<endl;
    
    do
    {
              int cho;  
              bool repeat(false);
              cout<<">>";
              cin>>cho;
              
              
              switch (cho)
              {
                     case 1:
                          
                     case 2:
                          
                     case 3:
                          
                     case 4:
                          ofstream cList;
                          cout<<"Catagory Name>>";
                          cin>>catEnter;
                          cList.open ("Catagories.txt", ios::app);
                          cList<<catEnter;
                          cList.close();
                          cin.get();
                          break;
                     
                     case 5:
                          
                     default:
                             cout<<"Bad user input, please select a menu option";
                             repeat = true;
                             }
                             }
                     while (repeat);
                     }

Recommended Answers

All 5 Replies

As has been repeated many times in the last few days.
You cannot declare anything in the outside scope of a case statement.
you need either to put cList outside of the switch or use something like this

case 4:
   {
      ofstream cList;
      // ..... stuff
    }
   break;
 case 5:

Also please put break; after each case.

But didn't i do it how you just showed me in your example?

case 4:
    ofstream cList;
    cout<<"Catagory Name>>";
    cin>>catEnter;
    cList.open ("Catagories.txt", ios::app);
    cList<<catEnter;
    cList.close();
    cin.get();
    break;

Sorry for not fully understanding what is going on, im am still in the current progress of trying to comprehend fstream and how to properly use it.

Edit: Ok, i see what you did, using { and }. But can you inform me why that worked now? I want to be able to understand what im writing instead of copy and pasting. Thanks!

commented: Wanting to understand not just do +1

But can you inform me why that worked now? I want to be able to understand what im writing instead of copy and pasting. Thanks!

Ok, well you have gone up in my estimation. So let me see what I can do to answer the question. [feel free to ask for clarifications/questions]

The C++ standard says that you cannot jump over an initialization statement (implicit or explicit). [ISO C++ 2003 6.7]

Interestingly, this means you cannot write

// This code will fail to compile
goto label;
int a=4;       // NOT OK.
int x;            // OK 
label:
int b=10;
// .. more stuff

The switch statement is such a code block (effectively). As I understand the reason, it is because the case points are actually effective labels. The worst example of that I have seen is Duff's device. It is on line everywhere and it is in the C++ book (as a question!!). . But is uses a do { } while block with case statements within the do while!.

This is a start on a long road of strangeness. C++ has a strong tension between compiler writers / low-level access / high order coding. It is places like this that it comes to the fore.

commented: reps for analysis, and mentioning Duff :) +23

Thank you for helping me out, the explanation is a little cloudy to me simply because of the vocabulary that i am still slightly unfamiliar with, but i think i can decipher it. Thanks!

//...
bool repeat=false;
do{
   //...
   switch(cho){
   default:
      //...Bad input
      repeat=true;}
}while(repeat);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.