I recommend bundling these three variables into a single data type.
Lets call it "DrinkT".
struct DrinkT{
std::string name;
float cost;
unsigned int quantity;
DrinkT(std::string const& nameIn, float const& costIn, unsigned int const& quantityIn):
name(nameIn),
cost(costIn),
quantity(quantityIn)
{}
};
This has many advantages.
You will now be able to create drinks like this:
DrinkT coke("Coca-Cola",0.75,20);
and access the variables like this:
std::cout << coke.name << std::endl; //outputs: Coca-Cola
std::cout << coke.cost << std::endl; //outputs: 0.75
std::cout << coke.quantity << std::endl; //outputs: 20
A drink object will only hold what you have specified: a drink name, a drink cost, and a drink quantity.
The object has a constructor that takes all three of these values on construction.
This means, you can only create a drink object if you specify all of it's values at the same time.
If your going to be storing a lot of drink objects(and you may not know how many), we may also want to put them into some kind of container.
A vector is a good choice. A vector will grow to account for as many drinks as you want to store.
Let's loop through the file, read in the three values individually, and store them in our vector. We will do this again and again for each drink until we reach the end of the file.
int main(){
std::ifstream infile("file.txt");
std::vector<DrinkT> drinks;
std::string name;
std::string cost;
std::string quantity;
std::getline(infile,name,',');
std::getline(infile,cost,',');
std::getline(infile,quantity,' ');
while (infile){
drinks.push_back(DrinkT(name,atof(cost.c_str()),atoi(quantity.c_str())));
std::getline(infile,name,',');
std::getline(infile,cost,',');
std::getline(infile,quantity,' ');
}
//output
for(DrinkT drink : drinks){
std::cout << drink.name << " " << drink.cost << " " << drink.quantity << std::endl;
}
return EXIT_SUCCESS;
}
compiled with g++ -std=c++0x -o main main.cpp
information on some of the language features used:
http://www.cplusplus.com/reference/string/getline/
http://www.cplusplus.com/reference/stl/vector/