Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can someone tell me why I'm getting a segmentation fault (core dumped)? After I compile my code, I'm only able to to input the last

Can someone tell me why I'm getting a segmentation fault (core dumped)? After I compile my code, I'm only able to to input the last cin before I get segmentation fault (core dumped. Thank you.

Note: I did this w/o pointers as I'm doing problems out of my book and have not arrived to pointers yet.

Inventory.hpp

#ifndef INVENTORY_H

#define INVENTORY_H

class Inventory

{

//member variables

private:

int itemNumber, quantity;

double cost;

//member functions

public:

Inventory();

Inventory(int, int, double);

void setItemNumber(int); //beginning of setter funtions

void setQuantity(int);

void setCost(double);

int getItemNumber(); //beginning of getter functions

int getQuantity();

double getCost();

double getTotalCost(); //made this a double on purpose

};

#endif

Inventory.cpp (implementation file)

#include "Inventory.hpp" //include statement for class Inventory

#include

/*********************

**Description: Default Constructor

*********************/

Inventory::Inventory()

{

setItemNumber(000);

setQuantity(000);

setCost(000);

}

/**********************

**Description: Constructor that accepts all member variables as arguments.

**Calls other class functions to copy these values into appropriate member

**variables. Then calls the setTotalCost fuction.

***********************/

Inventory::Inventory(int iN, int q, double c)

{

setItemNumber(iN); //using set method

setQuantity(q);

setCost(c);

}

/***********************

**Description: The next 3 functions are setter functions

***********************/

void Inventory::setItemNumber(int iN)

{

setItemNumber(iN);

}

void Inventory::setQuantity(int q)

{

setQuantity(q);

}

void Inventory::setCost(double c)

{

setCost(c);

}

/********************

**Description: The next 3 functions are getter functions

*********************/

int Inventory::getItemNumber()

{

return itemNumber;

}

int Inventory::getQuantity()

{

return quantity;

}

double Inventory::getCost()

{

return cost;

}

double Inventory::getTotalCost()

{

return quantity * cost;

}

inventoryMain.cpp

#include "Inventory.hpp"

#include

#include

using std::cout;

using std::cin;

using std::endl;

using std::fixed;

using std::setprecision;

int main ()

{

int iN, q;

double c;

cout << "Please enter the item number. ";

cin >> iN;

cout << "Please enter the quantity. ";

cin >> q;

cout << "Please enter the cost. ";

cin >> c;

//create class object and set it with user inputs as arguments

Inventory inv1(iN, q, c);

//Display inventory data

cout << " Inventory Data ";

cout << "Item Number: " << inv1.getItemNumber() << endl;

cout << "Quantity: " << inv1.getQuantity() << endl;

cout << "Cost Per Item: " << inv1.getCost() << endl;

cout << "Total Cost: $: " << fixed << setprecision(2) << inv1.getTotalCost() << endl;

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

C++ Database Development

Authors: Al Stevens

1st Edition

1558283579, 978-1558283572

More Books

Students also viewed these Databases questions

Question

4. What are the current trends in computer software platforms?

Answered: 1 week ago