Create a class called Invoice with the properties (Part number, Part Description, Quantity and Price).
Create appropriate methods and data types.
Use the class Invoice and create an array of Invoice objects (Part number, Part Description, Quantity and Price) initialized as shown below:

// initialize array of invoices
Invoice[] invoices = {
new Invoice( 83, "Electric sander", 7, 57.98M ),
new Invoice( 24, "Power saw", 18, 99.99M ),
new Invoice( 7, "Sledge hammer", 11, 21.5M ),
new Invoice( 77, "Hammer", 76, 11.99M ),
new Invoice( 39, "Lawn mower", 3, 79.5M ),
new Invoice( 68, "Screwdriver", 106, 6.99M ),
new Invoice( 56, "Jig saw", 21, 11M ),
new Invoice( 3, "Wrench", 34, 7.5M )
};
Write a console application that displays the results:

a) Use a Selection sort to sort the Invoice objects by PartDescription in ascending order.
b) Use an Insertion sort to sort the Invoice objects by Price in descending.
c) Calculate the total amount for each invoice amount (Price * Quantity)
d) Display the description and totals in ascending order by the totals.
Sorted by description ascending order:

83 Electric sander 7 $57.98
77 Hammer 76 $11.99
56 Jig saw 21 $11.00
39 Lawn mower 3 $79.50
24 Power saw 18 $99.99
68 Screwdriver 106 $6.99
7 Sledge hammer 11 $21.50
3 Wrench 34 $7.50
Sorted by price in descending order:

24 Power saw 18 $99.99
39 Lawn mower 3 $79.50
83 Electric sander 7 $57.98
7 Sledge hammer 11 $21.50
77 Hammer 76 $11.99
56 Jig saw 21 $11.00
3 Wrench 34 $7.50
68 Screwdriver 106 $6.99

Here is what I have so far. I am getting so many errors though. I am new to c++ so please explain or help in any way possible.

#include<iostream.h>
int main()
{
class Invoice
{
int p,r,s;
char[] q=new char();
Invoice(int p,char q,int r,int s)
{
this.p=p;
this.q=q;
this.r=r;
this.s=s;
}
};
Invoice[] invoices = {
new Invoice( 83, "Electric sander", 7, 57.98M ),
new Invoice( 24, "Power saw", 18, 99.99M ),
new Invoice( 7, "Sledge hammer", 11, 21.5M ),
new Invoice( 77, "Hammer", 76, 11.99M ),
new Invoice( 39, "Lawn mower", 3, 79.5M ),
new Invoice( 68, "Screwdriver", 106, 6.99M ),
new Invoice( 56, "Jig saw", 21, 11M ),
new Invoice( 3, "Wrench", 34, 7.5M )
};
void SelectionSort(Invoice &invoice)
{
int i, j, first;
char temp;
int invoiceLength =invoice.length( );
for (i= invoiceLength - 1; i > 0; i--)
{
first = 0; // initialize to subscript of first element
for (j=1; j<=i; j++) // locate smallest between positions 1 and i.
{
if (invoice[j].q < invoice[first].q)
first = j;
}
temp = invoice[first].q; // Swap smallest found with element in position i.
invoice[first].q = invoice[i].q;
invoice[i].q = temp;
}
for(i=0;i<invoiceLength;i++)

cout<<" "<<invoice[i].p<<" "<<invoice[i].q<<" "<<invoice[i].r<<" "<<invoice[i].s;
return;
}
void insertion_sort()
{
int key,i;
for(int j=1;j<invoice.length;j++)
{
key=invoice[j].s;
i=j-1;
while(invoice[i].s>key && i>=0)
{
invoice[i+1].s=invoice[i].s;
i--;
}
invoice[i+1].s=key;
}
}
for(i=0;i<invoice.length;i++)
cout<<" "<<invoice[i].p<<" "<<invoice[i].q<<" "<<invoice[i].r<<" "<<invoice[i].s;
}
void total()
{
for(i=0;i<invoice.length;i++)
cout<<" "<<invoice[i].p*invoice[i].r<<endl;
}

return 0;

}

Recommended Answers

All 3 Replies

Format your code.

Explain what? You didn't tell us anything we can explain.

You have tried to declare all your functions inside main. You can not declare functions inside functions and while you can declare classes inside functions you probably don't want to here because then the class wont be visible to any other functions.

You code layout should have this structure

#include statements

Declaration of functions (SelectionSort, insertion_sort, total)

Definition of classes (Invoice in this case)

int main()
{
  // Your code for main here

  return 0;
}

Defintion of other functions such as (SelectionSort, insertion_sort, total)

Wow, that is truly a mess!

Here are some of the problems I've spotted from an initial look (after pasting into a text editor and auto-indenting the code):
1. Your class and functions have all been declared and defined inside main.
I recommend that you take the class definition and the function definitions and move them outside of main.
So your program should be structured something like this:

// #includes
...

// class definition here
...

// function definitions here
...

// main function
int main()
{
// Create your array of Invoice objects
// call selection sort function and display sorted array
// call insertion sort function and display sorted array
// etc.....
}
  1. Inside your Invoice class, the char array q is unsized. You might want to consider giving it a size, or making it a char*, or a const char* (if use of std::string is out of the question). Also you shouldn't be using new here, if you are going to use new anywhere it should be in the constructor when that member is initialised. Also bear in mind, if you are going to use new in the constructor for your class, you should also have a destructor which deletes that member using delete[] if it is an array!

  2. As your class currently stands: Because you haven't marked any public:, private: or protected: sections in your class, everything is implicitly private. Meaning that your constructor is private. Therefore there is no way of constructing an instance of your class. So you should at least put the constructor into a section marked public:!

  3. Inside your class constructor, you should be aware that the this pointer is a pointer and as such, requires the use of arrow notation rather than dot notation. (So you should be using this-> rather than this.)

  4. In your constructor, the line this->q=q; (or in your actual code this.q=q;) will not work if q is a char array. Arrays cannot be assigned like this; you need to copy each element individually. However, if Invoice.q (and the corresponding q parameter in the constructor) was a char* or const char*, this line would be OK.

  5. Your selection_sort function takes a reference to a single Invoice object as a parameter, did you mean to pass a reference to an array of Invoice objects instead?

  6. In both of your sorting functions, you refer to invoice.length() and invoice.length, but your invoice class has no length() member function and/or no length member variable.

  7. The compiler will complain about all calls your program makes to cout because cout is in the std:: namespace not the global namespace. You can resolve this by explicitly calling std::cout each time, or by adding a using statement after your includes. e.g. using std::cout;

  8. Your insertion_sort and total functions do not take any parameters, should these take a reference to the array?

There are probably a number of other errors in your code, but those are the most obvious ones I could see!

Edit: Also the final parameter to your constructor, (the price) should be a float or double as it is a decimal value, not an integer... Although, what's with the M at the end of the values for the 4th parameter in your array initialisation code? should that be an f to make it a float? M doesn't make any sense! 57.98 would be a double, 57.98f would be a float, but 57.98M would just throw up all kinds of compiler errors!

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.