![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Oct 2004
Location: England, UK
Posts: 139
Rep Power: 0
![]() |
restricting cin
Hi guys, me again with another pissing college assignment......
Basically we need to make a programme that will simulate a telephone. It all works fine and is great and everthing is good in the world. Until, I'm doing error testing and find that when I input a coin value as a character such as 'h' it completly trips out and goes into an infinate loop! ![]() Here's the code for the function..... int coins () // the coin input is stored as a function as it is used twice
{
for(;;)
{
cin>> value;
if (value == 0)
{
break;
}
else
{}
if (value != 10 &&
value != 20 &&
value != 50 &&
value != 100)
{
value = 1;
}
switch(value)
{
case 10:
total = total + value;
remaining = total / charge;
cout<< endl << "You entered " << value << "p. Your total is "
<< total << "p. That equals " << remaining << " seconds. "
<< endl << "Enter another coin or '0' to dial."
<< endl << endl;
break;
case 20:
total = total + value;
remaining = total / charge;
cout<< endl << "You entered " << value << "p. Your total is "
<< total << "p. That equals " << remaining << " seconds. "
<< endl << "Enter another coin or '0' to dial."
<< endl << endl;
break;
case 50:
total = total + value;
remaining = total / charge;
cout<< endl << "You entered " << value << "p. Your total is "
<< total << "p. That equals " << remaining << " seconds. "
<< endl << "Enter another coin or '0' to dial."
<< endl << endl;
break;
case 100:
total = total + value;
remaining = total / charge;
cout<< endl << "You entered " << value << "p. Your total is "
<< total << "p. That equals " << remaining << " seconds. "
<< endl << "Enter another coin or '0' to dial."
<< endl << endl;
break;
default:
cout<< endl << "Your last coin was not valid, please try again"
<< " using one of the accepted coins."
<< endl << endl;
break;
}
cout.flush();
}
return 0;
}My teacher suggested that cout.flush() would solve the problem, but needless to say it didn't. basically I need to be able to make it so the only values that can be entered are 10, 20 ,50 and 100. cheers guys Ade
__________________
Don't wound what you can't kill Last edited by Ade; Feb 9th, 2005 at 4:20 AM. |
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: Nov 2004
Posts: 250
Rep Power: 4
![]() |
Aside from using a nonstandard method to read keystrokes, the only good way to go about what you want is to validate the input:
#include <iostream>
#include <limits>
using namespace std;
// Clear stream state
template <typename Ch, typename Traits>
basic_ios<Ch, Traits>& clear ( basic_ios<Ch, Traits>& io )
{
io.clear();
return io;
}
bool valid_coin ( int coin )
{
return coin == 10 || coin == 20 || coin == 50 || coin == 100;
}
int main()
{
int coin;
while ( true ) {
cout<<"Enter 10, 20, 50, or 100: ";
if ( !( cin>> coin >> clear ) || !valid_coin ( coin ) ) {
cerr<<"Invalid entry, please try again"<<endl;
// Empty the stream of bad characters
cin.ignore ( cin.rdbuf()->in_avail() );
}
else
break; // Everything is okay
}
cout<<"You entered: "<< coin <<endl;
cin.get();
} |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Oct 2004
Location: England, UK
Posts: 139
Rep Power: 0
![]() |
Oh my god!!
Eggbert, you're not dead!! PS thanks for the reply ! ![]()
__________________
Don't wound what you can't kill |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Oct 2004
Location: England, UK
Posts: 139
Rep Power: 0
![]() |
OK, I just tested your code Egg and it does the same as mine, if I enter a character such as L, y, t etc. it enters an infinate loop.
I have no idea why???? ![]()
__________________
Don't wound what you can't kill |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|