我试图让我的程序循环遍历文件,每次都接收大量信息。但是,在正确输入 2 行之后,无论文件的内容是什么,它总是默认为默认值。最初它是一个 eof while 循环,但我将其更改为一个 for 循环来尝试修复它。这是我的代码:
ifstream furniture;
furniture.open("h://furniture.txt");
for(int i=0;i<=count;i++)
{
type=0;
furniture>>type>>name>>number>>material>>colour>>mattress;
switch (type)
{
case 1:
{
Item* item= new Bed(number, name, material, colour, mattress);
cout<<"working, new bed"<<endl;
v.push_back(item);
cout<<"working pushback"<<endl;
count++;
break;
}
case 2:
{
Item* item= new Sofa(number, name, material, colour);
cout<<"working, new sofa"<<endl;
v.push_back (item);
cout<<"working pushback"<<endl;
count++;
break;
}
case 3:
{
Item* item= new Table(number, name, material, colour);
cout<<"working, new table"<<endl;
v.push_back(item);
cout<<"working pushback"<<endl;
count++;
break;
}
default:
{
cout<<"Invalid input"<<endl;
type=0;
break;
}
}
}
我尝试了一系列不同的解决方案,但似乎都没有解决问题。任何帮助将不胜感激。