处理交换机中的文件C ++:交叉初始化错误

时间:2014-05-27 00:10:46

标签: c++

我试图允许用户在选择任何2个案例选项后能够读取文件。但它给了我一个错误,说“跨越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;
    }
}

3 个答案:

答案 0 :(得分:0)

您需要将案例陈述括在大括号中:

case 1:
{
    //your code
}

或在您的案例中没有定义新的varibales:

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

原因是switch语句的case部分没有定义范围。因此,编译器不知道您跳转到case语句不会跳过变量的减速。

答案 1 :(得分:0)

您可以执行以下操作来实现此功能

// 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;
}

或者,以下代码也可以使用

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;
// ...
}

关键是,您不能在switch子句的case语句范围内定义任何多个不同的变量声明。您需要在switch语句之外定义变量,或在case块添加其他范围以定义内部变量。

<子> 看起来您的代码中存在许多其他严重缺陷,这些解释超出了这个实际问题!

答案 2 :(得分:0)

完整的错误消息应为

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

快速解决方法是在每个case子句的主体周围添加一对花括号。也就是说,

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

原因如下。如果不添加大括号,变量staffIDO(和studID)的范围超出了case子句,并且可以在没有初始化的情况下访问(当不执行case子句时)。