C++ cash register program w/ loop

Program compiles but crashes when run

Page 1 of 1

4 Replies - 7676 Views - Last Post: 27 October 2008 - 01:19 PM Rate Topic: -----

#1 mistymoon1966  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 17-May 07

C++ cash register program w/ loop

Posted 26 October 2008 - 02:00 PM

I am having a problem with an assignment that is due tomorrow, the program compiles great but crashes when it gets to the loop. I am at wits end, could someone please tell me how to fix the program so it not only compiles but runs, thanks. Any help would be appreciated, thanks. The code is below


//Week 3 Project//
//October 25, 2008//
//The purpose of this program is to create a cash register program in C++ that includes a loop with 100 items.//

#include <iostream>				//Basic input and output library//
#include <iomanip>				//Currency output library//
using namespace std;
#define taxrate 10/100;			//definition of taxrate//

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

int main()
{
	char firstname [30];		//variable definitions//
	char lastname [30];
	const int MAX = 100;
	double *item = NULL;
	int counter = 0;
	double payment;
	double subtotal;
	double tax;
	double total;
	double change;
	
	cout << "Hello, this is team B's week 3 cash register project." << endl;

	//Code for user input of name//
	cout << endl
		 << "Please enter your first name: "; 
	cin >> firstname;

	cout << endl
		 << "Please enter your last name: "; 
	cin >> lastname;

	while (counter<=MAX)		//loop for the 100 items//
	{
		if (*item!=-1)
			cout << endl;
		{

			cout << endl
				 << "Please enter the price of the item: "; 
			cin >> item[counter];
			++counter;
		}
	}  //end loop//							

		subtotal = item[MAX];		//calculations//
		tax = subtotal* taxrate;
		total = subtotal + tax;
		change = payment - total;

		cout << endl
			 << "The subtotal for your order is $ " 
			 << setiosflags(ios::fixed)<<setprecision(2) << subtotal
			 << endl;

		cout << endl
			 << "The tax for your order is $ " 
			 << setiosflags(ios::fixed)<<setprecision(2) << tax
			 << endl;

		cout << endl
			 << "The total for your order is $ "
			 << setiosflags(ios::fixed)<<setprecision(2) << total 
			 << endl;

		cout << endl
				 << "Please enter the amount of payment,$ "; 
		cin >> payment;

		cout << endl
			 << "The change from the order payment is $ " 
			 << setiosflags(ios::fixed)<<setprecision(2) << change
			 << endl;	 
		
return 0;		//return to operating system//
}




Is This A Good Question/Topic? 0
  • +

Replies To: C++ cash register program w/ loop

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,659
  • Joined: 23-August 08

Re: C++ cash register program w/ loop

Posted 26 October 2008 - 03:45 PM

The problem is that you're accessing a NULL pointer (item). You either need to allocate memory for that or get rid of it. Fact is, you don't need an array of items unless you need to keep a list of the various prices. All you really need to do is add the price of the item to the subtotal if it's not -1.
prompt user for item price
while input > 0 and counter < max
	add item amount to subtotal
	prompt user for item price

Was This Post Helpful? 0
  • +
  • -

#3 mistymoon1966  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 17-May 07

Re: C++ cash register program w/ loop

Posted 27 October 2008 - 07:45 AM

View PostJackOfAllTrades, on 26 Oct, 2008 - 03:45 PM, said:

The problem is that you're accessing a NULL pointer (item). You either need to allocate memory for that or get rid of it. Fact is, you don't need an array of items unless you need to keep a list of the various prices. All you really need to do is add the price of the item to the subtotal if it's not -1.
prompt user for item price
while input > 0 and counter < max
	add item amount to subtotal
	prompt user for item price


Thanks I got the program to work, now I have one more issue, all the calculations are correct except for the change after the user inputs the amount tendered. Below is the code. Please help, as I am getting a little nervous here as the assignment is due today, thanks.

[code]

//Week 3 Project//
//October 25, 2008//
//The purpose of this program is to create a cash register program in C++ that includes a loop with 100 items.//

#include <iostream> //Basic input and output library//
#include <iomanip> //Currency output library//
using namespace std;
#define taxrate 10/100; //definition of taxrate//

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

int main()
{
char firstname [30]; //variable definitions//
char lastname [30];
const int MAX = 100;
int counter = 0;
double payment = 0;
double subtotal = 0;
double tax = 0;
double total = 0;
double change = 0;
double items[MAX];

//Welcome and Title//
cout << "Hello, this is team B's week 3 cash register project." << endl;
//Code for user input of name//
cout << endl
<< "Please enter your first name: ";
cin >> firstname;
cout << endl
<< "Please enter your last name: ";
cin >> lastname;
cout << endl;
cout << endl
<< "Enter -1 when all items have been entered.\n";

//Loop//
while (counter<MAX && items[counter-1]!=-1)
{
cout << endl
<<"Please enter the price of your item: ";
cin >>items[counter];
++counter;
}

for(int i = 0; i < counter-1; i++)
{

subtotal = subtotal + (items[i]);

}

//calculations//
tax = subtotal * taxrate;
total = subtotal + tax;
change = payment - total;

cout << endl
<< "\n\n\nThe subtotal for your order is: $ "
<< setiosflags(ios::fixed)<<setprecision(2) << subtotal
<< endl;

cout << endl
<< "The tax for your order is: $ "
<< setiosflags(ios::fixed)<<setprecision(2) << tax
<< endl;

cout << endl
<< "The total for your order is: $ "
<< setiosflags(ios::fixed)<<setprecision(2) << total
<< endl;

cout << endl
<< "Please enter the amount of payment: $ \n";
cin >> payment;

cout << endl
<< "The change from the order payment is: $ "
<< setiosflags(ios::fixed)<<setprecision(2) << change
<< endl;

return 0; //return to operating system//
}

[code]
Was This Post Helpful? 0
  • +
  • -

#4 jderecho  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 26
  • Joined: 23-July 08

Re: C++ cash register program w/ loop

Posted 27 October 2008 - 08:20 AM

Just move the calculation for the amount of change to a point after you get the amount of payment, and it will work. You might also add some code to insure that the payment is greater than the amount owed.

Juan
Was This Post Helpful? 0
  • +
  • -

#5 mistymoon1966  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 17-May 07

Re: C++ cash register program w/ loop

Posted 27 October 2008 - 01:19 PM

View Postjderecho, on 27 Oct, 2008 - 08:20 AM, said:

Just move the calculation for the amount of change to a point after you get the amount of payment, and it will work. You might also add some code to insure that the payment is greater than the amount owed.

Juan


Thank you so much, I moved the payment calculation below the amount of payment and added code and the program works like a dream, thanks a million. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1