-
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.
-
If you switch on an integer (or any other numeric variable type) you only need to write
The apostrophes are only for characters. You'd only use them if you were switching on a char variable.
QuantumPete
-
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.