Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello, I am working on a green grocery online shopping console application using

ID: 3568885 • Letter: H

Question

Hello, I am working on a green grocery online shopping console application using c++. The program should read a file, but i am having issues in which it is build successfully but crashes when I start without debugging. The code should work and be able to do the option 1. The code is below. I will rate 5++ for anybody who can solve this. Thanks.

The out put should be like this shown below

outputs the data from the file

which item do you want to add to shopping cart?

how many would you like?

at the end of shopping: this is what you bought and this is your bill ""

******************************************start of code*******************************************************************

#include <iostream>

#include<string>

#include<fstream>

using namespace std;

class GroceryItem {

public:

GroceryItem();//Default argument

GroceryItem(string, double, double, double); // four argument

void set_item_name(string); // Assigns a value to the data memeber item_name

void set_item_price(double); //Assigns a value to the data member item_price

void set_qty_on_hand(double);// Assigns a value to the data member quantity_on_hand.

void set_qty_purchased(double); // Sets qty_purchased to zero before a customer begins shopping.

string get_item_name(); // Returns the value of the data memebr item_name.

double get_item_price(); // Returns the value of the data member item_price

double get_qty_on_hand(); // Returns the value of the data member quantity_on_hand.

double get_qty_purchased(); // Retruns the value of the data memebr qty_purchased.

private:

string item_name;

double item_price;

double quantity_on_hand;

double quantity_purchased;

};

GroceryItem::GroceryItem(string name, double price, double hand, double quantity){

set_item_name(name);

set_item_price(price);

set_qty_on_hand(hand);

set_qty_purchased(quantity);

}

void GroceryItem::set_item_name(string name){

item_name = name;

}

string GroceryItem::get_item_name(){

return item_name;

}

void GroceryItem::set_item_price(double price){

item_price = price;

}

double GroceryItem::get_item_price(){

return item_price;

}

void GroceryItem::set_qty_on_hand(double hand){

quantity_on_hand = hand;

}

double GroceryItem::get_qty_on_hand(){

return quantity_on_hand;

}

void GroceryItem::set_qty_purchased(double purchased){

if (purchased > 0)

quantity_purchased = purchased;

if (purchased <= 0)

{

quantity_purchased = 0;

cout << " cart cannot be negative, it will be set to 0. ";

}

}

double GroceryItem::get_qty_purchased(){

return quantity_purchased;

}

int main(){

int input;

double quantity;

double sub;

double total;

ifstream infile;

infile.open("C:File.txt");

GroceryItem *arr[10];

int count = 0;

string name;

double price;

double hand;

int i = 0;

//creating multiple objects

if (infile.fail()){

cerr << "Error Opening file" << endl;

}

while (!infile.eof()){

//flush before begin

string str;

getline(infile, name);

arr[i]->set_item_name(name);

getline(infile, str);

price = atof(str.c_str());

arr[i]->set_item_price(price);

getline(infile, str);

hand = atof(str.c_str());

arr[i]->set_qty_on_hand(hand);

count++;

i++;

}

do{

cout<<"Welcome to Kmart Happy Shopping ";

for (int i=0; i< 10; i++){

cout get_item_price() << arr[i]->get_qty_on_hand() << arr[i]->get_qty_purchased();

}

cout<<"Pick your number"<

cin>>input;

if(input==0){

cout<<"You picked Raisin Grains "<

cout<<"How many quantites would you like to purchase"<

cin>>quantity;

arr[0]->set_qty_purchased(quantity);

sub = arr[0]->get_qty_on_hand();

sub = sub - quantity;

arr[0]->set_qty_on_hand(sub);

}

}while(input!=5);

infile.close();

}

***********************************************************end of code***********************************************************

The txt file or dat file should contain this as shown below.

Milk

2.41

200

grains

3.50

111

Explanation / Answer

#include<iostream>

#include<string.h>

#include<fstream>
#include<stdlib.h>

using namespace std;

class GroceryItem {

public:

GroceryItem();//Default argument

GroceryItem(string, double, double, double); // four argument

void set_item_name(string); // Assigns a value to the data memeber item_name

void set_item_price(double); //Assigns a value to the data member item_price

void set_qty_on_hand(double);// Assigns a value to the data member quantity_on_hand.

void set_qty_purchased(double); // Sets qty_purchased to zero before a customer begins shopping.

string get_item_name(); // Returns the value of the data memebr item_name.

double get_item_price(); // Returns the value of the data member item_price

double get_qty_on_hand(); // Returns the value of the data member quantity_on_hand.

double get_qty_purchased(); // Retruns the value of the data memebr qty_purchased.

private:

string item_name;

double item_price;

double quantity_on_hand;

double quantity_purchased;

};

GroceryItem::GroceryItem(string name, double price, double hand, double quantity){

set_item_name(name);

set_item_price(price);

set_qty_on_hand(hand);

set_qty_purchased(quantity);

}

void GroceryItem::set_item_name(string name){

item_name = name;

}

string GroceryItem::get_item_name(){

return item_name;

}

void GroceryItem::set_item_price(double price){

item_price = price;

}

double GroceryItem::get_item_price(){

return item_price;

}

void GroceryItem::set_qty_on_hand(double hand){

quantity_on_hand = hand;

}

double GroceryItem::get_qty_on_hand(){

return quantity_on_hand;

}

void GroceryItem::set_qty_purchased(double purchased){

if (purchased > 0)

quantity_purchased = purchased;

if (purchased <= 0)

{

quantity_purchased = 0;

cout << " cart cannot be negative, it will be set to 0. ";

}

}

double GroceryItem::get_qty_purchased(){

return quantity_purchased;

}

int main(){

int input;

double quantity;

double sub;

double total;

ifstream infile;

//infile.open("C:File.txt");
infile.open("File.txt");

GroceryItem *arr[10];

int count = 0;

string name;

double price;

double hand;

int i = 0;

//creating multiple objects

if (infile.fail()){

cerr << "Error Opening file" << endl;

}

while (!infile.eof()){

//flush before begin

// Alloacte memory for the array to stor the values
arr[i] = (GroceryItem *)malloc(sizeof(GroceryItem ));


string str;

getline(infile, name);
arr[i]->set_item_name(name);

getline(infile, str);

price = atof(str.c_str());
//cout << price << endl;

arr[i]->set_item_price(price);

getline(infile, str);

hand = atof(str.c_str());
//cout << hand << endl;

arr[i]->set_qty_on_hand(hand);
arr[i]->set_qty_purchased(0.0);

count++;

i++;

}

do{

cout<<"Welcome to Kmart Happy Shopping ";

for (int i=0; i< count; i++){

cout << arr[i]-> get_item_price() << " " << arr[i]->get_qty_on_hand() << " " << arr[i]->get_qty_purchased() << endl;

}

cout<<"Pick your number ";

cin>>input;

if(input==0){

cout<<"You picked Raisin Grains "<

cout<<"How many quantites would you like to purchase";

cin >> quantity;

arr[0]->set_qty_purchased(quantity);

sub = arr[0]->get_qty_on_hand();

sub = sub - quantity;

arr[0]->set_qty_on_hand(sub);

}

}while(input!=5);

infile.close();

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote