I am trying to create a new variable in a switch, read in some data then push back the variable onto a vector. The only problem is if I try to create a variable in a switch the compiler complains, so is there a way to do this without making the compiler complain?
Example:
Example:
Code:
struct Fruit {
std::string name;
double price;
};
void Read(std::vector<Fruit>& f, std::ifstream& in) {
int type;
in >> type;
switch(type) {
case APPLE:
Fruit apple;
apple.name = "Apple";
in >> apple.price;
f.push_back(apple);
case KIWI:
Fruit kiwi;
kiwi.name = "Kiwi";
in >> kiwi.price;
f.push_back(kiwi);
}
}
Comment