Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 4th, 2006, 1:57 PM   #1
lamefif
Newbie
 
lamefif's Avatar
 
Join Date: Mar 2005
Posts: 16
Rep Power: 0 lamefif is on a distinguished road
Looping effect, when entering string in an int variable

when i enter other then int in count it just keeps going
is there any way of stoping the user from entering string, i mean with no side effects.
check out the code comments
while (action!="q"){//ok action is ""
       
            cout <<"--------------------------------------------------------------------"<<endl;
            cout << "Enter \"q\" to exit the program" <<endl;
            cout << "Enter \"s\" to display stock information" <<endl;
            cout << "Enter \"c\" to display client information" <<endl; //bla bla 
            cout << "To sell pruducts enter \"sell\" and follow the instructions" <<endl;
            cout <<"--------------------------------------------------------------------"<<endl;
            cin>> action; //enter sell
		if (action=="s"){ //go here no
			for (int i=0; i<6; i++){
				cout <<"----------------------------------------------------------"<<endl;
				cout << "Pruduct "<< i<<"   ";
				cout << sarray[i]<<endl;    
				}
			}
		if (action=="c"){//go here no
			for (int i=0; i<5; i++){        
				cout <<"----------------------------------------------------------"<<endl;
				cout << "Manufacturer:  "<< i<<"    \n";        
				cout << *carray[i]<<endl;   
				}
		cout <<"----------------------------------------------------------"<<endl;
		for (int i=5; i<10; i++){       
			cout <<"----------------------------------------------------------"<<endl;
			cout << "Supplier:  "<< i<<"    \n";        
			cout << *carray[i]<<endl;   
			}
			}   
		if (action=="sell"){//go here ok
		mark: cout << "How meny types of products are you selling?\nyou can select up to six types"<<endl;
			cin>> count; //enter string by mistake ups


			if(count>0&&count<6){//go here no
				cout<< "ok"<< endl;
				//system("PAUSE");
				for(int i=0; i<count; i++){
					cout << "Enter the "<< i+1<<" product"<<endl;
					cin >> prNumber;
					if(prNumber>5 || prNumber <0){
						cout << "Enter quantity value"<<endl;
						cin >>quantity;
						pNumber[i]=prNumber;
						qNumber[i]=qrNumber;
						}
					}
				}
			else{ //go here yes
				cout<< "There was an error in you selection! lets start again"<< endl;
				cin>>count;//here it should stop and wait for input,  and it this was not here it should end up at if (action ="sell")...?
				system("PAUSE");
				}


			}
		}
as you can see i need some help
thanks
lamefif is offline   Reply With Quote
Old Jan 4th, 2006, 2:09 PM   #2
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,473
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
Look into the isdigit() function.

I also suggest having a defined range limit... so you can error check against that too.
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Jan 4th, 2006, 2:16 PM   #3
HaCkeR
Hobbyist Programmer
 
HaCkeR's Avatar
 
Join Date: Nov 2005
Location: UK
Posts: 131
Rep Power: 0 HaCkeR is an unknown quantity at this point
Send a message via AIM to HaCkeR Send a message via MSN to HaCkeR
From you notes i think your having a problem with getting the input again if it has an error. If im right which i might not be but try using
cin.clear();
cin.sync();

they should clear the user input so it doesnt skip straight over the Input

dunno if you was having a problem with that but i think you were
HaCkeR is offline   Reply With Quote
Old Jan 4th, 2006, 2:40 PM   #4
jayme
Professional Programmer
 
jayme's Avatar
 
Join Date: Nov 2005
Location: Canada
Posts: 495
Rep Power: 0 jayme is an unknown quantity at this point
Send a message via MSN to jayme
I also suggest putting your ----------------------------------------------------------'s into a char like so..

   char dash[59] = "----------------------------------------------------------";
                cout <<dash<<endl;
            cout << "Enter \"q\" to exit the program" <<endl;
            cout << "Enter \"s\" to display stock information" <<endl;
            cout << "Enter \"c\" to display client information" <<endl; //bla bla 
            cout << "To sell pruducts enter \"sell\" and follow the instructions" <<endl;
            cout <<dash<<endl;
its just cleaner
__________________

Quote:
Originally Posted by Mohamed Jihad
Durka durka!
Due to incorrect calculations during the middle ages, our calendar actually begins a few years after Jesus' birth. Thus the real 6/6/6 happened a few years back. The world already ended and you missed it.

Download Code::Blocks now!
jayme is offline   Reply With Quote
Old Jan 4th, 2006, 3:01 PM   #5
lamefif
Newbie
 
lamefif's Avatar
 
Join Date: Mar 2005
Posts: 16
Rep Power: 0 lamefif is on a distinguished road
thanks guys i can use all your suggestions
lamefif is offline   Reply With Quote
Old Jan 4th, 2006, 11:50 PM   #6
nindoja
Programmer
 
Join Date: Jun 2005
Posts: 92
Rep Power: 4 nindoja is on a distinguished road
also, you can't check like if (action=="s"), you need to use single quotes, if (action=='s'). IR's suggestion is good as well.
nindoja is offline   Reply With Quote
Old Jan 5th, 2006, 9:12 AM   #7
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Quote:
Originally Posted by nindoja
also, you can't check like if (action=="s"), you need to use single quotes, if (action=='s'). IR's suggestion is good as well.
You don't. Comparing action=='s' is comparing a string against a character. action=="s" is comparing a string against a string.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Jan 5th, 2006, 12:29 PM   #8
nindoja
Programmer
 
Join Date: Jun 2005
Posts: 92
Rep Power: 4 nindoja is on a distinguished road
whoops, I thought he was checking characters against a character. Sorry about that, just misread his post.
nindoja is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:46 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC