3 Replies - 1803 Views - Last Post: 07 October 2007 - 12:51 PM Rate Topic: -----

#1 James Bond C++ Spy  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 132
  • Joined: 03-October 07

Using Header files and creating objects of a class

Posted 05 October 2007 - 12:49 PM

I am in my first semester of C++ and I am running into troubles. I have had a semester of VB. I am having some trouble getting this to work.

Problem: Have user enter 5 products and quantities. Calculate the total for the 5 products * quantity. Use a switch to find the products and different prices to multiply times the quantity. My code is in one CPP file and they want it in 2 CPP's and a .h file. This is my first look at Object Oriented Programming and I am having trouble grasping the concept of the 3 files. Thanks for any help with this problem. This site is awesome

Here is my code:

#include <iostream>

using std::cin;
using std::cout;
using std::endl;

int main()
{
int ProductNumber;

int OrderProduct1;
int OrderProduct2;
int OrderProduct3;
int OrderProduct4;
int OrderProduct5;

int Quantity1;
int Quantity2;
int Quantity3;
int Quantity4;
int Quantity5;

double OrderTotal1;
double OrderTotal2;
double OrderTotal3;
double OrderTotal4;
double OrderTotal5;

// do while account number is less than 6
while ( ProductNumber < 6 )
{

//user inputs product number and quantity
	 cout << "Please Enter the Product Number or -1 to Exit: ";
	  cin >> OrderProduct1;
	  cout << "Please Enter the Quantity";
	  cin >> Quantity1;
	  
	 cout << "Please Enter the Product Number: ";
	  cin >> OrderProduct2; 
	  cout << "Please Enter the Quantity";
	  cin >> Quantity2;
	  
	 cout << "Please Enter the Product Number: ";
	  cin >> OrderProduct3; 
	  cout << "Please Enter the Quantity";
	  cin >> Quantity3;

	 cout << "Please Enter the Product Number: ";
	  cin >> OrderProduct4; 
	  cout << "Please Enter the Quantity";
	  cin >> Quantity4;

	 cout << "Please Enter the Product Number: ";
	  cin >> OrderProduct5; 
	  cout << "Please Enter the Quantity";
	  cin >> Quantity5;

	  switch ( ProductNumber) // switch statement nested in while
	  {
		 case '1': // product 1
			OrderTotal1 = Quantity1 * 2.98; // total charge for product 1 
			break; // necessary to exit switch

		 case '2': // product 2
			OrderTotal2 = Quantity2 * 4.50; // total charge for product 2	
			break; // exit switch

		 case '3': // product 3
			OrderTotal3 = Quantity3 * 9.98; // total charge for product 3  
			break; // exit switch

		 case '4': // product 4
			OrderTotal4 = Quantity4 * 4.49; // total charge for product 4	
			break; // exit switch

		 case '5': // product 5
			OrderTotal5 = Quantity5 * 6.87; // total charge for product 5	
			break; // exit switch

		 case '\n': // ignore newlines,  
		 case '\t': // tabs, 
		 case ' ': // and spaces in input
			break; // exit switch

		 default: // catch all other characters
			cout << "Incorrect Product Number."
			   << " Enter a New Product Number." << endl;
			break; // optional; will exit switch anyway
	  } // end switch
   } // end while
} // end function input product number and quantity


cout << "Total Amount for Order 1 Is: $" << OrderTotal1 << endl;
cout << "Total Amount for Order 2 Is: $" << OrderTotal2 << endl;
cout << "Total Amount for Order 3 Is: $" << OrderTotal3 << endl;
cout << "Total Amount for Order 4 Is: $" << OrderTotal4 << endl;
cout << "Total Amount for Order 5 Is: $" << OrderTotal5 << endl;

} // end while loop
} // end main


Is This A Good Question/Topic? 0
  • +

Replies To: Using Header files and creating objects of a class

#2 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 45
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: Using Header files and creating objects of a class

Posted 05 October 2007 - 02:46 PM

well, first off, this won't compile, because it's got two extra }s in it somewhere. check your indentation, and make sure you have the ends of the routine bodies where you actually meant to.

header files are usually used to contain forward declarations of classes and functions and numerical constants associated with them. within a header file, you could then use an #include directive to include the actual source code associated with the definitions of your classes/functions.

however, since this code is just one great big whopping routine, there is really no easy way to break it up. multiple files only works if you've got your code in discrete blocks like functions and classes.

the following code snippets are an example how a program can be created with a couple of .cpp files and a custom header file:

//  circle.h
#ifndef __CIRCLE_H
#define __CIRCLE_H

#define PI 3.1415926535

double area(double radius);
double circumference(double radius);

#include "circle.cpp"

#endif



//  circle.cpp
double area(double radius) {
	return PI*radius*radius;
}

double circumference(double radius) {
	return 2.0*PI*radius;
}



//  main routine and include directives
#include <iostream>
#include "circle.h"

using namespace std;

int main(int argv, char* argc) {
	
	double radius=10.0;
	
	cout << "radius of circle: " << radius << endl;
	cout << "  area:		   " << area(radius) << endl;
	cout << "  circumference:  " << circumference(radius) << endl;
	return 0;
}



hope this helps a bit.

-jjh
Was This Post Helpful? 0
  • +
  • -

#3 James Bond C++ Spy  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 132
  • Joined: 03-October 07

Re: Using Header files and creating objects of a class

Posted 05 October 2007 - 08:27 PM

OK I guess I need more help. :blink: How could I change the first code to work in 3 files? I took a stab at OOP for this problem but not even close I don't think. I read the examples and still don't get it but if I had this program that I've worked on and know about with maybe I could see how it is being written and learn from that. Anyone out there that can help me please. I will include that code so maybe someone can help. Thanks so much for the help.


// MailOrderHouse.cpp
// Member-function definitions for class ProductBook that
// uses a switch statement to calculate the charges for 5 products.
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include "MailOrderHouse.h" // include definition of class ProductBook

// constructor initializes products and quantities with string supplied as argument;
// initializes counter data members to 0
ProductBook::ProductBook( string name )
{
setquantity( name ); // validate and store products
Quantity1 = 0; // initialize Product 1 to 0
Quantity2 = 0; // initialize Product 2 to 0
Quantity3 = 0; // initialize Product 3 to 0
Quantity3 = 0; // initialize Product 4 to 0
Quantity4 = 0; // initialize Product 5 to 0

} // end ProductBook constructor


// function to retrieve the products and quantities
string ProductBook::getProduct1()
string ProductBook::getQuantity1()

string ProductBook::getProduct2()
string ProductBook::getQuantity2()

string ProductBook::getProduct3()
string ProductBook::getQuantity3()

string ProductBook::getProduct4()
string ProductBook::getQuantity4()

string ProductBook::getProduct5()
string ProductBook::getQuantity5()
{

return Product1;
return Quantity1;
return Product2;
return Quantity2;
return Product3;
return Quantity3;
return Product4;
return Quantity4;
return Product5;
return Quantity5;
} // end function getproduct and getquantity


// display a message to the ProductBook user
void ProductBook::displayMessage()
{
// this statement calls getproduxt and getquantity to calculate the
// total amount of charges
cout << "Please Enter the Product Number\n" << getproduct1() << "!\n"
<< endl;
cin >> product1 >>;
cout << "Please Enter the Quantity\n" << getQuantity1() << "!\n"
<< endl;
cin >> quantity1 >>;

cout << "Please Enter the Product Number\n" << getproduct2() << "!\n"
<< endl;
cin >> product2 >>;
cout << "Please Enter the Quantity\n" << getQuantity2() << "!\n"
<< endl;
cin >> quantity2 >>;

cout << "Please Enter the Product Number\n" << getproduct3() << "!\n"
<< endl;
cin >> product3 >>;
cout << "Please Enter the Quantity\n" << getQuantity3() << "!\n"
<< endl;
cin >> quantity3 >>;

cout << "Please Enter the Product Number\n" << getproduct4() << "!\n"
<< endl;
cin >> product4 >>;
cout << "Please Enter the Quantity\n" << getQuantity4() << "!\n"
<< endl;
cin >> quantity4 >>;

cout << "Please Enter the Product Number\n" << getproduct5() << "!\n"
<< endl;
cin >> product5 >>;
cout << "Please Enter the Quantity\n" << getQuantity5() << "!\n"
<< endl;
cin >> quantity5 >>;
} // end function displayMessage


// input product number and quantity from user; update counter
void ProductBook::inputProduct1()
void ProductBook::inputQuantity1()

void ProductBook::inputProduct2()
void ProductBook::inputQuantity2()

void ProductBook::inputProduct3()
void ProductBook::inputQuantity3()

void ProductBook::inputProduct4()
void ProductBook::inputQuantity4()

void ProductBook::inputProduct5()
void ProductBook::inputQuantity5()

{
int Product1; // product entered by user
int Product2; // product entered by user
int Product3; // product entered by user
int Product4; // product entered by user
int Product5; // product entered by user

int Quantity1; // quantity entered by user
int Quantity2; // quantity entered by user
int Quantity3; // quantity entered by user
int Quantity4; // quantity entered by user
int Quantity5; // quantity entered by user


cout << "Please Enter the Product Number: " << endl
<< "Enter the EOF character to end input." << endl;

// loop until user types end-of-file key sequence
while ( ( product = cin.get() ) != EOF )
{
// determine which product was entered
switch ( product ) // switch statement nested in while
{
case '1': // product 1
Total1 = Quantity1 * 2.98; // total charge for product 1
break; // necessary to exit switch

case '2': // product 2
Total2 = Quantity2 * 4.50; // total charge for product 2
break; // exit switch

case '3': // product 3
Total3 = Quantity3 * 9.98; // total charge for product 3
break; // exit switch

case '4': // product 4
Total4 = Quantity4 * 4.49; // total charge for product 4
break; // exit switch

case '5': // product 5
Total5 = Quantity5 * 6.87; // total charge for product 5
break; // exit switch

case '\n': // ignore newlines,
case '\t': // tabs,
case ' ': // and spaces in input
break; // exit switch

default: // catch all other characters
cout << "Incorrect Product Number."
<< " Enter a New Product Number." << endl;
break; // optional; will exit switch anyway
} // end switch
} // end while
} // end function input product number and quantity

// display a report based on the products and quantity entered by user
void ProductBook::displayProductReport()
{
// output summary of results
cout << "\n\nProduct Number and Amount of Total Charges:"
<< "\n1: " << Total1 // display total for product 1
<< "\n2: " << Total2 // display total for product 2
<< "\n3: " << Total3 // display total for product 3
<< "\n4: " << Total4 // display total for product 4
<< "\n5: " << Total5 // display total for product 5
<< endl;
} // end function display ProductReport




// MailOrderHouse.h
// Definition of class ProductBook that determines a amount.
// Member functions are defined in MailOrderHouse.cpp
#include <string> // program uses C++ standard string class
using std::string;

// PeoductBook class definition
class ProductBook
{
public:
ProductBook( string ); // constructor initializes products
void setProduct1( string ); // function to set the product 1
string getProduct1(); // function to retrieve product 1
void setQuantity1( string ); // function to set the quantity 1
string getquantity1(); // function to retrieve quantity 1

void setProduct2( string ); // function to set the product 2
string getProduct2(); // function to retrieve the product 2
void setQuantity2( string ); // function to set the quantity 2
string getquantity2(); // function to retrieve quantity 2

void setProduct3( string ); // function to set the product 3
string getProduct3(); // function to retrieve the product 3
void setQuantity3( string ); // function to set the quantity 13
string getquantity3(); // function to retrieve quantity 3

void setProduct4( string ); // function to set the product 4
string getProduct4(); // function to retrieve the product 4
void setQuantity4( string ); // function to set the quantity 4
string getquantity4(); // function to retrieve quantity 4

void setProduct5( string ); // function to set the product 5
string getProduct5(); // function to retrieve the product 5
void setQuantity5( string ); // function to set the quantity 5
string getquantity5(); // function to retrieve quantity 5


void determineCharges(); // calculates charges entered by the user

private:
string Product1; // product 1 for this Product Book
string Product2; // product 2 for this Product Book
string Product3; // product 3 for this Product Book
string Product4; // product 4 for this Product Book
string Product5; // product 5 for this Product Book

string Quantity1; // quantity 1 for this Product Book
string Quantity2; // quantity 2 for this Product Book
string Quantity3; // quantity 3 for this Product Book
string Quantity4; // quantity 4 for this Product Book
string Quantity5; // quantity 5 for this Product Book


}; // end class productBook






// MailOrderHouse_Driver.cpp
// Create ProductBook object and invoke its determined Amounts function.

// include definition of class ProductBook from MailOrderHouse.h
#include "MailOrderHouse.h"

int main()
{
// create ProductBook object myProductBook and
// pass products to constructor
ProductBook myProductBook( "Totals" );

myProductBook.determineCharges(); // find total charges
return 0; // indicate successful termination
} // end main

This post has been edited by James Bond C++ Spy: 05 October 2007 - 08:36 PM

Was This Post Helpful? 0
  • +
  • -

#4 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 45
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: Using Header files and creating objects of a class

Posted 07 October 2007 - 12:51 PM

first off, remember to use the code tags, and especially to separate different files in different code blocks.

there's a big problem here:

string ProductBook::getProduct1()
string ProductBook::getQuantity1()

string ProductBook::getProduct2()
string ProductBook::getQuantity2()

string ProductBook::getProduct3()
string ProductBook::getQuantity3()

string ProductBook::getProduct4()
string ProductBook::getQuantity4()

string ProductBook::getProduct5()
string ProductBook::getQuantity5() {	

	return Product1;
	return Quantity1;
	return Product2;
	return Quantity2;
	return Product3;
	return Quantity3;
	return Product4;
	return Quantity4;
	return Product5;
	return Quantity5;
}


this isn't a function, nor is it 10 different functions. you need to specify the function body and return value for each function separately:
string ProductBook::getProduct1() {
	return Product1;
}
string ProductBook::getQuantity1() {
	return Quantity1;
}
...


the same problem exists for your inputProduct1() and inputQuantity1() functions, except that these are also missing declarations in the class body.

your function displayProductReport() uses 5 undeclared variables Total1...Total5. if these are supposed to be data members in your class, you need to declare them in your class.

hope these changes help get you started.

-jjh
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1