Hello, newbie here
I need some help with my programming assignment, I've gotten most of it but I'm having problems with what my for loop is displaying. My teacher wanted us to create a program to act as a cash register. I'm supposed to display the output of each transaction after the user has entered all their items. That's where I'm having the problem because when the program displays the stuff in the for loop, it's all gibberish. Here's what my teacher sent me:
Ok the way you have the program set up you are using a pointer to point to each element of the structure array. I noticed that you were not incrementing the pointer so I add a line of code at the end that does that. Put I know that once you start incrementing the pointer you need to remember where the first element is. So I added some code to create a temporary pointer that you can store the original address. Then when you are ready to read the data back out of the array you just restore the pointer to that first address.
I don't understand how to go about restoring the pointer to the first address. Here's the source code, and I included a link that shows the program output so you'll see what I'm talking about when I said the for loop is displaying gibberish. Ignore my comments. Any help would be appreciated, thank you very much
Code:
#include <iostream>
#include <iomanip>
using namespace std;
struct Sales
{
//variables listed below
float price;
float quantity;
float discount;
float tax;
float total;
float totaltax;
float totaldiscount;
};
//function prototypes listed below
void get_name(char name[]);
Sales get_info();
int main()
{
Sales* SalesList;
Sales* Temp;
int itemsold=0;
float subtotal=0;
float totaldiscount=0;
float granddiscountedprice=0;
float grandtax=0;
float grandtotal=0;
float priceb4discount;
float priceafterdiscount;
int choice;
char name1[50];
int index;
cout << "Welcome to the Akpala SuperStore!! This program will determine the cost\n";
cout << "of the items you have purchased...";
cout << endl;
cout << endl;
cout <<"----------------------------------";
cout << endl;
do
{
SalesList = new Sales[10];
Temp = SalesList;
get_name(name1);//Function call so user can enter name of item
*SalesList = get_info();
itemsold+=SalesList->quantity;
totaldiscount+=SalesList->totaldiscount;
priceb4discount=(SalesList->price*SalesList->quantity); //Calculates subtotal, before discount and tax
subtotal+=priceb4discount;
priceafterdiscount=(priceb4discount-SalesList->discount);
granddiscountedprice+=priceafterdiscount;
grandtax+=SalesList->totaltax;
grandtotal+=SalesList->total;
cout << endl;
cout << endl;
cout << "To enter another item, press 1, otherwise press 2: ";
cin >> choice;
cout << endl;
SalesList++;
}while(choice!=2);
for (index=0; index<10; index++)
{
cout << "You purchased " << SalesList[index].quantity << " items at a price of $ " << SalesList[index].price << endl;
cout << "each, at a discount of " << SalesList[index].discount << " %" << " and at a tax " << SalesList[index].tax << endl;
cout << endl;
}
cout << fixed << showpoint << setprecision(2);
cout << endl;
cout << "Here is your receipt:";
cout << endl;
cout << "--------------------------------------";
cout << endl;
cout << "Items sold: " << itemsold << endl;
cout << "Subtotal: $ " << subtotal << endl;
cout << "Total Discount: $ " << totaldiscount << endl;
cout << "Total Tax: $ " << grandtax << endl;
cout << "Total Cost: $ " << grandtotal << endl;
cout << endl;
cout << "Thanks for shopping at the Akpala SuperStore! Please come back soon";
cout << endl;
return 0;
}
//Definition of get_name function
void get_name(char name[])
{
cout << "Please enter the name of the item: " ;
cin >> name;
}
//Definition of get_info function
Sales get_info()
{
Sales items;
cout << "Please enter the price of the item: $ ";
cin >> items.price;
while (items.price < 0 || items.price == 0)
{
cout << "Invalid, please enter a positive amount. ";
cin >> items.price;
}//Validaton of input
cout << "Please enter the quantity of the item: ";
cin >> items.quantity;
while (items.quantity < 0 || items.quantity == 0)
{
cout << "Invalid, please enter a positive amount. ";
cin >> items.quantity;
}//Validation of input
cout << "Please enter a discount: ";//User enters discount
cin >> items.discount;
while (items.discount < 0 || items.discount > 100)
{
cout << "Invalid, please enter an amount greater than 0 or less than 100: ";
cin >> items.discount;
}//Validation of input
items.totaldiscount = (items.price*items.quantity*(items.discount/100)); //Calculation of total discount
cout << "Please enter a tax amount, enter 0 if not taxable: "; //User enters tax, if any
cin >> items.tax;
items.totaltax = (items.price)*(items.tax/100); //Calculation of total tax
items.total= (items.price*items.quantity)-(items.totaldiscount) + (items.totaltax); //Total amount of sales
return items;
}
http://www.geocities.com/chris5823/test.txt