Thread: Why doesnt my "if" work?

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    8

    Why doesnt my "if" work?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void initialize (void);
    void menu (char drink1[5][10],char drink2[5][10],char drink3[5][10],char drink4[5][10],char drink5[5][10],float price1,float price2,float price3,float price4,float price5);
    
    int main (void)
    {
    	initialize ();
    
    	return 0;
    }
    
    void initialize ()
    {
    	char drink1[5][10], drink2[5][10], drink3[5][10], drink4[5][10], drink5[5][10];
    	float price1,price2,price3,price4,price5;
    
    	printf("Please input the name of drink : ");
        scanf("%s",&drink1);
    	printf("Enter the price of the drink : ");
    	scanf("%f",&price1);
    
    	printf("\nPlease input the name of drink : ");
    	scanf("%s",&drink2);
    	printf("Enter the price of the drink : ");
    	scanf("%f",&price2);
    
    	printf("\nPlease input the name of drink : ");
    	scanf("%s",&drink3);
    	printf("Enter the price of the drink : ");
    	scanf("%f",&price3);
    
    	printf("\nPlease input the name of drink : ");
    	scanf("%s",&drink4);
    	printf("Enter the price of the drink : ");
    	scanf("%f",&price4);
    
    	printf("\nPlease input the name of drink : ");
    	scanf("%s",&drink5);
    	printf("Enter the price of the drink : ");
    	scanf("%f",&price5);
    
    	menu (drink1,drink2,drink3,drink4,drink5,price1,price2,price3,price4,price5);
    
    }
    
    void menu (char drink1[5][10],char drink2[5][10],char drink3[5][10],char drink4[5][10],char drink5[5][10],float price1,float price2,float price3,float price4,float price5)
    {
    	int purchase;
    	float coin, balance;
    
    	printf("\n1. %s		%3.2f\n",drink1,price1);
    	printf("2. %s	%3.2f\n",drink2,price2);
    	printf("3. %s		%3.2f\n",drink3,price3);
    	printf("4. %s		%3.2f\n",drink4,price4);
    	printf("5. %s	%3.2f\n",drink5,price5);
    
    	printf("Select a drink to purchase\n");
    	scanf("%d",&purchase);
    	printf("Your choice : %d\n",purchase);
    
    	switch (purchase)
    	{	
    	case 1:printf("Enter coin (10, 20, 50 cents or -1 to cancel) : ");	    
    		   scanf("%f",&coin);
    
    		   if (coin==10||20||50)  /*this if doesnt work. it doesn goes to another if when i type -1 */
    		   {
    			   balance = ((price1*100) - coin)/100;		
    			   printf("Balance : RM%3.2f\n",balance);
    
    			   for (purchase=0;purchase<10;purchase++)		
    			   {			
    				   printf("Enter coin (10, 20, 50 cents or -1 to cancel) : ");	    				
    				   scanf("%f",&coin);
    
    				   if (coin==10||20||50)
    				   {
    						balance = ((balance*100) - coin)/100;				
    						printf("Balance : RM%3.2f\n",balance);
    				   }
    				   if (coin==-1)
    				   {
    					   printf("Purchase canceled");	    
    					   menu (drink1,drink2,drink3,drink4,drink5,price1,price2,price3,price4,price5);
    				   }
    			   }
    		   }
    		
    		   if (coin==-1)	
    		   {		
    			   printf("Purchase canceled");	    
    			   menu (drink1,drink2,drink3,drink4,drink5,price1,price2,price3,price4,price5);	
    		   }		
    		   else
    		   {
    				printf("Invalid\n");
    		   }
    	break;
    
    	case -999:printf("Enter maintenance options\n");
    		    main ();
    	break;
    
    	default:printf("Invalid\n");
    		    menu (drink1,drink2,drink3,drink4,drink5,price1,price2,price3,price4,price5);
    	}
    }
    is there something wrong with my coding? i'm looking at it and cant figure what's wrong over there.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    if (coin==10||20||50)
    must be
    Code:
    if (coin==10||coin==20||coin==50)
    Kurt

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You should not be recursively calling either main or menu in your menu function. Use a loop to redisplay the menu when necessary and some flag/break to exit the loop when necessary.

    Also, there is no need to make all of those two-dimensional drink variables. You can either have a single two-dimensional drink[5][10] variable to represent all 5 different drinks, or five individual drink[10] variables. The former would simplify function parameter passing. Likewise, your price variables could be made into an array.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Jul 2006
    Posts
    8
    i dont get it, if i had only char drink[5][10], could i get the output listed in order, like what i had now? in 1, 2,3....?

    this is what i did earlier, but i dont know how to get the output into order
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void initialize (void);
    void menu (char choice[5][10],float price[10]);
    
    int main (void)
    {
    	initialize ();
    
    	return 0;
    }
    
    void initialize ()
    {
    	char choice[5][10];
    	float price[10];
    	int i;
    
    	for (i=0;i<5;i++)
    	{
    		printf("\nEnter the name of drinks : ");
    		scanf("%s",choice[i]);
    		printf("Enter the price for the drinks : ");
    		scanf("%f",&price[i]);
    
    	}
    
    
    	menu (choice,price);
    
    }
    
    void menu (char choice[5][10],float price[10])
    {
    	
    	int i;
    
    	printf("\nWhich drink do you want to buy?\n");
    
    	for (i=0;i<5;i++)
    	{
    		printf("%s	RM%4.2f\n",choice[i], price[i]);
    	}
    }

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Why is price[] 10 elements large instead of 5?

    To "get the output in order", you'll have to sort the data. You might try a bubble sort (simple but slow).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    yea...what he said.
    Registered Linux User #380033. Be counted: http://counter.li.org

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    scanf("%s",choice[i]);
    You might want to read the FAQ: FAQ > How do I... (Level 1) > Get a line of text from the user/keyboard (C)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  2. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  3. help getting program to work
    By jlmac2001 in forum C Programming
    Replies: 2
    Last Post: 11-13-2002, 11:04 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM