system crash when char is entered during int switch? solution?

This is a discussion on system crash when char is entered during int switch? solution? within the C++ Programming forums, part of the General Programming Boards category; i noticed when i have a switch statement the default does not catch chars... here is a sample- Code: int ...

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    21

    system crash when char is entered during int switch? solution?

    i noticed when i have a switch statement the default does not catch chars...
    here is a sample-

    Code:
    int input;
    cin>>input;
    
    switch(input)
    {
    
    case 1:
    statement;
    break;
    
    case 2:
    statement;
    break;
    
    case 3:
    statement;
    break;
    
    default:
    cout<<"cant do that\n";
    break;
    
    }
    im guessing this is because the switch statement is only taking ints, but is there a way I can prevent a system crash if myself or someone accidentally inputs a char rather than an int?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,775
    Check with:
    Code:
    if (cin >> input)
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    How to deal with invalid input from std::cin: RPG troubles
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Jul 2012
    Posts
    21
    sweet, thanks!! currently hit a wall in my programming learning journey, will return with many questions after some good sleep

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. comparing an entered value to a char
    By hawaiian_girl in forum C Programming
    Replies: 3
    Last Post: 10-21-2010, 05:28 PM
  2. solution to a linear system of equations
    By e4321 in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2009, 08:00 PM
  3. What does system crash mean?
    By alyeska in forum C++ Programming
    Replies: 4
    Last Post: 10-06-2007, 03:53 AM
  4. loop freak out when char entered
    By cerin in forum C++ Programming
    Replies: 7
    Last Post: 04-02-2005, 01:31 AM
  5. declared int but entered a char
    By lockpatrick in forum C Programming
    Replies: 6
    Last Post: 05-01-2002, 10:42 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21