while (y==1)
 { 
	 float discount,addcost;

	 pricetopay=calpricetopay(ty,pricetopay,addcost,discount);
	 cout<<endl;
	 cout<<endl;
	 cout<<"\t\t\tTOY TOY Shoppie"<<endl;
	 cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
	 cout<<endl;
	 cout<<"Name of item:"<<ty.name<<endl;
	 cout<<"Price of item:"<<ty.price<<endl;
	 cout<<"Made in:"<<ty.madein<<endl;
	 cout<<"Manufactured Date:"<<ty.date.d<<"/"<<ty.date.m<<"/"<<ty.date.y<<endl;
	 cout<<"Price to be paid: "<<pricetopay<<endl;
	
	 cout<<"Do you want to proceed with another transaction?"<<endl;
	 cout<<"If yes, Please type '1'. If no, please press 2 to quit."<<endl;
	 cin>>y;

}

Above is the source code i typed...I cant find any errors in there...But the program is repeating infinitely....
I wonder is there anything to be changed ?

Recommended Answers

All 12 Replies

How is y declared?
You should perhaps check cin to make sure that no error has occured on it.

the y is declared in this way:

int y;

as in?

How is y declared?
You should perhaps check cin to make sure that no error has occured on it.

I agree, it sounds like the stream is corrupted. An error trap may be advisable.

@OP:
Based on what I see, there must be some other input previous to this. It's not unusual for characters to be left in an input stream after an input depending on the type of input and how the input was collected. When you take the input for 'y', it's probably grabbing a lingering char from the stream and corrupting the stream.

To check for stream corruption, create a loop around your prompts and input statements. You would then check the return from the function cin.good() . If it returns false, the stream is corrupted, you will have to reset the stream with cin.clear() and re-start the loop.

int inputVar = 0;
do {
  cin.clear()
  cout << "This is my prompt for input: ";
  cin >> inputVar;
} while (!cin.good());

misread post

erm, may I know what does it mean by "stream corrupted"?
I tried using the input u gave me and placed it around the loop...when i type the input, the output was the same...straight away printing infinite output...

Make sure you're initializing y to 1 before you start the while loop or make it a do while loop. I didn't see you mention anywhere that y was being assigned 1 after you declared it

i did declared as below

int y=1;

at the beginning of the main function.
sry for posting mistake just now ><

If your input is expecting a number and you enter a character instead, it can "corrupt", or cause and error in, the input stream, making it unusable. This means that the data in the stream is not correct and the stream needs to be reset.

To check for an error, you use the member function good(). If good() returns false, there is an error. You then use the member function clear() to reset the stream's error flags and make the stream usable again.

The code I provided is only an example, you will have to modify it to fit your program.

Your loop is dependent on user input to determine if it must continue or not, if the stream gets corrupted, it causes the value to not be stored to the variable and remains in the stream. If the character in the stream is not removed and the stream reset, an input operation can not occur and the value in your variable does not get changed. Since it is already '1', the loop condition remains true and does not become false, resulting in an infinite loop.

Your code is working fine for me, maybe run the debugger and watch your variable y as you step through.

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
struct mydate
{
 int d;
 int m;
 int y;
};
struct toy
{ 
 char name[20];
 float price;
 string madein;
 mydate date;
 };
float calpricetopay(toy,float,float,float);

int main ()
{
  int q=1;
  toy ty;
  float pricetopay;
  
 
 	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(2);

	cout<<"\t\t\tWelcome to the TOY TOY Shoppie!"<<endl;
	cout<<"********************************************************************************"<<endl;
	cout<<endl;

 do{ 
	 float discount,addcost;

	 	
	
	cout<<"Please enter the NAME of item: ";
	cin.get(ty.name,20);
	cout<<"Please enter the PRICE of item: RM ";
	cin>>ty.price;
	cout<<"Please enter the PLACE where the toy is made : ";
	cin>>ty.madein;
	cout<<"Please enter the MANUFACTURED DATE of item (dd/mm/yyyy):"<<endl;;
	cout<<"Year:";
	cin>>ty.date.y;
	cout<<"Month:";
	cin>>ty.date.m;
	cout<<"Day:";
	cin>>ty.date.d;

 

	 pricetopay=calpricetopay(ty,pricetopay,addcost,discount);
	 {
	 cout<<endl;
	 cout<<endl;
	 cout<<"================================================================"<<endl;
	 cout<<"\t\t\tTOY TOY Shoppie"<<endl;
	 cout<<"\t\t\t<<<Invoice>>>"<<endl;
	 cout<<"================================================================="<<endl;
	 cout<<endl;
	 cout<<"Name of item: "<<ty.name<<endl;
	 cout<<"Price of item: RM"<<ty.price<<endl;
	 cout<<"Made in: "<<ty.madein<<endl;
	 cout<<"Manufactured Date: "<<ty.date.d<<"/"<<ty.date.m<<"/"<<ty.date.y<<endl;
	 cout<<"Price to be paid: "<<pricetopay<<endl;
	 cout<<"_________________________________________________________________"<<endl;
	 cout<<endl;
	 }
	 
	  
		 cout<<"<Do you want to proceed with another transaction?>"<<endl;
	 cout<<"<If yes, Please type 1. If no, please type 2 to quit.>"<<endl;
	 cin>>q;
	 
}	while (q==1);
 
 
 return 0;
}

float calpricetopay(toy ty,float pricetopay,float addcost,float discount)
{
   string country( "malaysia" );
		{
		if (ty.madein==country)
			addcost=0;
		else
			addcost=10;
	}
			{
	if (ty.date.y<2011) 
		discount=float(10)/100;
	else
		discount=0;
	}

 pricetopay=ty.price*(1-discount)+addcost;
 
 return pricetopay;
 
}

here is source code for the loop i am doing...Its still repeating infinitely~~

Enter 2 to quit. Works fine.

Okay, i have your code working, just had to make a few small modifications, if you're still interested I can go over them with you

Other than pricetopay,addcost, and discount not being initialized, it worked fine. Assign values to pricetopay,addcost, and discount before you send them to calcpricetopay().

What kind of input are you entering for the toy data when you are testing? I still think you are corrupting your input stream....

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.