1 Replies - 5160 Views - Last Post: 05 March 2007 - 02:57 PM Rate Topic: -----

#1 oat  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 30-January 07

Using Classes [Grocery Store]

Posted 05 March 2007 - 02:42 PM

I think I'm pretty close in getting this, but I get plenty of errors that are similar.... Yes, this is for school, so the problem is as follows....

Part A

Quote

Define a class names GroceryItem. Include private fields that hold an item's stock number, price, quantity in stock, and total value. Write a public function named dataEntry() that calls four private functions. Three of the private functions prompt the user for keyboard input for a value for one of the data fields stock number, price, and quantity in stock. The function that sets the stock number requires the user to enter a value between 1000 and 9999 inclusive; continue to prompt the user until a valid stock number is entered. The functions that set the price and quantity in stock require non-negative values; continue to prompt the user until valid values are entered. Include a fourth private function that calculates the GroceryItem's total value field (price times quantity in stock). Write a public function that displays a groceryItem object, assigns values to its fields, and uses the display function.


Part B

Quote

Write a main() function that declares an array of 10 GroceryItem objects. Assign values to all 10 items and display them.


My Code
// grocery.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>

using namespace std;

class GroceryItem
{
private:
	int stockNum;
	double itemPrice;
	double quantity;
	double total;
};
class dataEntry
{
public:
	void display();
	void setStockNum(int);
	void setItemPrice(double);
	void setQuantity(double);
	void total(double);
};
void dataEntry::display()
{
	cout<<"Your Total is "<<total<<endl;
}
void dataEntry::setStockNum(int num)
{
	const int HIGH_STOCK = 9999;
	const int LOW_STOCK = 1000;
	int stockNum;
	while(stockNum < LOW_STOCK || stockNum > HIGH_STOCK)
	{
		cout<<"Please Enter A Valid Stock Number ID: ";
			cin>>stockNum;
	}

	stockNum = num;
	return num;
}
void dataEntry::setItem(double price)
{
	double itemPrice;
	while(itemPrice < 0)
	{
		cout<<"Please Enter A Valid Price: ";
			cin>>itemPrice;
	}
	itemPrice = price;
	return price;
}
void dataEntry::setQuantity(double number)
{
	double numQuantity;
	while(numQuantity < 0)
	{
		cout<<"Please Enter A Valid Quantity: ";
			cin<<numQuantity;
	}
	numQuantity = number;
	return number;
}
void dataEntry::total(double total)
{
	double number;
	double price;
	total = number * price;
	return total;
}

int main()
{
	dataEntry Items;
	int stockNum, quantity;
	double itemPrice;
	
	cout<<"Please Enter the Items' Stock Number: ";
		cin>>stockNum;
		cout<<"Please Enter the Items' Price: ";
		cin>>itemPrice;
		cout<<"Please Enter the Quantity Preferred: ";
		cin>>quantity;
		Items.setStockNum(stockNum);
		Items.setItemPrice(itemPrice);
		Items.setQuantity(quantity);
		Items.display();
}



Errors
c:\documents and settings\mike\my documents\visual studio 2005\projects\grocery\grocery\grocery.cpp(28) : error C3867: 'dataEntry::total': function call missing argument list; use '&dataEntry::total' to create a pointer to member
1>c:\documents and settings\mike\my documents\visual studio 2005\projects\grocery\grocery\grocery.cpp(42) : error C2562: 'dataEntry::setStockNum' : 'void' function returning a value
1>		c:\documents and settings\mike\my documents\visual studio 2005\projects\grocery\grocery\grocery.cpp(21) : see declaration of 'dataEntry::setStockNum'
1>c:\documents and settings\mike\my documents\visual studio 2005\projects\grocery\grocery\grocery.cpp(44) : error C2039: 'setItem' : is not a member of 'dataEntry'
1>		c:\documents and settings\mike\my documents\visual studio 2005\projects\grocery\grocery\grocery.cpp(18) : see declaration of 'dataEntry'
1>c:\documents and settings\mike\my documents\visual studio 2005\projects\grocery\grocery\grocery.cpp(53) : error C2562: 'setItem' : 'void' function returning a value
1>		c:\documents and settings\mike\my documents\visual studio 2005\projects\grocery\grocery\grocery.cpp(44) : see declaration of 'setItem'
1>c:\documents and settings\mike\my documents\visual studio 2005\projects\grocery\grocery\grocery.cpp(61) : error C2784: 'std::basic_ostream<char,_Traits> &std::operator <<(std::basic_ostream<char,_Traits> &,unsigned char)' : could not deduce template argument for 'std::basic_ostream<char,_Traits> &' from 'std::istream'

1>c:\documents and settings\mike\my documents\visual studio 2005\projects\grocery\grocery\grocery.cpp(61) : error C2676: binary '<<' : 'std::istream' does not define this operator or a conversion to a type acceptable to the predefined operator
1>c:\documents and settings\mike\my documents\visual studio 2005\projects\grocery\grocery\grocery.cpp(64) : error C2562: 'dataEntry::setQuantity' : 'void' function returning a value
1>		c:\documents and settings\mike\my documents\visual studio 2005\projects\grocery\grocery\grocery.cpp(23) : see declaration of 'dataEntry::setQuantity'
1>c:\documents and settings\mike\my documents\visual studio 2005\projects\grocery\grocery\grocery.cpp(71) : error C2562: 'dataEntry::total' : 'void' function returning a value
1>		c:\documents and settings\mike\my documents\visual studio 2005\projects\grocery\grocery\grocery.cpp(24) : see declaration of 'dataEntry::total'


Is This A Good Question/Topic? 0
  • +

Replies To: Using Classes [Grocery Store]

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Using Classes [Grocery Store]

Posted 05 March 2007 - 02:57 PM

First things first: For every warning of the following type:

Quote

void function returning a value

Check each of the functions in question. Remember tat if the header looks like this:
void dataEntry::setQuantity(double number)


the key word void indicates that you are not returning a value from the function - except that you appear to be. For the function in question, you have:
return number;


Do you need to return a value from those functions?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1