Thread: Need help with "if" statements

  1. #16
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by citizen
    Code:
     printf("Enter a value:\n"); 
       scanf("%d",&a);
       { // just delete this brace. It's not necessary.
    Still wouldn't fix it. They're missing the one for main.


    Quzah.
    Hope is the first step on the road to disappointment.

  2. #17
    Registered User
    Join Date
    May 2006
    Posts
    9
    Quote Originally Posted by citizen
    Code:
     printf("Enter a value:\n"); 
       scanf("%d",&a);
       { // just delete this brace. It's not necessary.
    I thought that was it and deleted it but that just made the whole thing much worse and gave more errors.

  3. #18
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Running the most recent post through a beautifier, this is what you have:
    Code:
    #include <stdio.h>
    #include <string.h>
    int main() 
    {
       int a;
       char name[1];
       int d=1; 
       printf ("%s","Welcome to Harry's Vending Machine\n");
       printf("Enter a value:\n"); 
       scanf("%d",&a);
       {  
          if ( a >= 1 )
          {
             printf ("\nPress 1 for 7 up\n");
             printf ("\nPress 2 for Coca Cola\n");
             printf ("\nPress 3 for Pepsi\n");
             printf ("\nPress 4 for Bottled Water\n");
             printf ("\nPress 5 for Dr. Pepper\n");
             scanf ("%d", 5);
             printf("Thank you for your purchase\n");
             printf ("\nEnjoy your refreshing drink and don't forget your change\n");       
          }
          else
          {
             printf("Please insert correct amount\n");
             scanf("%d", &a); 
          }
    Following good indentation habits is for the new to help write good code.

    [edit]FAQ > How do I... (Level 1) > How do I get a number from the user (C)
    Last edited by Dave_Sinkula; 05-12-2006 at 05:46 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #19
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    Code:
    /* aa.c */
    
    
    
    #include <stdio.h>
    #include <string.h>
    int main() 
    {   
      int a,d ;
      char name[1];
       d=1; 
       printf ("%s","Welcome to Harry's Vending Machine\n");
       printf("Enter a value:\n"); 
       
    
             
        printf ("\nPress 1 for 7 up\n");
        printf ("\nPress 2 for Coca Cola\n");
        printf ("\nPress 3 for Pepsi\n");
        printf ("\nPress 4 for Bottled Water\n");
        printf ("\nPress 5 for Dr. Pepper\n");
        scanf ("%d", &a);
    
    
         
       if(a >= 1) 
    {   
    
        printf("Thank you for your purchase\n");
        printf ("\nEnjoy your refreshing drink and don't forget your change\n");       
      }  
       else {
       printf("Please insert correct amount\n");
       scanf("%d", &a); 
      }
    }
    do not need d
    do not need name[]
    menu before enter a value
    prog not done are you going to do something with each diff selection?

    anyway
    Code:
    if (test;) { statement; }
     else if (test;) {statement;}
    Last edited by kryptkat; 05-12-2006 at 06:03 PM.

  5. #20
    Registered User
    Join Date
    May 2006
    Posts
    9
    Quote Originally Posted by kryptkat
    Code:
    /* aa.c */
    
    
    
    #include <stdio.h>
    #include <string.h>
    int main() 
    {   
      int a,d ;
      char name[1];
       d=1; 
       printf ("%s","Welcome to Harry's Vending Machine\n");
       printf("Enter a value:\n"); 
       
    
             
        printf ("\nPress 1 for 7 up\n");
        printf ("\nPress 2 for Coca Cola\n");
        printf ("\nPress 3 for Pepsi\n");
        printf ("\nPress 4 for Bottled Water\n");
        printf ("\nPress 5 for Dr. Pepper\n");
        scanf ("%d", &a);
    
    
         
       if(a >= 1) 
    {   
    
        printf("Thank you for your purchase\n");
        printf ("\nEnjoy your refreshing drink and don't forget your change\n");       
      }  
       else {
       printf("Please insert correct amount\n");
       scanf("%d", &a); 
      }
    }
    do not need d
    do not need name[]
    menu before enter a value
    prog not done are you going to do something with each diff selection?

    anyway
    Code:
    if (test;) { statement; }
     else if (test;) {statement;}
    actually now that everything seems fine because whenever they put in the $1 value it is working perfectly because it takes them to the soda selection and they can select the soda of their choice. The only problem that I have right now is that I need to find out whenever they put in an incorrect about(anything under $1) it will take them to a message "insert correct amount." That's all I need done for right now. Thank you so much for your help I appreciate it

  6. #21
    Registered User
    Join Date
    May 2006
    Posts
    9
    This is what I have so far if someone can look over my coding and let me know if everything is correct that would be appreciated. However, when I try to compile the code I am getting a syntax error towards the end which I just can't figure out what it might be from.
    Code:
    /* aa.c */
    #include <stdio.h>
    #include <string.h>
    int main() 
    {   
      int a;
      char name[1]; 
       printf ("Welcome to Harry's Vending Machine\n");
       printf("Enter a value:\n"); 
       {
       check: if ('a' >=1)
        scanf('a'==0);
        printf ("\nPress 1 for 7 up\n");
        printf ("\nPress 2 for Coca Cola\n");
        printf ("\nPress 3 for Pepsi\n");
        printf ("\nPress 4 for Bottled Water\n");
        printf ("\nPress 5 for Dr. Pepper\n");
        scanf ('a'==0);
        if('a' >= 1)
      {
        printf("Thank you for your purchase\n");
        printf ("\nEnjoy your refreshing drink and don't forget your change\n");       
      }
       else {
       printf("Please insert correct amount\n");
        while ( a < 1 );   
    }

  7. #22
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Harryt123
    Code:
    /* aa.c */
    #include <stdio.h>
    #include <string.h>
    int main() 
    {   
      int a;
      char name[1]; /* An array of 1 item is pointless. */
       printf ("Welcome to Harry's Vending Machine\n");
       printf("Enter a value:\n"); /* Shouldn't you display the menu before asking for a choice? */
       {
       check: if ('a' >=1) /* If the letter (character) 'a' is greater than or equal to one... */
        /* 'check:' is a goto lable, which you never use. */
        scanf('a'==0); /* Did you even try compiling your code? */
        printf ("\nPress 1 for 7 up\n");
        printf ("\nPress 2 for Coca Cola\n");
        printf ("\nPress 3 for Pepsi\n");
        printf ("\nPress 4 for Bottled Water\n");
        printf ("\nPress 5 for Dr. Pepper\n");
        scanf ('a'==0); /* 'a' is the character constant for the letter a, not your variable. */
        if('a' >= 1) /* Same problem here... */
      {
        printf("Thank you for your purchase\n");
        printf ("\nEnjoy your refreshing drink and don't forget your change\n");       
      }
       else {
       printf("Please insert correct amount\n");
        while ( a < 1 );   /* Here is the only time you're comparing your variable a correctly. */
    }
    Commented. However there are a few more points, such as 'name' never being used, and the variable a being used uninitialized. Things like that. You need to read your chapter on using scanf over again, and learn how it works.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #23
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    build on this simplified code.

    Code:
    /* aa.c */
    
    
    
    
    #include <stdio.h>
    
    int main() 
    {   
      int a;
      
       printf ("Welcome to Harry's Vending Machine\n");
      
     
    
        printf ("\nPress 1 for 7 up\n");
        printf ("\nPress 2 for Coca Cola\n");
        printf ("\nPress 3 for Pepsi\n");
        printf ("\nPress 4 for Bottled Water\n");
        printf ("\nPress 5 for Dr. Pepper\n");
    
       printf("Enter a value:\n"); 
    
    
         scanf("%d",&a);
        if(a >= 1)
      {
        printf("Thank you for your purchase\n");
        printf ("\nEnjoy your refreshing drink \n");       
      }
      
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. newbie question - if statements without conditions
    By c_h in forum C++ Programming
    Replies: 2
    Last Post: 07-18-2008, 10:42 AM
  3. Efficiency of case statements
    By Yasir_Malik in forum C Programming
    Replies: 26
    Last Post: 05-23-2006, 11:36 AM
  4. Alternatives for "if"
    By criticalerror in forum C++ Programming
    Replies: 7
    Last Post: 01-22-2004, 08:03 PM
  5. Statements and Functions
    By GeekFreak in forum C++ Programming
    Replies: 5
    Last Post: 08-15-2002, 12:34 PM