Welcome to Dream.In.Code
Become a C++ Expert!

Join 150,055 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,715 people online right now. Registration is fast and FREE... Join Now!




switch not working properly

 
Reply to this topicStart new topic

switch not working properly

c--
1 Feb, 2008 - 10:11 PM
Post #1

New D.I.C Head
*

Joined: 26 Jan, 2008
Posts: 27

My switch is not working properly with a program. The program is ignoring every "case" but not the default. Do you know what's the problem??


CODE
// switch.cpp : main project file.


#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int type;

int main()
{

    cout << "Type a number from 1 to 3:;
    cin >> type;

    switch ( type ){

        case '1':
            
                cout << "1";
            break;
        case '2':
            
                cout << "2";
            
            break;

        case '3':
                cout << "3";
            break;

        case '\n':
        case '\t':
        case ' ':
            break;

        default:
            cout << "You typed an invalid number." << endl;
            break;



    }

    

    cin.ignore();
    cin.get();

    return 0;
}


User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Switch Not Working Properly
1 Feb, 2008 - 10:16 PM
Post #2

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 7,158



Thanked: 78 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
You declared type as an int, but you are comparing it as chars in the switch. Try declaring it as a char.
User is online!Profile CardPM
+Quote Post

Delta_Echo
RE: Switch Not Working Properly
1 Feb, 2008 - 10:18 PM
Post #3

D.I.C Regular
***

Joined: 24 Oct, 2007
Posts: 480


My Contributions
While using case <value>: you don't need the ' '. Example

case 1: {
// Code }


Goodluck smile.gif
User is offlineProfile CardPM
+Quote Post

c--
RE: Switch Not Working Properly
2 Feb, 2008 - 08:36 AM
Post #4

New D.I.C Head
*

Joined: 26 Jan, 2008
Posts: 27

Ohh thank you. smile.gif It worked
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Switch Not Working Properly
2 Feb, 2008 - 11:21 AM
Post #5

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,355



Thanked: 51 times
Dream Kudos: 25
My Contributions
QUOTE(Delta_Echo @ 2 Feb, 2008 - 01:18 AM) *

While using case <value>: you don't need the ' '. Example

case 1: {
// Code }


Goodluck smile.gif

This is only true if the user is switching on an integer value, as he is here. Switching on a char value would require the single quotes.

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 10:26PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month