跨越字符串的初始化并跳到标签情况

Crosses initialization of string and jump to label case

本文关键字:标签 情况 字符串 初始化 跨越      更新时间:2023-10-16

我正在尝试为程序做案例菜单。而且我总是会遇到交叉初始化的错误,我从未见过此错误。也许有人可以解释我的代码发生了什么问题。

#include <iostream>
#include <cstdlib>
#include <string>
#include <sstream>
#include <stdlib.h>
#include <fstream>
using namespace std;
#define N_CARS 1
struct car{
    string model;
    int year;
    double price;
    bool available;
}cars [N_CARS];

void menu();
void mainMenu();
void writeToFile(ofstream &outputFile , const car& p)
{
    outputFile << p.model << " "
               << p.year << " "
               << p.price << " "
               << p.available<<"n";
}
int choice1 = 0;

int main(int argc, char** argv) {
    menu();
    return 0;
}

void menu() {
    do {
        mainMenu();
        switch(choice1) {
            case 1:
                string mystr;
                string mystr2;
                string mystr3;
                int n;
                for (n=0; n<N_CARS; n++)
                {
                    cout << "Enter title: ";
                    getline (cin,cars[n].model);
                    cout << "Enter year: ";
                    getline (cin,mystr);
                    stringstream(mystr) >> cars[n].year;
                    cout << "Enter price: ";
                    getline (cin,mystr2);
                    stringstream(mystr2) >> cars[n].price;
                    cout << "Choose availability: ";
                    getline (cin,mystr3);
                    stringstream(mystr3) >> cars[n].available;
                }
                ofstream outputFile;
                outputFile.open("bla.txt", fstream::app);
                for (n=0; n<N_CARS; n++)
                    writeToFile(outputFile, cars[n]);
                outputFile.close();
                break;
            case 2:
                break;
            case 5:
                break;
        }
    } while(choice1 != 5);
}

void mainMenu(void) {
    cout << "Main Menun";
    cout << "1 - Enter Carn";
    cout << "5 - Quitn";
    cout << "Please choose: ";
    cin >> choice1;
}

错误:

In function 'void menu()':
    [Error] jump to case label [-fpermissive]
    [Error] crosses initialization of 'std::ofstream outputFile'
    [Error] crosses initialization of 'std::string mystr3'
    [Error] crosses initialization of 'std::string mystr2'
    [Error] crosses initialization of 'std::string mystr'

case标签确实很像可怕的 goto标签;它们不构成新的范围。因此,switch选择正确的case标签要跳到或更糟的是,类似于goto语句跳到标签。不允许跳跃穿越您的各种对象的初始化 - 正如错误消息所说的那样。

最干净的事情是将case 1:break; case 2:之间的所有内容都切成新的函数称为enterCar

void enterCar() {
    // code previously found between the case labels
}
// ...
switch(choice1) {
    case 1: enterCar(); break;
    case 2:
    // ...
}

一个函数构成了一个新范围,可以在其中正确初始化本地对象。作为奖励,您正在将所有意大利面条代码留在您身后。

您必须在 case -blocks中包含变量定义,并带有示波器支架{}。

另一件事:忘记 #define 用于Comple Time常数,使用 constexpr

constexpr size_t NumCars = 5;

还请注意 size_t :对于描述大小的变量和常数,例如阵列大小或索引,使用size_t。

此外,您包括 cstdlib stdlib.h 。C-StandardLibrary的headerFiles始终应包含在C-Prefix和.h-sission中,因此踢出 stdlib.h 。无论如何您都不需要它。