Thread: Need help with "Switch"

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    15

    Need help with "Switch"

    Ok, I was given an assignment that looks like this:

    In a menu, ask the user whether they want to calculate the factorial of a non-negative number, or exit. If they choose to calculate the factorial and enter any negative number, print an error message and take them back to the main menu. If they choose an invalid menu option, print an error message and re-print the menu.

    I'm completely new to switch and I have no idea what I'm doing wrong. What happens with the program is when I compile it gives me five lines of messages saying "Warning: Character constant too long for its type" on line 9. So I'm not sure whats going on there. But when I run it the menu shows up with option 1 and 2 but when I type a number and press enter nothing happens. Any suggestions?




    Code:
    #include <stdio.h>
    
    	int main () {
    
    	int j,i,k,result,num;
    
    	num = 5;
    
    	while (num == 5) {
    
    	printf("Please choose a menu item:\n1. Factorial\n2. Exit\n");
    	scanf("%d\n", &k);
    
    		switch (k) {
    
    			case 'k == 1':
    			printf("Enter a number:\n");
    			scanf("%d\n", &i);
    			result = 1;
       			for (j=i;j>=1;j--)
          			result = result*j;
       			printf("%d! = %d\n",i,result); 
    			break;
    
    			case 'k == 2':
    			return 0;
    			break;
    
    			case 'k > 2':
    			printf("Sorry - Invalid menu option, please select again.\n");
    			break;
    
    			case 'k < 1':
    			printf("Sorry - Invalid menu option, please select again.\n");
    			break; 
    
    			}
    	}
    
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You never did hit I'm feeling lucky on Google did you.

    Case labels must be constant integral values, so you could have case 42: and case -117: and case 65535: and so on.

  3. #3
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Your code seems ok, but i would suggest that you read a bit about the switch.
    Code:
    switch( <integer expression> )
    {
       case <integer constant expression>:  <statements> break;
       ...
    }
    The field <integer constant expression> means a number or something that evaluates to a number like: 5+6*7 and MUST be evaluatable before the program is compiled. Therefore the error in your code is the 'case' statement because case 'k == 1' actually is a valid integer constant expression but with a few compiler warnings about multibyte character constants... etc.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  4. #4
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Code:
    			case 'k > 2':
    			printf("Sorry - Invalid menu option, please select again.\n");
    			break;
    
    			case 'k < 1':
    			printf("Sorry - Invalid menu option, please select again.\n");
    			break;
    Also you want to do range check on user input? Use an if() statement. It will save you writting twice the same error message...
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I think you desperately need to learn indenting too. It's very important to gasp this concept early on.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can a "switch" be inside a loop?
    By gmk0351 in forum C Programming
    Replies: 5
    Last Post: 03-28-2008, 05:47 PM
  2. Can "switch" based on std::string?
    By meili100 in forum C++ Programming
    Replies: 7
    Last Post: 02-20-2008, 10:09 AM
  3. What is the "switch" function?
    By engstudent363 in forum C Programming
    Replies: 3
    Last Post: 02-15-2008, 01:47 AM
  4. How to use "switch" to perform large numbers cases?
    By megablue in forum C Programming
    Replies: 4
    Last Post: 07-09-2003, 10:51 AM
  5. "Switch"
    By face_master in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 06-12-2002, 04:47 PM