![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2005
Posts: 16
Rep Power: 0
![]() |
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");
}
}
}![]() thanks |
|
|
|
|
|
#2 |
|
Programming Guru
![]() ![]() ![]() |
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." |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
|
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 |
|
|
|
|
|
#4 | |
|
Professional Programmer
|
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;
__________________
▄▄▄▄ Quote:
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! ▄▄▄▄ |
|
|
|
|
|
|
#5 |
|
Newbie
Join Date: Mar 2005
Posts: 16
Rep Power: 0
![]() |
thanks guys i can use all your suggestions
|
|
|
|
|
|
#6 |
|
Programmer
Join Date: Jun 2005
Posts: 92
Rep Power: 4
![]() |
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.
|
|
|
|
|
|
#7 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Quote:
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
|
#8 |
|
Programmer
Join Date: Jun 2005
Posts: 92
Rep Power: 4
![]() |
whoops, I thought he was checking characters against a character. Sorry about that, just misread his post.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|