我的代碼中有一個switch語句,如果用戶選擇1,他會被要求寫入文件寫入,如果他選擇2;他會被要求寫出要讀取的文件的名稱,但我得到這個錯誤:無法在C++中的switch語句中打開文件
'case_name'的初始化被大小寫標籤忽略。
這裏是我的代碼:
char FileName[100];
char f[100];
int choice;
bool x;
int idd,iddd;
string line;
cout<<"enter file name: "<<endl;
cin>>FileName;
ofstream products_out(FileName,ios::out);
products_out<<table.p<<" "<<table.n<<" "<<table.a<<" "<<table.b<<endl;
while(1){
cout<<"1-add product"<<endl;
cout<<"2-retreive product"<<endl;
cout<<"3-update name"<<endl;
cout<<"5-update cost"<<endl;
cout<<"6-update quantity"<<endl;
cout<<"4-exit"<<endl;
cin>>choice;
switch(choice){
case 1:
cout<<"enter id"<<endl;
cin>>id;
s.ID=id;
cout<<"enter name"<<endl;
cin>>name;
strcpy(s.name,name);
cout<<"enter cost"<<endl;
cin>>cost;
s.cost=cost;
cout<<"enter quantity"<<endl;
cin>>quantity;
s.quantity=quantity;
x=table.insert(s);
products_out<<s.ID<<" "<<s.name<<" "<<s.cost<<" "<<s.quantity<<endl;
cout<<"yes inserted"<<endl;
break;
case 2:
cout<<"enter id of product"<<endl;
cin>>idd;
cout<<"enter the file you want to open"<<endl;
cin>>f;
ifstream products_in(f,ios::in);
products_in.seekg(0, ios::beg);
getline(products_in, line);
if (line.find(id))
{
cout << endl << line;
}
break;
沒問題是你不能在switch語句中聲明一個新變量 – smac89
有趣的是,你向我們展示了不同的代碼。首先,你的代碼中沒有'file_name',你在你所說的警告信息中引用了它。其次,在交換機中不使用'FileName'。你想騙誰? :) – 2013-12-09 22:04:16