case switch not working

This is a discussion on case switch not working within the C Programming forums, part of the General Programming Boards category; Hi guys, I was just writing a wee program to calulate APR on a credit card, i was going to ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Location
    Glasvegas, Scotland.
    Posts
    68

    case switch not working

    Hi guys,

    I was just writing a wee program to calulate APR on a credit card, i was going to use a case statement, so did a bit of reading and looked at some examples, seems simple but i can't get mine to work. Could you help?
    My code:
    Code:
    #include <stdio.h>
    
    int main()
    {
    float balance, apr, apr_dec, interest;
    int term;
    
    // Ask for balance on card:
    printf("Enter the balance of the card.\n");
    	scanf("%f",&balance);
    printf("Card Balance = %.2f\n", balance);
    
    // Get Rate:
    printf("Enter the current interest rate (APR) in percent.\n");
    	scanf("%f", &apr);
    printf("Apr = %.2f\%\n", apr);
    apr_dec = apr / 100;
    
    // Get Term
    printf("Enter the Term in Months.\n");
    	scanf("%d", &term);
    printf("Term = %d months\n");
    
    
    interest = (apr_dec * balance) / 12;
    printf("interest = %.2f\n", interest);
    
    	int selection ;
    	printf("Enter the type of payment : \n") ;
    	printf("1: minimum payment\n") ;
    	printf("2: fixed amount\n") ;
    	printf("selection : ");
    	scanf("%d", &selection) ;
    	switch (selection) {
    	case '1' :
    		printf("Case 1");
    		break ;
    	case '2' : 
    		printf("Case 2");
    		break ;
    	}
    
    printf("something\n");
    }
    When i run the above i get this output:

    Code:
    Enter the balance of the card.
    1000
    Card Balance = 1000.00
    Enter the current interest rate (APR) in percent.
    9.9
    Apr = 9.90%
    Enter the Term in Months.
    30
    Term = 30 months
    interest = 8.25
    Enter the type of payment :
    1: minimum payment
    2: fixed amount
    selection : 1
    something
    So it won't do anything in the switch. Any ideas?

    Thanks in advance.
    Kai.

  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    If you switch on an integer (or any other numeric variable type) you only need to write
    Code:
    case 1:
    The apostrophes are only for characters. You'd only use them if you were switching on a char variable.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #3
    Registered User
    Join Date
    Oct 2007
    Location
    Glasvegas, Scotland.
    Posts
    68
    Ah thanks! You're a legend!

    Interestingly, i had it as char selection; to begin with but that wouldn't work so changed it to int.

    Thanks a lot!
    Kai.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 03-05-2009, 10:32 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  3. enumeration with switch case?
    By Shadow12345 in forum C++ Programming
    Replies: 17
    Last Post: 09-26-2002, 04:57 PM
  4. A simple array question
    By frenchfry164 in forum C++ Programming
    Replies: 7
    Last Post: 11-25-2001, 03:13 PM

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