Thread: why isnt my switch working

  1. #1
    Unregistered
    Guest

    why isnt my switch working

    here i my code. I dont know why it doesnt work. Can anybody help. thanx:

    #include <stdio.h>
    #include <stdlib.h>

    int main (void)
    {
    int letter;

    printf("A. Push data into stack\n"
    "B. Pop and print data\n"
    "C. Print data at top of stack\n"
    "D. Print entire stack (top to base)\n"
    "E. Print stack status: Empty\n"
    "F. Print stack status: Full\n"
    "G. Print number of elements\n"
    "H. Destroy stack and quit\n");

    printf("please enter the operation you would like to perform");
    scanf("%c",&letter);

    switch (letter)
    {
    case 'A'rintf("Push data into stack");break;
    case 'B'rintf("Pop and print data");break;
    case 'C'rintf("Print data at top of stack");break;
    case 'D'rintf("Print entire stack");break;
    case 'E'rintf("Print stack status");break;
    case 'F'rintf("Print stack status");break;
    case 'G'rintf("Print number of elements");break;
    case 'H'rintf("Destroy stack and quit");break;
    defaultrintf("your an idiot");
    }

    return 0;
    }

  2. #2
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Use code tags whenever you post code.

    > int letter;
    Should be,
    char letter;

    > scanf("%c",&letter);
    Can also be,
    letter=getch();
    The world is waiting. I must leave you now.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    You are scanf'ing a char into an int. I am not sure that will work.

    Use the getch() suggestion. It will return the letter as an int and of course is the usual method...

    You could also get a char with scanf and cast it in the switch statement : switch( (int) someChar )
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >You are scanf'ing a char into an int. I am not sure that will work.
    Why not? An int can hold all of the values that a char can and since they are both integral types, scanf and printf don't distinguish between the two as long as the values don't go out of bounds:
    Code:
    #include <stdio.h>
    
    int main ( void )
    {
      int i = 0;
      char c = 0;
      scanf ( "%c %d", &i, &c );
      printf ( "%d %c\n", i, c );
      return 0;
    }
    Though most code checkers will warn you about different arguments for the corresponding format flag. Generally, while int and char are interchangeable in scanf it's best to either not do so or cast the result to avoid unforseen bugs:

    scanf("%c",(char *)&letter);

    All in all it's easier to avoid scanf altogether, this is my recommendation.

    To solve your problem, there's a little trick called initialization. This is one of those times where leaving garbage values in your variables causes problems, initialize letter to any value and the program should work properly. I used

    int letter = 0;

    -Prelude
    My best code is written with the delete key.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > int i = 0;
    > char c = 0;
    > scanf ( "%c %d", &i, &c );
    This is HIGHLY dangerous code - char/int promotion rules do not apply to pointers to those types.

    Scanning a %c into an integer is only safe in that it will not overwrite any memory. However, what you don't know is which byte (in the integer) the char will go into, since this will depend on the endian-ess of your machine.

    Which is exactly the trap that Unregistered seems to have fallen into.

    Scanning a %d into a char is just asking for trouble. Not only will the char being pointed at get overwritten, so will the bytes following it (which you don't own), up to the size of an integer

    > while int and char are interchangeable in scanf
    See above - this only applies to printf

    > scanf("%c",(char *)&letter);
    Do this at your peril - your code will break if you get it wrong, and any tools which might help check it will be crippled.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 01-18-2008, 04:06 AM
  2. cin.get(); not working with my switch statement
    By tenor_jazz13 in forum C++ Programming
    Replies: 2
    Last Post: 01-12-2008, 10:33 PM
  3. nested switch issue
    By fsu_altek in forum C Programming
    Replies: 3
    Last Post: 02-15-2006, 10:29 PM
  4. help with switch
    By homegrown in forum C++ Programming
    Replies: 3
    Last Post: 09-07-2003, 05:08 PM
  5. Switch Case
    By FromHolland in forum C++ Programming
    Replies: 7
    Last Post: 06-13-2003, 03:51 AM