Thread: Switch

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    30

    Switch

    I do not understand even if I wrote why this program doesnt print the total but does print the entered value?
    Please explain me.

    #include <stdio.h>
    #include <math.h>

    int main (void) {

    int pronum; //this must be an integer, counter
    float total;


    scanf("%d",&pronum);

    switch (pronum) {

    case '1':
    total = (pronum * 2.98);
    break;

    case '2':
    total =( pronum* 4.50);
    break;

    case '3':
    total = (pronum * 9.98);
    break;

    case '4':
    total = (pronum * 4.49);
    break;

    case '5':
    total = (pronum * 6.87);

    break;



    }


    printf("%f\n%d",total,pronum);


    return 0;
    }
    Attached Files Attached Files
    Last edited by erdemtuna; 11-19-2015 at 03:09 PM.

  2. #2
    Registered User
    Join Date
    Oct 2015
    Posts
    43
    Because in the switch you have to use 1, not '1'
    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    int main (void) {
    
    
    	int pronum; //this must be an integer, counter
    	float total;
    
    
    
    
    	scanf("%d",&pronum);
    
    
    	switch (pronum) {
    
    
    		case 1:
    			total = (pronum * 2.98);
    			break;
    
    
    		case 2:
    			total =( pronum* 4.50);
    			break;
    
    
    		case 3:
    			total = (pronum * 9.98);
    			break;
    
    
    		case 4:
    			total = (pronum * 4.49);
    			break;
    
    
    		case 5:
    			total = (pronum * 6.87);
    
    
    			break;
    
    
    
    
    
    
    	}
    
    
    
    
    	printf("%f\n%d",total,pronum);
    
    
    
    
    	return 0;
    }
    By using '1' you're testing if the value is the ASCII code of 1, that is 49.

  3. #3
    Registered User
    Join Date
    Nov 2015
    Posts
    30
    Oh,, so in numbers, we dont use '1' things. but if we want to indicate upper case or lower case letter, we can use?
    Sorry Im novice, im trying to learn

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    If you want to use letters you need to use a char, not an int.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with switch(x)
    By oreo in forum C Programming
    Replies: 3
    Last Post: 09-15-2010, 08:22 AM
  2. Switch
    By cogeek in forum C Programming
    Replies: 4
    Last Post: 12-23-2004, 06:40 PM
  3. A switch that doesn't switch
    By Death_Wraith in forum C++ Programming
    Replies: 5
    Last Post: 11-06-2004, 12:18 PM
  4. Is it ok to have if else in my switch?
    By blackgold>> in forum C++ Programming
    Replies: 5
    Last Post: 04-29-2004, 11:09 AM
  5. switch
    By modec in forum C Programming
    Replies: 4
    Last Post: 05-19-2003, 05:09 PM