Char assigned value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • leeon0220
    New Member
    • Feb 2017
    • 2

    Char assigned value

    Hi guys .. My program is working however instead of putting the assigning f=='x' it must xl -- HOWEVER when i used xl the computation goes wrong .. what shoud i do .. THANKS in ADVANCE
    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    char f;
    int s_price, disc, t_price;
    clrscr();
    printf(" \n\tEnter the Shirt Size (X,L,M,S): ");
    scanf("%c",&f);
    printf(" \n\tEnter the The Price of Shirt: ");
    scanf("%d",&s_price);
    
    
    if (f=='x' && s_price>=500)
    		t_price=s_price+100;
    if (f=='x' && s_price<500)
    		t_price=s_price;
    if (f=='l' && s_price>=400)
    		t_price=s_price+50;
    if (f=='l' && s_price<400)
    		t_price=s_price+0;
    if (f=='m' || f=='s'&& s_price>0)
    		t_price=s_price+0;
    
    
    {
     if (f=='x' && s_price>=500)
    	printf(" \n\n\tThe Discount is : 100");
    else if (f=='x' && s_price<500)
    	printf(" \n\tNo Discount ! Purchase atleast 500php ");
    else if (f=='l' && s_price>=400)
    	printf(" \n\n\tThe Discount is : 50");
    else if (f=='l' && s_price<400)
    	printf(" \n\tNo Discount ! Purchase atleast 400php ");
    else if (f=='m' || f=='s')
    	printf(" \n\tThe Discount is only for XL and Large Shirts ");
    }
    		printf(" \n\n\tThe Total Price is: %d",t_price);
    getch();
    }
    Last edited by Frinavale; Feb 27 '17, 03:12 PM.
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    Is your question "why can't I make the program accept and recognize an input of "XL"?
    • XL is a string not a single character.
    • Need to declare f as a char array Instead of a char.
    • Need the scanf format to be "%s" instead of "%c".
    • Need to use strcmp instead of ==.

    Additionally, you may want to make your input processing case-insensitive.

    Comment

    Working...