If Else statement

Hey guys, I am having trouble with my if/else statement



The "Else" bit doesn't want to run if i press n, it still runs the "if" statement.

Any help is much appreciated
Thankyou.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
printf("\n\n");
			printf("*****************************************************\n");
			printf("**   Find Disclosed amount of your shopping bill   **\n");
			printf("*****************************************************\n");
			printf("\nPlease enter the item name: ");
			scanf("%s", &itemname);
			printf("\nPlease enter the price: ");
			scanf("%f", &price);
			printf("\nPlease enter the discount %%: ");
			scanf("%f", &discount);
			printf("\nWould you like to add any more items? Y/N: ");
			scanf("%s", &yn);
				
			
			if(yn='y')
			{
				printf("\nPlease enter the item name: ");
				scanf("%s", &itemname2);
				printf("\nPlease enter the price: ");
				scanf("%f", &price2);	
				
				total=price+price2;
				
				discounted=total*(discount/100);
				
				printf("\nYour total price is: %.0f\n Press ENTER to continue", discounted);
				getch();
				break; 	
			}
			else 
			discounted=price*(discount/100);
			
			printf("\nYour total price is: %.0f\nPress ENTER to continue", discounted);
	
closed account (48T7M4Gy)
== not =
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
if(yn='y')
			{
				printf("\nPlease enter the item name: ");
				scanf("%s", &itemname2);
				printf("\nPlease enter the price: ");
				scanf("%f", &price2);	
				
				total=price+price2;
				
				discounted=total*(discount/100);
				
				printf("\nYour total price is: %.0f\n Press ENTER to continue", discounted);
				getch();
				break; 	
			}
			else 
			discounted=price*(discount/100);
			
			printf("\nYour total price is: %.0f\nPress ENTER to continue", discounted);


On line 1, it needs to be if(yn == 'y'), not if(yn='y')
== is comparing the two, used for boolean "stuff", while = is declaring yn to equal 'y'

also
1
2
3
4
else 
			discounted=price*(discount/100);
			
			printf("\nYour total price is: %.0f\nPress ENTER to continue", discounted);

needs to be
1
2
3
4
5
6
else 
{
			discounted=price*(discount/100);
			
			printf("\nYour total price is: %.0f\nPress ENTER to continue", discounted);
}

take note of the curly brackets, you need them a lot.
check your line 16. in c you can't compare by using '='.
Thanks guy's you guys are legends!
Topic archived. No new replies allowed.