Hi,

i am trying to write a program for my homework.
Write a function, lookupPrice that uses the product code to look up for the price. If the product code is on special promotion, a 10% discount is given. If the product code is found, the function returns the price, otherwise it returns 0.0 The function prototype is as follows:
double lookupPrice (int prodCode)

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
struct product
{
	int pCode;
	string pDesc;
	double pCost;
	bool sPromo;
};

double lookupPrice(int);

int main()
{
	int id;
	double pprice;
	cout << "Please select a product code: ";
	cin >> id;
	pprice = lookupPrice(id);
	cout << "Price is "<<pprice;
	

	cin.ignore();
	cin.ignore();
	return 0;


}
double lookupPrice(int id)
{

	product products[10] =
		{
		{1000, "Pilot Blue Pen", 1.20, "Yes"},
		{1001, "Pilot Black Pen", 1.35, "No"},
		{1002, "File", 1.50, "No"},
		{1003, "Exercise Book", 0.90, "Yes"}
		};
	if (product.sPromo="Yes")// this is where the error takes place when i try to compile. 1>test4.cpp(41): error C2059: syntax error : '.' and many other error are related to the '.' in this function.

	{
		product.pCost=product.pCost-product.pCost/10;}
	else
		{product.pCost=product.pCost;
	}
	return (product.pCost);
}
if (product.sPromo="Yes")

'product' is the definition of a kind of structure; it is not an actual instance of that structure. You need to decide which object that you have created you want to test. I expect it's one of that array of product you created; the array named 'products'.

thanks! but then.... this error came out. :( what does that error mean.. tried to surf the net for the answer but not too sure what it means

double lookupPrice(int id)
{

	product products[10] =
		{
		{1000, "Pilot Blue Pen", 1.20, "Yes"},
		{1001, "Pilot Black Pen", 1.35, "No"},
		{1002, "File", 1.50, "No"},
		{1003, "Exercise Book", 0.90, "Yes"}
		};
	if (products.sPromo="Yes") //1>test4.cpp(41): error C2228: left of '.sPromo' must have class/struct/union. same for the others. 

	{
		products.pCost=products.pCost-products.pCost/10;}
	else
		{products.pCost=products.pCost;
	}
	return (products.pCost);
}

'products' is a pointer to an object of type 'product'.

The operator '.', as in 'products.sPromo', only works on actual structures, not on pointers.

'products[0]' is an object of type product. 'product' is a pointer to an object of type product.

ok! got it running!! thanks! :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.