Switch case, with ofstream <- noob

This is a discussion on Switch case, with ofstream <- noob within the C++ Programming forums, part of the General Programming Boards category; yesterday i posted a similar program,, but it was very confusing with many nested if statements, i had to many ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    266

    Switch case, with ofstream <- noob

    yesterday i posted a similar program,, but it was very confusing with many nested if statements, i had to many problems with it
    im trying to use a switch statement now..but i get an error -
    stuff like this, i get about 9 errors -
    Code:
    initialization of 'a_file' is skipped by 'case' label
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    int main()
    {
    	int cho;
    	string off = "working";
    	int toggle = 0;
    	string str;
    	string filename;
    	cout << "whats the file? include extenstion \n";
    	cin >> filename;
    	cin.ignore();
    	do{
    	cout << "\nwill you be editing or creating new file/starting over? \n 1.Edit \n 2.NEW \n\n";
        cin >> cho;
    	cin.ignore();
    	switch ( cho )
    	{
    		case 1:
    			ofstream a_file(filename.c_str(), ios::app);
    			break;
    		case 2:
    			ofstream b_file(filename.c_str());
    			break;
    		default:
    			cout << "\nERROR , wrong input \n";
    			break;
    	}
    	}while((cho != 1) || (cho != 2)); //confused here, is it && or || i need?
    
    
    
    							if(cho == 1)
    							{		
    								if(!a_file.is_open())
    								{
    								cout << "\nFile unopenable";
    								cin.get();
    								}
    								else if(b_file.is_open()){
    								toggle =  1;
    								cout << "\nNEW is on";
    								}
    								else{
    									off = "off";
    								}
    							}
    						if(cho == 2)
    							{		
    								if(!b_file.is_open())
    								{
    								cout << "\nFile unopenable";
    								cin.get();
    								}
    								else if(a_file.is_open()){
    								toggle =  1;
    								cout << "\nEditing is on";
    								}
    								else{
    									off = "off";
    								}
    							}	
    
    	cout << " \n What do you want to enter in the file?" << endl;
    	getline (cin,str);
    	cin.ignore();
    
    
    		if (cho == 2){
    		b_file<< str;
    	}
    	else
    	{
    		a_file << "\n" << str;
    	}
    	cin.get();
    return 0;
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Need to work on formatting a bit, there's a reason for the "preview" button.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    int main()
    {
        int cho;
        string off = "working";
        int toggle = 0;
        string str;
        string filename;
        cout << "whats the file? include extenstion \n";
        cin >> filename;
        cin.ignore();
        do
        {
            cout << "\nwill you be editing or creating new file/starting over? \n 1.Edit \n 2.NEW \n\n";
            cin >> cho;
            cin.ignore();
            switch ( cho )
            {
            case 1:
                ofstream a_file(filename.c_str(), ios::app);
                break;
            case 2:
                ofstream b_file(filename.c_str());
                break;
            default:
                cout << "\nERROR , wrong input \n";
                break;
            }
        }while((cho != 1) || (cho != 2)); //confused here, is it && or || i need?
    
        if(cho == 1)
        {		
            if(!a_file.is_open())
            {
                cout << "\nFile unopenable";
                cin.get();
            }
            else if(b_file.is_open())
            {
                toggle =  1;
                cout << "\nNEW is on";
            }
            else
            {
                off = "off";
            }
        }
        
        if(cho == 2)
        {		
            if(!b_file.is_open())
            {
                cout << "\nFile unopenable";
                cin.get();
            }
            else if(a_file.is_open())
            {
                toggle =  1;
                cout << "\nEditing is on";
            }
            else
            {
                off = "off";
            }
        }	
    
        cout << " \n What do you want to enter in the file?" << endl;
        getline (cin,str);
        cin.ignore();
    
    
        if (cho == 2)
        {
            b_file<< str;
        }
        else
        {
            a_file << "\n" << str;
        }
        cin.get();
        return 0;
    }
    The "initialization of 'a_file' is skipped by 'case' label" error is caused by your code I've highlighted in red above. Depending on what choice the user makes, there may or may not be an instatiation of a particular ofstream object and this is what the compiler is complaining about. To fix that, you should rework things a bit so that the declaration of the a_file and b_file variable have scope within the entire function and not just the block of code they are currently in, the only thing you should be doing in the switch statement is opening the files.

    Code:
    int main()
    {
        ...
    
        ofstream a_file, b_file;
    
        ...
    
        do
        {
            ...
            switch ( cho )
            {
            case 1:
                a_file.open(filename.c_str(), ios::app);
                break;
            case 2:
                b_file.open(filename.c_str());
                break;
            default:
                cout << "\nERROR , wrong input \n";
                break;
            }
        } while( ...
    Code:
    while((cho != 1) || (cho != 2)); //confused here, is it && or || i need?
    I think you want "&&" and not "||" there.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You cannot declare a variable like that in a switch-statement.
    ok you could like that
    Code:
    	switch ( cho ) 	{
    		case 1: {
    			    ofstream a_file(filename.c_str(), ios::app);
    			    break;
                            }
    		case 2: { 
    			     ofstream b_file(filename.c_str());
    			     break;
                            }
    		default:
    			cout << "\nERROR , wrong input \n";
    			break;
    	}
    But that woud not help much because the variables would get out of scope after the break statement.
    I think you don't need that switch statement at all. just do it like this :

    Code:
       ofstream ofile;
       if( cho == 1 ) {
          ofile.open(filename.c_str(), ios::app );
       }
       else if ( cho == 2 ) {
          ofile.open(filename.c_str() );
       }
       cout << " \n What do you want to enter in the file?" << endl;
       getline (cin,str);
       cin.ignore();
       if (cho == 2){
    	ofile << str;
       }
       else {
          ofile << "\n";
       }
    BTW it has to be
    Code:
    	}while((cho != 1) && (cho != 2));
    Kurt

    oops . beeing late reformatting that mess.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    yea i got it,

    lets say i want to add another ofstream, read_file
    my do while statement,,,in my case - what would it need to be?
    something like this?,, or not?
    Code:
    do{
    ....
    }while((cho != 1) && (cho != 2) && (cho !=3));

  5. #5
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Hey come on. Why don't you just type that in your editor and run it. Would save you a lot of time.
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. noob question re: switch case & loops
    By scooglygoogly in forum C++ Programming
    Replies: 13
    Last Post: 08-25-2008, 11:08 AM
  4. Keypress reading
    By geek@02 in forum Windows Programming
    Replies: 1
    Last Post: 06-16-2004, 12:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21