Reputation: 55
Hey guys this code is for a coffee machine. I am trying to add a operator mode for this machine.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
struct coffee {
string name;
int itemprice;
string country;
int quantity;
};
float remainder, price2, price;
int main() {
int coffeetype = 1;
cout<<"\nPress'1'for buy a coffee\n";
cout<<"\nPress' 2' for operator mode\n\n";
int input;
cin>>input;
if (input==2)
{
cout << "Welcome to operator mode \n";
cout << "Press '1' for add more coffee powder \n";
cout << "Press '2' for exit\n";
int op;
cin >> op;
if(op==2){
return op;
}
}
coffee drink[] = {
{ "Espresso", 120, "Italy", 20 },
{ "Iced coffee", 150, "France", 20 },
{ "Long black", 80, "Austral", 20 },
{ "Americano", 100, "America", 20 },
{ "Latte", 200, "Italy", 20 },
{ "Irishcoffee",130, "Ireland", 20 },
{ "Cappuccino", 180, "Italy", 20 }
};
cout << fixed;
cout << setprecision(2);
cout<<"Enter the name of coffee";
while(coffeetype != 8){
for (int i = 0; i != sizeof(drink)/sizeof(drink[0]); ++i)
cout<< "\n " << i+1 << ") "<<drink[i].name<<"\t\t"<<drink[i].itemprice<<"\t\t"<<drink[i].country<<"\t\t("<<drink[i].quantity<<") remaining";
I've used a vector for this struct part.
vector<coffee> drink {
{ "Espresso", 120, "Italy", 20 },
{ "Iced coffee", 150, "France", 20 },
{ "Long black", 80, "Austral", 20 },
{ "Americano", 100, "America", 20 },
{ "Latte", 200, "Italy", 20 },
{ "Irishcoffee",130, "Ireland", 20 },
{ "Cappuccino", 180, "Italy", 20 }
};
but after using this part my 'for loop' didn't work.. Could some one help me to 'cout' the vector part.
And also i need your help to make the operator mode.By operator mode operator should be able to add more coffee types and change the number of coffees in the machine..Below i have shown the code i got from one of the contributor in stackoverflow.But i dont know how to implement below code part to my code.
coffee entry;
cin >> entry.country
>> entry.itemprice
>> entry.country
>> entry.quantity;
drink.push_back(entry);
How to use above code to modify details in the struct(drink).
Upvotes: 2
Views: 77
Reputation: 131435
First, drink
is a poor choice of name for the array, which holds information regarding many dirnks - and in fact, many coffee-based drinks. So let's call it coffee_drinks
.
The problem with your for loop is probably your use of:
sizeof(drink)/sizeof(drink[0])
this "hack" works for C-style arrays - not for std::vector
s. The sizeof()
an std::vector
is not the total size in bytes of its elements - since the memory of the elements is dynamically allocated on the heap, and is only pointed to by the vector class instance.
You could just write:
for(int i = 0; i < coffee_drinks.size(); i++)
but even better, you can make it:
for(coffee drink : coffee_drinks)
which iterates over all elements in the std::vector
. This "trick" works for any class which has a begin()
and end()
member; it's called a range-based for loop.
Upvotes: 3