I have created a program allows you to input the amount of items purchased, and then input the price of each item and the sales tax. It then creates a receipt which totals all the prices and sales tax.
Now I want to be able to list the items and their prices in the sales receipt as well. What would I have to do so lets say you pick 5 items and type in 5 prices and it give you the sales receipt, before it tells you the total, I want it to list the 5 items so looks like this:
I want to number my items. I want it to ask "Input sales item 1" "Input sales item 2" "Input sales item 3" and then on the receipt I want it to do the same thing:
Sale item 1: $(price)
Sale item 2: $(price)
Sale item 3: $(price)
I want those number to show up. I can't find any tutorials or anything to read that teaches how to number items either.
cout << "** Sales Item " << count + 1 << ": $" << setw(9) << setprecision(2) << price[count] << " **" << endl;
Ended up doing it that way and it works like a charm!
I found one big issue! I cannot input decimal value for the price of an item. I know this is because price is set to integer. I need to be able to use decimal values for the price. Previously I used double for price originally but to create the dynamic array, I changed it to integer.
What options are there? Or what can I do? The entire program just stops working as soon as I input decimals into the sales item price such 10.50.
double * price;
price= new (nothrow) double[items];
Don't forget to delete[] price; after your output is done. Currently, each subsequent run is allocating new memory without freeing previously allocated memory, which will continually grow over time until the user runs out of system memory.
yeah i got the incrementing working. I need to learn to completely follow through on my code when making edit. I had changed it to double in the variable but failed to recognize to change it to double[items].
#include <iostream>
#include <iomanip>
usingnamespace std;
int main()
{
char rerun;
do //The do-while for a program rerun option
{
// Declare variables: counter, items, sales items, sales total, sales tax, and grand total
int items, count;
double * price;
double total = 0, percent, g_t, tax;
// Input information
cout<<"How many sales items do you have? :";
cin>>items;
price= new (nothrow) double[items];
if (price == 0)
cout << "error";
else
{
for (count = 0; count < items; count++)
{
cout<<"Enter the value of the sales item " << count + 1 << ": $";
cin>>price[count];
total = total + price[count];
}
}
cout << endl << endl;
cout<<"Enter in the sales tax percentage: ";
cin>>percent;
tax = total * (percent/100);
g_t = tax + total;
cout << endl << endl;
cout << "********************************************" << endl;
cout << "******** S A L E S R E C E I P T ********" << endl;
cout << "********************************************" << endl;
cout << "** **" << endl;
cout << "** **" << endl;
for (count = 0; count < items; count++)
cout << "** Sales Item " << count + 1 << ": $" << setw(9) << setprecision(2) << price[count] << " **" << endl;
delete[] price;
cout << "** **" << endl;
cout << "** **" << endl;
cout << "********************************************" << endl;
cout << "** **" << endl;
cout << "** **" << endl;
cout << setiosflags(ios::fixed) << setprecision(2);
cout << "** Total Sales: $" << setw(9) << total << " **" << endl;
cout << "** Sales Tax: $" << setw(9) << tax << " **" << endl;
cout << "** ----------- **" << endl;
cout << "** Grand Total: $" << setw(9) << g_t << " **" << endl;
cout << "** **" << endl;
cout << "** **" << endl;
cout << "********************************************" << endl;
cout << endl << endl;
cout<<"Do you want to run this program again? (y/n)";
cin>>rerun;
}
while (rerun == 'y' || rerun == 'Y');
system ("PAUSE");
return 0;
} //End Main Function
On the first run of the program, entering decimal values such as 1.50 will get rounded to 2 on sales receipt. However, if you select Y and run the program again, it properly displays the decimal place.
I have one last component of this program now. I want to be able to name the sales item such as book, pen, car, etc. and then in the sales receipt, instead of it listing the items as Sales Item 1, Sales Item 2, etc. I want it to be listed (name of item): Price so like Book: Price, Pen: Price, and etc.
I know this goes back into memory and storing it all but it seems a little more complex.
#include <iostream>
#include <iomanip>
usingnamespace std;
int main()
{
char rerun;
do //The do-while for a program rerun option
{
// Declare variables: counter, items, sales items, sales total, sales tax, and grand total
int items, count;
double * price; // creates the array to store the price
double total = 0, percent, g_t, tax;
string * names = new string[items];
// Input information
cout<<"How many sales items do you have? :";
cin>>items;
// Input and storage of items names
names = new (nothrow) string[items];
if (names == 0)
cout << "error names";
else
{
for (count = 0; count < items; count++)
{
cout << "Enter the name of the sales item " << count + 1 << ": ";
cin >> names[count];
}
}
// Input and storage of items prices
price = new (nothrow) double[items];
if (price == 0)
cout << "error";
else
{
for (count = 0; count < items; count++)
{
cout<<"Enter the value of the sales item " << count + 1 << ": $";
cin>>price[count];
total = total + price[count];
}
}
cout << endl << endl;
cout<<"Enter in the sales tax percentage: ";
cin>>percent;
tax = total * (percent/100);
g_t = tax + total;
cout << endl << endl;
cout << "********************************************" << endl;
cout << "******** S A L E S R E C E I P T ********" << endl;
cout << "********************************************" << endl;
cout << "** **" << endl;
cout << "** **" << endl;
cout << setiosflags(ios::fixed) << setprecision(2);
for (count = 0; count < items; count++)
cout << "** Sales Item " << count + 1 << ": $" << setw(9) << price[count] << " **" << endl;
delete[] price; // clears the memory
cout << "** **" << endl;
cout << "** **" << endl;
cout << "********************************************" << endl;
cout << "** **" << endl;
cout << "** **" << endl;
cout << "** Total Sales: $" << setw(9) << total << " **" << endl;
cout << "** Sales Tax: $" << setw(9) << tax << " **" << endl;
cout << "** ----------- **" << endl;
cout << "** Grand Total: $" << setw(9) << g_t << " **" << endl;
cout << "** **" << endl;
cout << "** **" << endl;
cout << "********************************************" << endl;
cout << endl << endl;
cout<<"Do you want to run this program again? (y/n)";
cin>>rerun;
}
while (rerun == 'y' || rerun == 'Y');
system ("PAUSE");
return 0;
} //End Main Function
Errors I receive
:\Users\Neel P\Documents\Classes\Summer 2011\CMPSC 101\HW6\salesname.cpp(26) : warning C4291: 'void *__cdecl operator new(unsigned int,const struct std::nothrow_t &)' : no matching operator delete found; memory will not be freed if initialization throws an exception
c:\program files (x86)\microsoft visual studio\vc98\include\new(36) : see declaration of 'new' salesname.cpp(34) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<
char> >' (or there is no acceptable conversion)
The warning is self explanatory and regards my previous post about memory, your error is a result of not including the string header #include <string> .
Also,
1 2 3 4 5 6
int items, count;
string * names = new string[items]; // items = ???
// Input information
cout<<"How many sales items do you have? :";
cin>>items;