how to display wrong input if i using switch??

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2009
Posts: 72
Reputation: samsons17 is an unknown quantity at this point 
Solved Threads: 1
samsons17 samsons17 is offline Offline
Junior Poster in Training

how to display wrong input if i using switch??

 
0
  #1
Nov 6th, 2009
i need help with this very simple question....
the default in this program will just display the wrong input
if the input entered by user is different with the data type of the variable which is char..
but not if the user entered the the word which is the character is located in front of the word...

in my program, if the user entered apple instead of 'a',this program will still print that i'm in the red team...

I want this program to display the input error..

this is my code :

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main () {
  5.  
  6. char answer;
  7.  
  8. cout<<"Enter Your Team :";
  9. cin>>answer;
  10.  
  11. switch (answer) {
  12. case 'a' :
  13. cout<<"You in the Red Team";
  14. break;
  15.  
  16. case 'b' :
  17. cout<<"You in the blue team";
  18. break;
  19.  
  20. default :
  21. cout<<"wrong input";
  22. break;
  23. }
  24. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,466
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 189
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso
 
0
  #2
Nov 6th, 2009
Then you should take the input as a string. Then if the size of the string is
greater than 1 then display an error message, else compare it with a or b.
Doing this way you would have to use if/else. This isn't that bad if you
don't have many cases.
1) Prove that the area of a circle is pi*r^2, where "r" is the radius of the circle.
2) Problem 2[b]solved by : jonsca
Reply With Quote Quick reply to this message  
Reply

Message:




Views: 209 | Replies: 1
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC