How can I get the program below to reject all characters except numbers and these symbols (-, =, +, *, /, %)
How can I get the program below to reject all characters except numbers and these symbols (-, =, +, *, /, %)
You need to write your own input function using get
something like this:
Code:do { a = cin.get() if ( a == desiredchar ) input += a } while ( inputting )
A switch and case statement may be easier to use.
Code:bool ValidChar(char C) { if((C >= '0') && (C <= '9')) return true; if(C == '-') return true; if(C == '=') return true; if(C == '+') return true; if(C == '*') return true; if(C == '/') return true; if(C == '%') return true; return false; }
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
Or even
Code:bool ValidChar(char C) { return strchr( "0123456789-+*/%=", C ) != NULL; }
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Where do I put the bool statement? (I'm a newbie.)