Program won't process completely

Hi,
I have a program that is supposed to process dummy inventory data and print results to the screen. No matter what I try, I cant seem to get the program to process beyond a certain line. The code at that line is:
Inventory inv2(777,10,21.50); There are three different sections to display data and only the first one does. It seems that every time it gets to that line of code, it skips everything after that.
Here is the main program:

#include <cstdlib>
#include <stdio.h>
#include <iostream>
#include "Inventory.h"

using namespace std;

/*
*
*/
int main() {
// Declare an Inventory object and use the default constructor
Inventory inv;
// Display the member values
cout << "Object is initialized with values using the default constructor\n"; cout << "The values of the members --- \n";
cout << "Item number: " << inv.getItemNumber() << endl;
cout << "Quantity: " << inv.getQuantity() << endl;
cout << "Cost: " << inv.getCost() << endl;
cout << "Total cost: " << inv.getTotalCost() << endl;
// Define an Inventory object and use the overloaded constructor.
Inventory inv2(777,10,12.50);
// Display the member values.
cout << "Assign values to the object members using the overloaded constructor\n"; cout << "The values of the members --- \n";
cout << "Item number: " << inv2.getItemNumber() << endl;
cout << "Quantity: " << inv2.getQuantity() << endl;
cout << "Cost: " << inv2.getCost() << endl;
cout << "Total cost: " << inv2.getTotalCost() << endl << endl;
// Use the mutator functions to change the member values.
inv2.setItemNumber(555);
inv2.setQuantity(20);
inv2.setCost(19.95);
inv2.setTotalCost();
// Display the modified values.
cout << "The values are changed using mutators.\n"; cout << "The values of the members --- \n";
cout << "Item number: " << inv2.getItemNumber() << endl; cout << "Quantity: "<< inv2.getQuantity() << endl;
cout << "Cost: " << inv2.getCost()<< endl;
cout << "Total cost: " << inv2.getTotalCost() << endl << endl;
system("pause");
return 0;
}



Here is the header file:

#ifndef INVENTORY_H
#define INVENTORY_H

class Inventory {

private:
int itemNumber;
int quantity;
double cost;
double totalCost;

public:
Inventory(); //Default constructor
Inventory(int,int,double); //Second constructor
void setItemNumber(int);
void setQuantity(int);
void setCost(double);
void setTotalCost();
int getItemNumber() const;
int getQuantity() const;
double getCost() const;
double getTotalCost() const;

};

#endif /* INVENTORY_H */


and here is the resource file:


#include "Inventory.h"
#include <iostream>
#include <cstdlib>

using namespace std;

//*************************************************************************************
// The default constructor initializes itemNumber, quantity, cost and totalCost to 0. *
//*************************************************************************************

Inventory::Inventory()
{
itemNumber = 0;
quantity = 0;
cost = 0.0;
totalCost = 0.0;
}

//*************************************************************************************
// The second constructor accepts item number, quantity and unit cost as arguments *
// storing them in the member variables and then calls setTotalCost. *
//*************************************************************************************

Inventory::Inventory(int inum, int qty, double cst )
{
if (inum >= 0)
itemNumber = inum;
else
cout << "Invalid item number! Number must NOT be less than zero.";
exit(EXIT_FAILURE);

if (qty >=0)
quantity = qty;
else
cout << "Invalid quantity! Number must NOT be less than zero.";
exit(EXIT_FAILURE);

if (cst >= 0)
cost = cst;
else
cout << "Invalid cost! Number must NOT be less than zero.";
exit(EXIT_FAILURE);

setTotalCost();
}

//*************************************************************************************
// setItemNumber accepts an integer and copies it to itemNumber variable. *
//*************************************************************************************

void Inventory::setItemNumber(int inumber)
{
if (inumber >= 0)
itemNumber = inumber;
else
cout << "Invalid item number! Number must NOT be less than zero.";
exit(EXIT_FAILURE);
}

//*************************************************************************************
//setQuantity accepts an integer and copies it to quantity variable. *
//*************************************************************************************

void Inventory::setQuantity(int qty)
{
if (qty >=0)
quantity = qty;
else
cout << "Invalid quantity! Number must NOT be less than zero.";
exit(EXIT_FAILURE);
}

//**************************************************************************************
//setCost accepts a double and copies it to cost variable. *
//**************************************************************************************

void Inventory::setCost(double cst)
{
if (cst >=0)
cost = cst;
else
cout << "Invalid cost! Number must NOT be less than zero.";
}

//**************************************************************************************
// setTotalCost calculates total cost (quantity * cost) and stores in totalCost *
// variable. *
//**************************************************************************************

void Inventory::setTotalCost()
{
totalCost = quantity * cost;
}

//**************************************************************************************
// getItemNumber returns value of itemNumber. *
//**************************************************************************************

int Inventory::getItemNumber() const
{
return itemNumber;
}

//**************************************************************************************
// getQuantity returns value of quantity. *
//**************************************************************************************

int Inventory::getQuantity() const
{
return quantity;
}

//**************************************************************************************
// getCost returns the value of cost. *
//**************************************************************************************

double Inventory::getCost() const
{
return cost;
}

//**************************************************************************************
// getTotalCost returns the value of totalCost. *
//**************************************************************************************

double Inventory::getTotalCost() const
{
return totalCost;
}


Any assistance would be greatly appreciated.. Thanks!

Curly braces are your friends - ALWAYS use curly braces.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Inventory::Inventory(int inum, int qty, double cst )
{
    if (inum >= 0)
        itemNumber = inum;
    else
        cout << "Invalid item number! Number must NOT be less than zero.";
    exit(EXIT_FAILURE); //will always run, program ends here

    if (qty >=0)
        quantity = qty;
    else
        cout << "Invalid quantity! Number must NOT be less than zero.";
    exit(EXIT_FAILURE);

    if (cst >= 0)
        cost = cst;
    else
        cout << "Invalid cost! Number must NOT be less than zero.";
    exit(EXIT_FAILURE); 

    setTotalCost();
}
Last edited on
Thank you! I was just logging on to say I figured it out. Curly braces are indeed my friends!

Thanks again.
Topic archived. No new replies allowed.