Write this program in C++ \"grocury.txt\" apples:5:1 milk:3:2 bread:3:1 candy:10
ID: 3541339 • Letter: W
Question
Write this program in C++
"grocury.txt"
apples:5:1
milk:3:2
bread:3:1
candy:10:1
cheese:5:6
oranges:4:2
cherries:3:2
How much money do you have?: 4
Here's what we have in stock today:
*** apples $1 (qty: 5)
*** milk $2 (qty: 3)
*** bread $1 (qty: 3)
*** candy $1 (qty: 10)
*** cheese $6 (qty: 5)
*** oranges $2 (qty: 4)
*** cherries $2 (qty: 3)
You have $4
What would you like to put into your cart: oranges
Would you like to continue shopping? (y/n): y
Here's what we have in stock today:
*** apples $1 (qty: 5)
*** milk $2 (qty: 3)
*** bread $1 (qty: 3)
*** candy $1 (qty: 10)
*** cheese $6 (qty: 5)
*** oranges $2 (qty: 3)
*** cherries $2 (qty: 3)
You have $2
What would you like to put into your cart: apples
Would you like to continue shopping? (y/n): y
Here's what we have in stock today:
*** apples $1 (qty: 4)
*** milk $2 (qty: 3)
*** bread $1 (qty: 3)
*** candy $1 (qty: 10)
*** cheese $6 (qty: 5)
*** oranges $2 (qty: 3)
*** cherries $2 (qty: 3)
You have $1
What would you like to put into your cart: milk
Sorry, you don't have enough money!
Would you like to continue shopping? (y/n): y
Here's what we have in stock today:
*** apples $1 (qty: 4)
*** milk $2 (qty: 3)
*** bread $1 (qty: 3)
*** candy $1 (qty: 10)
*** cheese $6 (qty: 5)
*** oranges $2 (qty: 3)
*** cherries $2 (qty: 3)
You have $1
What would you like to put into your cart: butter
Sorry, we're all out of butter today.
Would you like to continue shopping? (y/n): y
Here's what we have in stock today:
*** apples $1 (qty: 4)
*** milk $2 (qty: 3)
*** bread $1 (qty: 3)
*** candy $1 (qty: 10)
*** cheese $6 (qty: 5)
*** oranges $2 (qty: 3)
*** cherries $2 (qty: 3)
You have $1
What would you like to put into your cart: candy
Would you like to continue shopping? (y/n): n
Here's what you've added to your shopping cart:
* oranges
* apples
* candy
You spent $4 and have $0 left over
Explanation / Answer
please rate - thanks
any questions ask
tested with DEV C++
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int index(string,string[],int);
void breakup(string ,string[],int[],int[],int);
void output(string[],int[],int[],int);
int main()
{ string name[10],buffer,cart[10];
int cost[10],amt[10],spent=0,have=0,i,items=0,inCart=0;
char cont='y';
ifstream input;
input.open("grocery.txt"); //open file
if(input.fail()) //is it ok?
{ cout<<"file did not open please check it ";
system("pause");
return 1;
}
input>>buffer;
while(input)
{breakup(buffer,name,cost,amt,items);
items++;
input>>buffer;
}
cout<<"How much money do you have? ";
cin>>have;
while(cont=='y'||cont=='Y')
{output(name,cost,amt,items);
cout<<"You have $"<<have<<endl;
cout<<"What would you like to put into your cart: ";
cin>>buffer;
i=index(buffer,name,items);
if(i>=items)
cout<<"Sorry, we're out of "<<buffer<<" today. ";
else
if(cost[i]>have)
cout<<"Sorry, you don't have enough money! ";
else
{cart[inCart]=buffer;
inCart++;
have-=cost[i];
amt[i]--;
spent+=cost[i];
}
cout<< "Would you like to continue shopping? (y/n): ";
cin>>cont;
}
cout<<"Here's what you've added to your shopping cart: ";
for(i=0;i<inCart;i++)
cout<<"* "<<cart[i]<<endl;
cout<<"You spent $"<<spent<<" and have $"<<have<<" left over ";
input.close();
system("pause");
return 0;
}
void output(string name[],int cost[],int amt[],int items)
{int i;
cout<<"Here's what we have in stock today: ";
for(i=0;i<items;i++)
cout<<"***"<<name[i]<<" $"<<cost[i]<<" ( qty: "<<amt[i]<<") ";
}
int index(string buffer,string name[],int items)
{int i;
for(i=0;i<items;i++)
if(buffer.compare(name[i])==0)
return i;
return items;
}
void breakup(string buffer,string name[],int cost[],int amt[],int items)
{int i,j;
i=buffer.find(':',0);
j=buffer.find(':',i+1);
name[items]=buffer.substr(0,i);
amt[items]= atoi(buffer.substr(i+1,j-i+1).c_str());
cost[items]=atoi(buffer.substr(j+1,buffer.length()).c_str());;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.