switch problems

This is a discussion on switch problems within the C++ Programming forums, part of the General Programming Boards category; im working on a texted adventure where the user can walk around a map to solve puzzles. im using a ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    12

    switch problems

    im working on a texted adventure where the user can walk around a map to solve puzzles. im using a switch to move. if a player can only go east, west or south from the room they are in i have a default for if they try to go north. my problem is that when they go into the defualt it kicks them out of the program. i tried putting the switch into a while loop that should repeat until they enter a valid answer but it goes into infinite loop if they go into the default. i dont understand why its doing this. is my loop wrong or am i going about it the wrong way? my switch with the loop is below. the letters represent the direction they are going.

    Code:
    int x=0;
    
    while (x!=1)
    	{
    	switch(direction)
    	{
    	case 'e':
    	case 'E':
    		x=1;
    		room3();
    		break;
    
    	case 's':
    	case 'S':
    		x=1;
    		room5();
    		break;
    
    	case 'w':
    	case 'W':
    		x=1;
    		room1();
    		break;
    
    	default:
    		x=0;
    		cout << "not a choice\n";
    	}
    	}

  2. #2
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    I think you just need a break statement in the default case, and if you're using Dev-C++, you'll need to use cin.get() to pause the program before it exits. Or use:

    Code:
    while (char a=getchar() !='\n');
    Another problem programmers run into all the time is lines of code that don't execute because there's already a new line stored in the buffer, which can be cleared using cin.ignore();
    Last edited by Krak; 05-16-2005 at 07:55 AM.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    You don't need a break statement in the default case. It breaks anyway.

  4. #4
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Can I see more of your code? The switch ... case LOOKS ok.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    The only thing wrong with the switch is in the default case. You don't need to set x to 0 if it's already 0. Unless, of course, the rest of your code modifys x.

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    12

    got it

    thanks for all the input but i figured it out. i didnt have the cin inside the while. thanks again for being willing to help, i may have more questions. im planning on posting the final code for anyone thats interested in a beginners text adventure. off to do more coding

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ascii rpg help
    By aaron11193 in forum C Programming
    Replies: 18
    Last Post: 10-29-2006, 01:45 AM
  2. nested switch issue
    By fsu_altek in forum C Programming
    Replies: 3
    Last Post: 02-15-2006, 09:29 PM
  3. switch problems...
    By rexnprogress in forum C Programming
    Replies: 6
    Last Post: 11-26-2005, 06:43 PM
  4. Switch
    By cogeek in forum C Programming
    Replies: 4
    Last Post: 12-23-2004, 05:40 PM
  5. switch and string copy problems ?
    By Neildadon in forum C Programming
    Replies: 5
    Last Post: 06-12-2002, 07:42 AM

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