Need help with C++ program

Overview

For this assignment, implement a class called Product that will be used to represent a product that might be found in a novelty shop.

class Product

The Product class definition should be placed at the top of a source code file while the method implementation should be done after the closing curly brace for main().

Data Members

The Product class should contain four private data members. They are:

an array of characters that will hold the product identification code. It should have room for 11 characters plus a null terminator.
an array of characters that will hold the product name. It should have room for 45 characters plus a null terminator.
a float (or double) variable to hold the price of the product
an integer to hold the quantity of the product that is in stock
Constructors

Constructor 1:

The first constructor for the Product class (the default constructor) takes no arguments. Like all C++ constructors, it does not have a return data type.

It should set the product code and name data members to "null strings". This can be done by copying a null string literal ("") into a character array using strcpy() or by setting the first element of the array to a null character ('\0'). The price and quantity in stock data members should be set to 0.

Constructor 2:

The second constructor for the Product class should take four arguments: an array of character that represents a product code, an array of character that represents a product name, a float (or double) that represents a price for the product, and an integer that represents the quantity in stock for the product. DO NOT GIVE THESE ARGUMENTS THE SAME NAMES AS YOUR DATA MEMBERS. Like all C++ constructors, this constructor does not have a return data type.

Call the various "set" methods to set the value for all 4 data members.

Methods

getPrice

This accessor method returns the price data member. It takes no arguments and returns a float (or double).

getQuantity

This accessor method returns the quantity in stock data member. It takes no arguments and returns an integer.

setProductCode

This method will set the product code for an object. It takes one argument: an array of characters that holds a new product code. It returns nothing. The method should use the strcpy function to copy the new product code into the product code data member.

setName

This method will set the product name for an object. It takes one argument: an array of characters that holds a new product name. It returns nothing. The method should use the strcpy function to copy the new product name into the product name data member.

setPrice

This method will set the price for an object. It takes one argument: a double that holds a new price. It returns nothing. The method should check if the new price is greater than or equal to 0. If it is, it should set the price data member to the new price. Otherwise, it should set the price data member to 0.

setQuantity

This method will set the quantity in stock for an object. It takes one argument: an integer argument that holds a new quantity in stock. It returns nothing. The method should check if the new quantity is greater than or equal to 0. If it is, it should set the quantity data member to the new quantity. Otherwise, it should set the quantity data member to 0.

fulfillOrder

This method will process an order. It takes one argument: an integer that represents the quantity of this product that has been ordered. It returns an integer, which is the quantity of the product that the store is actually able to ship at this time.

The logic for this method should be as follows:

If the order quantity is less than zero, the order is in error. The number shipped should be zero. Do not alter the quantity in stock for the product.

If the order quantity is less than or equal to the quantity in stock, the order can be completely filled. The number shipped should be the same as the order quantity, and the order quantity should be subtracted from the quantity in stock.

Otherwise, this order can not be completely filled. The number shipped should be the quantity in stock, and the quantity in stock should be set to zero.

print

This method displays the contents of an object. It takes no arguments and returns nothing. It should print the product code, name, price, and quantity members on a single line. Use setw() to line the printed values up in columns (a width of 14 for the product code, 46 for the name, 6 for the price, and 6 for the quantity will match the sample output). The product code and name should be left-justified; the price and quantity should be right justified. The price should be printed using fixed-point notation with two places after the decimal point.

main()

In main(), create 3 Product objects. They should contain the values:

The first product should be created with the default constructor (ie. the one that takes no arguments)

The second product should be created with the second constructor. The product code should be "22222222222", product name "Virtual Education Pack", price of $0.99, and quantity in stock of 31.

The third product should be created with the second constructor. The product code should be "33333333333", product name "Dehydrated Water Bed", price of $-12.99, and quantity in stock of -6.

The rest of main() will include using the various methods on each of the 3 Product objects.

For each of the products, call the print method to display the product information. Before each call to print, display a short title such as "Product 1".

For product 1, call the set methods to set the product code to "11111111111", product name to "Flowbee Pet Groomer", price to $339.99, and the quantity in stock to 28. Call the print method to display the updated product 1 information. As before, make sure to include a short title such as "Product 1"

For product 2, call the getPrice and getQuantity methods and display the price and quantity in stock with appropriate labels.

For product 1, call the print method to display the initial product information. Call the fulfillOrder method and attempt to order -5 product 1s. Display the number of product 1s that were shipped. Call the print method again to display the updated product information.

For product 1, call the print method to display the initial product information. Call the fulfillOrder method and attempt to order 12 product 1s. Display the number of product 1s that were shipped. Call the print method again to display the updated product information.

For product 2, call the print method to display the initial product information. Call the fulfillOrder method and attempt to order 4 product 2s. Display the number of product 2s that were shipped. Call the print method again to display the updated product information.

For product 2, call the print method to display the initial product information. Call the fulfillOrder method and attempt to order 45 product 2s. Display the number of product 2s that were shipped. Call the print method again to display the updated product information.
This is what i have so far.

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

class Product
{
private:
char idNum[12];
char Pname[46];
double Price;
int Quantity;
public:
Product();
Product(char [], char [],double,int);
double getPrice();
int getQuantity();
void setProductCode( char []);
void setName(char[]);
void setPrice(double);
void setQuantity(int);
int fulfillOrder(int);
void print();

};

Product::Product()
{
idNum[12]={'\0'};
Pname[46]={'\0'};
}

Product::Product(char ID[],char PN[], double Pr,int Qu )
{
setProductCode( ID);
setName(PN);
setPrice(Pr);
setQuantity(Qu);
}

double Product::getPrice()
{
cout<<"Price: "<<Price<<endl;
return Price;
}

int Product::getQuantity()
{
cout<<"Quantitiy: "<<Quantity<<endl;
return Quantity;
}

void Product::setProductCode(char ID[])
{
strcpy(ID,ID);
}

void Product::setName(char PN[])
{
strcpy(PN,PN);
}

void Product::setPrice( double newPrice)
{
if(newPrice>=0)
{
Price=newPrice;
}
else
{
Price=0;
}
}

void Product::setQuantity(int newQuantity)
{
if(newQuantity>=0)
{
Quantity=newQuantity;
}
else
{
Quantity=0;
}
}

int Product::fulfillOrder( int orderq)
{
if( orderq<0)
{
cout<<"Error"<<endl;
orderq=0;
cout<<"Shipped: "<<orderq<<endl;
}

else if( orderq<=Quantity)
{
orderq=Quantity;
Quantity-=orderq;
cout<<"Shipped: "<<orderq<<endl;
}
else
{
orderq=Quantity;
orderq=0;
cout<<"Shipped: "<<orderq<<endl;
}
}

void Product::print()
{
cout<<"Product ID: "<<idNum<<" Product Name: "<<Pname<<" Price: "<<Price<<" Quantity: "<<Quantity<<endl;
}

int main()
{
Product product1=Product();

Product product2=Product{"22222222222","Virtual Education Pack",0.99,31};
cout<<"Product 1"<<endl;

product1.print();

Product product3=Product{"33333333333","Dehydrated Water Bed",-12.99,-6};
product2.print();

cout<<"Product 1"<<endl;


product1.setProductCode("11111111111");
product1.setName("Flowbee Pet Groomer");
product1.setPrice(399.99);
product1.setQuantity(28);
product1.print();

product2.getPrice();
product2.getQuantity();

product1.print();
product1.fulfillOrder(-5);
product1.print();

product1.print();
product1.fulfillOrder(12);
product1.print();

product2.print();
product2.fulfillOrder(4);
product2.print();

product2.print();
product2.fulfillOrder(45);
product2.print();



}


you have

Product::Product()
{
idNum[12]={'\0'};
Pname[46]={'\0'};
}

it should be

Product::Product()
{
idNum[12]='\0';
Pname[46]='\0';
}



Product product2=Product{"22222222222","Virtual Education Pack",0.99,31};
Product product3=Product{"33333333333","Dehydrated Water Bed",-12.99,-6};



should be

Product product2=Product("22222222222","Virtual Education Pack",0.99,31);
Product product3=Product("33333333333","Dehydrated Water Bed",-12.99,-6);




add this to the end of int main()

system("pause");
return 0;


and dont forget setw() and setprecision()
in the Product::print method




im still getting a lot of erros; what else is wrong with the program
errors*






strcpy(idNum,ID);
strcpy(Pname,PN);


you were copying the string into itself
Topic archived. No new replies allowed.