Can someone explain why i'm getting a segmentation error (core dumped)?

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) message.

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 <iostream>

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

**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 4 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 <iostream>

#include <iomanip>

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.\n";

cin >> iN;

cout << "Please enter the quantity.\n";

cin >> q;

cout << "Please enter the cost.\n";

cin >> c;

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

Inventory inv1(iN, q, c);

//Display inventory data

cout << "\nInventory Data\n";

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;

}
These three functions call themselves forever:
1
2
3
void Inventory::setItemNumber(int iN) { setItemNumber(iN); }
void Inventory::setQuantity(int q) { setQuantity(q); }
void Inventory::setCost(double c) { setCost(c); }

Last edited on
Ahhhhh! Thank you! I tried stepping away and I was still stumped.

I should have had them as such:

itemNumber = iN;
quantity = q;
cost = c;

I was using set methods in the wrong place. Ty again.
Topic archived. No new replies allowed.