void totalcat(fstream &file) // i get 0 for every catagory { int cat,sale = 0; i
ID: 3539621 • Letter: V
Question
void totalcat(fstream &file) // i get 0 for every catagory
{
int cat,sale = 0;
int i = 0;
inventory items;
file.open("data.dat",ios::in|ios::binary);
cout << "Choose the category you want to calculate"<<endl;
do
{
cout<< "choose number 1-3 :"<<endl;
cout<< "1.) Appliance "<< " 2.) Kitchenware "<<" 3.) Tool"<<endl;
cout << "category :";
cin>>cat;
switch(cat)
{
case 1:
strcpy(items.category, "Appliance");
// items.category = "Appliance" // how come you are assigning catagory in your total calculation?
break;
case 2:
strcpy(items.category, "Kitchenware");
// items.category = "Kitchenware" // how come you are assigning catagory in your total calculation?
break;
case 3:
strcpy(items.category, "Tool");
// items.category = "Tool" // how come you are assigning catagory in your total calculation?
break;
default : cout << "Please input number between 1 and 3 !"<<endl;;
}
}
while ((cat<1)||(cat>3));
if (cat==1)
{
while (!file.eof())
{
file.seekg((i)*sizeof(items), ios::beg);
file.read(reinterpret_cast<char *>(&items), sizeof(items));
if (!file.eof())
{
if (items.category == "Appliance")
sale = sale + (items.cost * items.quantity);
}
i++;
}
}
else if (cat==2)
while (!file.eof())
{
file.seekg((i)*sizeof(items), ios::beg);
file.read(reinterpret_cast<char *>(&items), sizeof(items));
if (!file.eof())
{
if (items.category == "Kitchenware")
sale = sale + (items.cost * items.quantity);
}
i++;
}
else if (cat==3)
while (!file.eof())
{
file.seekg((i)*sizeof(items), ios::beg);
file.read(reinterpret_cast<char *>(&items), sizeof(items));
if (!file.eof())
{
if (items.category == "Tool")
sale = sale + (items.cost * items.quantity);
}
i++;
}
else
{
cout << "you can only choose number 1-3" << endl;
}
cout <<"Total sales of selected category is: "<< sale<<endl<<endl;
file.close();
}
Explanation / Answer
you can not use "==" for string comparision. try this
if(strcmp(item.category,"Appliance")==0)
sale = sale + (item.cost * items.quantity);
if(strcmp(item.category,"Kitchenware")==0)
sale = sale + (item.cost * items.quantity);
if(strcmp(item.category,"Tool")==0)
sale = sale + (item.cost * items.quantity);
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.