11 Replies - 1387 Views - Last Post: 28 November 2010 - 06:12 PM Rate Topic: -----

#1 LunchBox_1001  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 10-November 10

Inventory Class

Posted 28 November 2010 - 05:02 PM

I have been working on this program for a couple of days and i cant figure out how to fix it i have 2 errors and they are:
warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
fatal error C1014: too many include files : depth = 1024

The program will run but the output it gives me is this:
Item Number: -858993460
Quantity:-858993460
Total Cost:-858993460

Here is the program and the .h file that goes with the program.

#include "Inventory.h"
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	int itemNumber,
		quantity;
	int cost,
		totalCost;

	cout << "Enter the Item Number: ";
	cin >> itemNumber;

	while (itemNumber < 0)
	{
		cout << "Enter a positive value for the Item Number: ";
		cin >> itemNumber;
	}
	cout << "Please enter how many item you would like: ";
	cin >> quantity;

	while (quantity < 0)
	{
		cout << "Enter a positive value for the item: ";
		cin >> quantity;
	}
	cout << "Please enter the cost of the item: $";
	cin >> cost;

	while (cost < 0)
	{
		cout << "Enter a positive value for the cost of the item: $";
		cin >> cost;
	}

	cout << "----------------------------------" << endl;

	Inventory inventoryA(itemNumber, quantity, cost);

	totalCost = inventoryA.getTotalCost();
	itemNumber = inventoryA.getItemNumber();
	cost = inventoryA.getCost();
	quantity = inventoryA.getQuantity();
	cout << "Item Number: " << itemNumber << endl;
	cout << "Quantity: " << quantity << endl;
	cout << "Cost: $" << cost << endl;
	cout << "Total Cost: $" << fixed << setprecision(2) << totalCost << endl; 
	
	return 0;
}



And here is the .h file
#ifndef INVENTORY_H
#define INVENTORY_H

class Inventory
{
private:
	int itemNumber;
	int quantity;
	double cost;
	double totalCost;
public:
	// Default constructor
	Inventory()
		{ itemNumber = quantity = cost = totalCost = 0; }
	
	// Overloaded constructor
	Inventory(int, int, double);	// Defined in Inventory.cpp

	// Mutators (i.e., "set" functions) defined in Inventory.cpp
	void setItemNumber(int);
	void setQuantity(int);
	void setCost(double);

	// setTotalCost calculates the total cost
	// and stores the result in the totalCost member
	void setTotalCost()
		{ totalCost = cost * quantity; }

	// Accessors (i.e., "get" functions)
	int getItemNumber()
		{ return itemNumber; }
	int getQuantity()
		{ return quantity; }
	double getCost()
		{ return cost; }
	double getTotalCost()
		{ return totalCost; }

	// Input validation functions
	bool validInt(int);
	bool validFloat(double);
};

#endif

// This is the inventory.cpp file.
// It contains the Inventory class function definitions. 

#include <iostream>
#include "Inventory.h"
using namespace std;

//************************************************************
// Overloaded constructor
// Accepts arguments to be stored in each member variable.
//************************************************************
Inventory::Inventory(int in, int q, double c)
{ 
	setItemNumber(in);
	setQuantity(q);
	setCost(c);
	setTotalCost();
}

//************************************************************
// setItemNumber accepts an argument to be stored in item number.
//************************************************************
void Inventory::setItemNumber(int in)
{
	while (!validInt(in))
	{
		cout << "Item Number must be positive. Please re-enter: ";
		cin  >> in;
	}
	itemNumber = in; 
}

//************************************************************
// setQuantity accepts an argument to be stored in quantity.
//************************************************************
void Inventory::setQuantity(int q)
{
	while (!validInt(q))
	{
		cout << "Quantity must be positive. Please re-enter: ";
		cin  >> q;
	}
	quantity = q; 
}

//************************************************************
// setCost accepts an argument to be stored in cost.
//************************************************************
void Inventory::setCost(double c)
{
	while (!validInt(c))
	{
		cout << "Cost must be positive. Please re-enter: ";
		cin  >> c;
	}
	cost = c; 
}

//************************************************************
// The validInt member tests its integer argument to see 
// if it is negative. If the argument is negative, the function 
// returns false. Otherwise, the function returns true.
//************************************************************
bool Inventory::validInt(int value)
{
	if (value < 0)    // the value is negative so it is NOT valid
		return false;
	else              // the integer value is valid
		return true;  
}

//************************************************************
// The validFloat member tests its floating-point argument to see
// if it is negative. If the argument is negative, the function 
// returns false. Otherwise, the function returns true.
//************************************************************
bool Inventory::validFloat(double value)
{
	if (value < 0)    // the value is negative so it is NOT valid
		return false;
	else              // the floating-point value is valid
		return true;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Inventory Class

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4071
  • View blog
  • Posts: 12,557
  • Joined: 25-December 09

Re: Inventory Class

Posted 28 November 2010 - 05:14 PM

In your code above: Is this supposed to be 2 or 3 files?

Jim
Was This Post Helpful? 0
  • +
  • -

#3 LunchBox_1001  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 10-November 10

Re: Inventory Class

Posted 28 November 2010 - 05:20 PM

There is 2 files sorry about that
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 4071
  • View blog
  • Posts: 12,557
  • Joined: 25-December 09

Re: Inventory Class

Posted 28 November 2010 - 05:26 PM

Then your problem probably starts at around line 46 of your include file. There probably should be 3 files.


Jim
Was This Post Helpful? 0
  • +
  • -

#5 LunchBox_1001  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 10-November 10

Re: Inventory Class

Posted 28 November 2010 - 05:32 PM

See my teacher gave me the .h file to use and we had to create the include file
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon


Reputation: 4071
  • View blog
  • Posts: 12,557
  • Joined: 25-December 09

Re: Inventory Class

Posted 28 November 2010 - 05:38 PM

Quote

// This is the inventory.cpp file.
// It contains the Inventory class function definitions.

#include <iostream>
#include "Inventory.h"
using namespace std;


The above snippet contains line 46 of your posted inventory.h file. So the lines above and everything below them should be in inventory.cpp


Jim
Was This Post Helpful? 0
  • +
  • -

#7 LunchBox_1001  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 10-November 10

Re: Inventory Class

Posted 28 November 2010 - 05:39 PM

Ok I got it to work by taking out line 50 of the .h file now I'm getting 4 warring and they are:
warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
warning C4244: 'argument' : conversion from 'double' to 'int', possible loss of data
warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
Was This Post Helpful? 0
  • +
  • -

#8 LunchBox_1001  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 10-November 10

Re: Inventory Class

Posted 28 November 2010 - 05:45 PM

Ok i did what you said but i still get those 4 errors
Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is offline

  • member icon


Reputation: 4071
  • View blog
  • Posts: 12,557
  • Joined: 25-December 09

Re: Inventory Class

Posted 28 November 2010 - 05:47 PM

Please post the entire error messages. They will include the file and line number where the error occurred.

And you need to separate the definition (.h) and the implementation (.cpp) as I stated before.



Jim

This post has been edited by jimblumberg: 28 November 2010 - 05:50 PM

Was This Post Helpful? 0
  • +
  • -

#10 LunchBox_1001  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 10-November 10

Re: Inventory Class

Posted 28 November 2010 - 05:54 PM

ok here they are

Line 13 of the .h file
warning C4244: '=' : conversion from 'double' to 'int', possible loss of data

Line 42 of the inventory.cpp
warning C4244: '=' : conversion from 'double' to 'int', possible loss of data

Line 44 of the inventory.cpp
warning C4244: '=' : conversion from 'double' to 'int', possible loss of data

Line 94 of the .h file but i moved it the inventory.cpp
warning C4244: 'argument' : conversion from 'double' to 'int', possible loss of data
Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg  Icon User is offline

  • member icon


Reputation: 4071
  • View blog
  • Posts: 12,557
  • Joined: 25-December 09

Re: Inventory Class

Posted 28 November 2010 - 06:03 PM

This should be about line 13 of your include file:

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



You should initialize your variables individually. Like:

itemNumber = 0; quantity = 0; cost = 0.0; totalCost = 0.0;


As for the other warnings without the modified code I can't tell where the affected lines are.

Jim
Was This Post Helpful? 0
  • +
  • -

#12 LunchBox_1001  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 10-November 10

Re: Inventory Class

Posted 28 November 2010 - 06:12 PM

OK thank you very much Jim that has been a lot of help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1