Hello members of Daniweb :)
I am a frosh IT student and I need little help with this :)
ok so the program is basically a simple cashier program.
you enter a code for an item. you buy it compute for the change etc. etc.
now my problem is with error handling.. where in the program should not engage in
an endless loop if for example a a letter instead of a number was entered.
or should that the item number that the user have input was not on the list.
or basically spaces, symbols, and anything non-numeric is not allowed.
Im having problems where and how could i place in the codes :(
please help me...
thanks in advance :D

#include <iostream.h>
#include <stdlib.h>

int main()
{	//variables------------------------------------

	double code,quan,tmoney,change,price=0,pricecurrent, fract;
	int wholenumb, thou, fhundred, thundred, ohundred, ften, tten, ten, five, one, c25, c10, c5, convert; 
	char ans = 'Y';
	//item list------------------------------------
	cout<<"=====================ITEMLIST========================"<<endl;
	cout<<"Item Code\t\tDescription\t\tPrice"<<endl;
	cout<<"1\t\t\tCoke 8oz\t\t9.00"<<endl;
	cout<<"2\t\t\tRoyal 8oz\t\t12.00"<<endl;
	cout<<"3\t\t\tSprite 8oz\t\t9.00"<<endl;
	cout<<"4\t\t\t7-up 8oz\t\t9.00"<<endl;
	cout<<"5\t\t\tCheese Cake\t\t25.00"<<endl;
	cout<<"6\t\t\tSweet Tarro Pie\t\t20.00"<<endl;
	cout<<"7\t\t\tMamon\t\t\t15.00"<<endl;
	cout<<"8\t\t\tBlueberry Muffins\t45.00"<<endl;
	cout<<"9\t\t\tChocolate Muffins\t40.00"<<endl;
	cout<<"10\t\t\tEnsaimada\t\t32.10"<<endl;
	cout<<"======================================================"<<endl;
	cout<<"\n\n\n\n";
	
	while(code!=0&&quan!=0)//loop would stop when the user entered both zero :)
	{

	cout<<"Item Code: ";
	cin>>code;
	//change log----------------------------
	if (code==1)
	{cout<<"Coke 8oz Price: 9.00";
	code=9.00;}
	else if(code==2)
	{cout<<"Royal 8oz Price: 12.00";
	code=12.00;}
	else if(code==3)
	{cout<<"Sprite 8oz Price: 9.00";
	code=9.00;}
	else if(code==4)
	{cout<<"7-up 8oz Price: 9.00";
		code=9.00;}
	else if(code==5)
	{cout<<"Cheese Cake Price: 25.00";
	code=25.00;}
	else if(code==6)
	{cout<<"Sweet Tarro Pie Price: 20.00";
	code=20.00;}
	else if(code==7)
	{cout<<"Mamon Price: 15.00";
	code=15.00;}
	else if(code==8)
	{cout<<"Blueberry Muffins Price: 45.00";
	code=45.00;}
	else if(code==9)
	{cout<<"Chocolate Muffins Price: 30.00";
	code=40.00;}
	else if(code==10)
	{cout<<"Ensaimada Price: 32.10";
	code=32.10;}
	else
		cout<<"Thanks Please Wait";
	cout<<"\n\n";
	

	//computation and output of price----------------
	cout.precision(2);
	cout.setf(ios::fixed,ios::floatfield); // floatfield set to fixed

	cout<<"\tQuantity: ";
	cin>>quan;
	pricecurrent = code*quan;
	price = price + pricecurrent;
	}

	cout<<"The total ammount is:"<<price<<endl;
	cout<<"Recieved: ";
	cin>>tmoney;
	// change & change breakdown~------------------------------------------------------
	cout.precision(2);
	cout.setf(ios::fixed,ios::floatfield); // floatfield set to fixed
	change = tmoney-price;
	cout<<"Change:\t\t"<<change<<endl;
	cout<<"================Change Breakdown====================="<<endl;
	wholenumb = (int) change;
	fract = change - wholenumb;
	cout<<"Peso: "<<wholenumb<<"\tCentavo: "<<fract<<endl;
	cout<<"================Expanded============================="<<endl;
	
	thou = wholenumb/1000;
	cout<<"P 1,000.00\t"<<thou<<"\t"<<thou*1000<<endl;
	
	fhundred = (wholenumb%1000)/500;
	cout<<"P 500.00\t"<<fhundred<<"\t"<<fhundred*500<<endl;
	
	thundred = (wholenumb%500)/200;
	cout<<"P 200.00\t"<<thundred<<"\t"<<thundred*200<<endl;
	
	ohundred = (wholenumb%200)/100;
	cout<<"P 100.00\t"<<ohundred<<"\t"<<ohundred*100<<endl;
	
	ften = (wholenumb%100)/50;
	cout<<"P 50.00\t\t"<<ften<<"\t"<<ften*50<<endl;
	
	tten = (wholenumb%50)/20;
	cout<<"P 20.00\t\t"<<tten<<"\t"<<tten*20<<endl;
	
	ten = (wholenumb%20)/10;
	cout<<"P 10.00\t\t"<<ten<<"\t"<<ten*10<<endl;
	
	five = (wholenumb%10)/5;
	cout<<"P 5.00\t\t"<<five<<"\t"<<five*5<<endl;
	
	one = (wholenumb%5)/1;
	cout<<"P 1.00\t\t"<<one<<"\t"<<one*1<<endl;
	
	convert = fract*100;
	
	c25 = convert/25;
	cout<<"P 0.25\t\t"<<c25<<"\t"<<c25*.25<<endl;
	
	c10 = (convert%25)/10;
	cout<<"P 0.10\t\t"<<c10<<"\t"<<c10*.10<<endl;
	
	c5  = (convert%10)/5;
	cout<<"P 0.05\t\t"<<c5<<"\t"<<c5*.05<<endl;
	
	cout<<"====================================================="<<endl;
	//breakdown by words :D--------------*@_@*--------------------------------------
        //we still have to print out the change in words.. but I think i can manage :)
	cout<<"Do You Want Another Transaction (Y/N):  ";
	cin>> ans;
	if(ans == 'y' || ans == 'Y')
	{ main();
	   price = 0;
	   pricecurrent = 0;
		cout<<endl;}
	else
		cout<<"Thanks for Shopping"<<endl;

	return 0;
}

Recommended Answers

All 3 Replies

Basically, if you are dealing with error handling which is not from your own program but a user input, do it right after a user enters a value. Each time an input is from a user, check it right away to see whether or not it is correct data type or format you want. If not, either keep looping until you get the right format or display an error message and exit.

At line 30, 72, and 79, you take an input from a user. These lines, instead of using the variable right away in your next line, you need to add error handling here.

ohhh..
thanks Taywin and mitrmkar
let me just try your advices and would be back :)

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.