switch/case problem?

This is a discussion on switch/case problem? within the C++ Programming forums, part of the General Programming Boards category; Hello, in part of the program I'm working on, I have it so the user inputs either y, Y, n, ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    10

    switch/case problem?

    Hello, in part of the program I'm working on, I have it so the user inputs either y, Y, n, or N for an answer using cin.getline. Though in my switch function, my compiler tells me "error C2450: switch expression of type 'char [2]' is illegal"

    here is my sample code:

    Code:
    void botguy(){
    	clear_screen();
    	cout<<"Is Botguy On Your Side? (y/n): ";
    	cin.getline(bot_an,2);
    	switch(bot_an){
    		case "y":
    Any help?
    ~Wiiplayer12

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    You can't use a character array to control a switch. You have to use an integral type.

    However, because you only want the user to type a single character, you can use the char type here. (A char is just an integer code for a character, which is why it works in a switch.)

    So instead of getline, use get or operator>> to read into a single char variable, and not a char[2]. Then make the case statements use chars by putting single quotes instead of double quotes.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    10
    wow, that was fast! Thanks alot, I'm new at C++ so yeah. Thanks again for the help, do you want the ful code? i kinda doubt you'll need it thoguh, but if you want to...

    anyways, thanks
    ~Wiiplayer12

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 04:46 PM

Tags for this Thread


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