0

I am trying to allow a user to be able to read to a file once they selected any of the 2 case options. but it is giving me an error that says, "crosses initialization of std::ofstream"

#include <cstdlib>
#include<iostream>
#include<fstream>
#include<string>
#include<stdlib.h>
using namespace std;

main() 
{
    cout<<"Welcome to Blah High"<<endl;
    string stud_passcode,staff_passcode;
    int studdID,stafID;
    char memopt; 

    cout<<"If you are a member of the student body select 1"<<endl; 
    cout<<"If you are a member of staff enter 2"<<endl;
    cin>>memopt;


    switch(memopt)
    {

        case 1:
        ofstream staffIDO("StaffID.txt");                                                              

        cout<<"Enter staff ID number"<<endl;
        cout<<"Press Ctrl+Z to exit"<<endl;                                                      

        while(cin >> stafID)
        {
            staffIDO<<stafID<<endl;
        }
        break;

    case 2:
    ofstream studID("student.txt"); 

    cout<<"Enter student ID number"<<endl;
    cout<<"Press Ctrl+Z to exit"<<endl;                                                      

    while(cin >> studdID)
    {
        studID<<studdID<<endl;
    }
    break;

    Default:
    cout<<"Invalid option"<<endl;
    }
}
Appleshell
  • 7,088
  • 6
  • 47
  • 96
user3677923
  • 9
  • 1
  • 1

3 Answers3

1

You need to enclose your case statements in braces:

case 1:
{
    //your code
}

or define no new varibales in your cases:

int var;
switch var_other
{
    case 1:
       //do stuff with var
}

The reason for this is that the case part of your switch statement does not define a scope. Therefore the compiler does not know that your jump into the case statements does not skip the deceleration of a variable.

Fantastic Mr Fox
  • 32,495
  • 27
  • 95
  • 175
1

You can do the following to get this working

// Declare the ofstream variables you need outside the switch,
// and use open() to open the conditionally determined files actually.
ofstream staffIDO;
ofstream studID;

switch(memopt) {

case 1: 
    staffIDO.open("StaffID.txt");                                                              
    cout<<"Enter staff ID number"<<endl;
    cout<<"Press Ctrl+Z to exit"<<endl;                                                      

    while(cin >> stafID) {
        staffIDO<<stafID<<endl;
    }
    break;

case 2:
    studID.open("student.txt"); 

    cout<<"Enter student ID number"<<endl;
    cout<<"Press Ctrl+Z to exit"<<endl;                                                      

    while(cin >> studdID){
        studID<<studdID<<endl;
    }
    break;

default:
    cout<<"Invalid option"<<endl;
    break;
}

Alternatively the following code might also work

switch(memopt) {
case 1: {
    ofstream staffIDO("StaffID.txt");                                                              
    cout<<"Enter staff ID number"<<endl;
    cout<<"Press Ctrl+Z to exit"<<endl;                                                      

    while(cin >> stafID) {
        staffIDO<<stafID<<endl;
    }
} break;

case 2: {
    ofstream studID("student.txt"); 

    cout<<"Enter student ID number"<<endl;
    cout<<"Press Ctrl+Z to exit"<<endl;                                                      

    while(cin >> studdID){
        studID<<studdID<<endl;
    }
} break;
// ...
}

The point is, you cannot define any multiple distinct variable declarations in the scope of a switch statement within the case clauses. You'll need to define your variables either outside the switch statement, or add an additional scope at the case block to define the variable inside.

Looks like you have a number of other serious flaws in your code, which explanation goes way beyond this actual question!

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
0

The full error message should be

jump to case label [-fpermissive] crosses initialization of std::ofstream.

A quick fix is to add a pair of curly braces around the body of each case clause. That is,

switch(memopt)
{
    case 1:
    {
       ...
       break;
    }
    case 2:
    {
       ...
       break;
    }
    default:
       break;
}

The reason is the following. If you don't add the braces, the scope of the variable staffIDO (and studID), extends beyond the case clause, and might be accessed without initialization (when the case clause is not executed).

Danke Xie
  • 1,757
  • 17
  • 13