Need help with "if" statements

This is a discussion on Need help with "if" statements within the C Programming forums, part of the General Programming Boards category; I am trying to create a vending machine program using "if" statements and I need to get the user to ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    9

    Need help with "if" statements

    I am trying to create a vending machine program using "if" statements and I need to get the user to “insert” their money. Since they can’t physically insert money, the program needs to ask them how much they wish to insert and have to check and see if they entered at least $1.00 (using the “if” statements). If they do not insert at least $1.00, it will give them an error message and NOT give them the option to purchase a drink.

    So far this is the coding that I have done using the Miracle C compiler. When I try to compile it, it gives me an error. I am just starting out getting familiarized with the C language and any help would be appreciated kindly.
    Code:
    #include <stdio.h>
    
    int main() 
    {    
      
       printf ("%s","Welcome to Harry's Vending Machine\n");
       
       printf("Enter a value:\n"); 
       
       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");
        
        printf("Thank you for your purchase\n");
        
        printf ("\nEnjoy your refreshing drink and don't forget your change\n");
        exit(0); 
        
       if ("d > a")
       
        printf("Please insert correct amount\n");
        
        scanf("a");
        
    return (0);
    
    }

  2. #2
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    You need to put braces around your if statements. The form is :

    Code:
    if(condition)
    {
           code to execute if condition is true 
    }
    Further, I don't quite know where you get the value of a.
    Also "d > a" is a string, not a condition. d > a, on the other hand, is.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    You have this:

    Code:
    if (a == 1)
    What is that going to do? You are doing a test on a variable that has not been declared (or initialised). Same story here:

    Code:
    if ("d > a")
    Declare those variables first, and always make sure they hold some meaningful value before you attempt to use the value.

    Next, you are not using scanf correctly:
    Code:
    scanf("a");
    Can you see what you are missing?

    Fix those problems and see where you are at. Also, is there any reason why you must use the Miracle C compiler? I understand that it is an old piece of poo - There are some fairly decent free ones out there these days. Bloodshed produces a nice IDE which utilises the GNU C compiler.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    A good start: variable a is not even defined or used, you should really declare a double at the beginning of main. Next, scanf examines a format string, and tries to convert user input accordingly and then puts it in a variable. Learn how to use it correctly, and then learn why scanf is bad, and use other methods of input.
    Code:
    scanf("%1.2f", &a);
    should help you out.
    To let your customer choose their drink, you'll have to use scanf to get another value for their choice (preferably stored in another, different variable). Then depending on what the choice is, you give them their drink!

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    9
    Quote Originally Posted by kermit
    You have this:

    Code:
    if (a == 1)
    What is that going to do? You are doing a test on a variable that has not been declared (or initialised). Same story here:

    Code:
    if ("d > a")
    Declare those variables first, and always make sure they hold some meaningful value before you attempt to use the value.

    Next, you are not using scanf correctly:
    Code:
    scanf("a");
    Can you see what you are missing?

    Fix those problems and see where you are at. Also, is there any reason why you must use the Miracle C compiler? I understand that it is an old piece of poo - There are some fairly decent free ones out there these days. Bloodshed produces a nice IDE which utilises the GNU C compiler.
    Can you please help me correctly declare a variable because everytime I try to do it I get an error. I am using Miracle C because I think it's easier for someone that is just starting out.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by Harryt123
    I am using Miracle C because I think it's easier for someone that is just starting out.
    I think using Miracle C is quite admirable for someone just starting out. It will not tell you when it is doing something wrong with correct code. Trying to guess at things you don't understand when the compiler is of no help and even harmful will truly make your learning experience a hard-earned adventure.

    Consider a good free compiler: Dev-C++. And search the board for "Miracle C" to see just how highly regarded it really is.
    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.*

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Miracle C is the "Herbert Schildt" of compilers.


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

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > Miracle C is the "Herbert Schildt" of compilers.
    At least you could set fire to a HS book - Miracle C has no redeeming features at all.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Do you understand this code?

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <conio.h>
    
    int main ( void )
    {
       int a;
       char name[15];
    
      strcpy(name, "peter");
    
      printf("Hello, %s, how old are you? ");
      scanf("%d", &a);
      printf("So you are %d years old!", a);
      
      getch();
      
      return 0;
    }
    If not, read up on how to read strings in using strcpy, and declaring variables.
    I appogise to the old syntax ir: using conio.h but I was taught C in 1997 when
    I was 14.

  10. #10
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Well those are some nice tricks. Why not explicitly list the arguments for printf though, so the compiler does not have to guess?

    What happens if there is more than one string?

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main ( void )
    {
       int a;
       char name[15];
       char string[15];
    
      
      strcpy(name, "peter");
      strcpy(string, "lalalalal");
    
      printf("Hello, %s, how old are you? ");
      fflush(stdout);
      scanf("%d", &a);
      printf("So you are %d years old!\n", a);
      
       return 0;
    }
    My output: Hello, lalalalal, how old are you?

    Now if I change the printf to look like this:

    Code:
    printf("Hello, %s, how old are you? ", name);
    Then I get the expected output:

    Hello, peter, how old are you?

    And if you know enough to apologise for using getch with conio.h, why don't you know enough not to include it?
    Last edited by kermit; 05-12-2006 at 04:03 AM.

  11. #11
    Registered User
    Join Date
    May 2006
    Posts
    9
    Quote Originally Posted by kermit
    Well those are some nice tricks. Why not explicitly list the arguments for printf though, so the compiler does not have to guess?

    What happens if there is more than one string?

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main ( void )
    {
       int a;
       char name[15];
       char string[15];
    
      
      strcpy(name, "peter");
      strcpy(string, "lalalalal");
    
      printf("Hello, %s, how old are you? ");
      fflush(stdout);
      scanf("%d", &a);
      printf("So you are %d years old!\n", a);
      
       return 0;
    }
    My output: Hello, lalalalal, how old are you?

    Now if I change the printf to look like this:

    Code:
    printf("Hello, %s, how old are you? ", name);
    Then I get the expected output:

    Hello, peter, how old are you?

    And if you know enough to apologise for using getch with conio.h, why don't you know enough not to include it?

    Thank you so much for your help and everyone that posted above you. I appreciate it greatly as I am just starting out and I'm sure you guys have more experience then I do. I just have a couple of questions to ask with two lines of codes that are errors.
    In dev-C++ which I am using over Miracle C because it at least shows which line is in error.
    [warning] comparison between pointer and integer.
    Code:
    {
       if("%d" > 1)
    than the next one that is giving me trouble is the following line.
    [warning] passing arg 1 of `scanf' from incompatible pointer type
    Code:
     scanf(&a,&d);
    I just pasted the two exact lines of code and not the entire code to make it easier to know exactly which code I was reffering to that had the errors, hopefully this will help.

  12. #12
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    The first error references the fact that, again "%d" is a string (or also a character pointer, as is said in the error, but don't worry about that yet.), so the compiler cannot compare it with the value 1, for it to work out, d would have to be an integer of some sort.

    The second error is because the format of scanf you've given is incorrect. It is supposed to be :

    Code:
    scanf(format string ,variables in which to put values read )
    Therefore, a valid use of scanf would be :

    Code:
    scanf("Input an integer : %d", &a); /*  Given that a is declared as an integer  */
    Yours, however, does not do that.

    Cheers.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  13. #13
    Registered User
    Join Date
    May 2006
    Posts
    9
    Quote Originally Posted by Happy_Reaper
    The first error references the fact that, again "%d" is a string (or also a character pointer, as is said in the error, but don't worry about that yet.), so the compiler cannot compare it with the value 1, for it to work out, d would have to be an integer of some sort.

    The second error is because the format of scanf you've given is incorrect. It is supposed to be :

    Code:
    scanf(format string ,variables in which to put values read )
    Therefore, a valid use of scanf would be :

    Code:
    scanf("Input an integer : %d", &a); /*  Given that a is declared as an integer  */
    Yours, however, does not do that.

    Cheers.
    Thank you so much everyone for the time and help you are giving me and I appreciate it kindly. I am going to post the revised version of my coding and please can you check if everything looks better than it was before. I am having a tiny error (null) Parse Error, expecting `'}''
    '' other than that everything seems to be much better.
    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); 
      }

  14. #14
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    Code:
     printf("Enter a value:\n"); 
       scanf("%d",&a);
       { // just delete this brace. It's not necessary.

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your 'name' is only one character in length.
    Your scanf just has a 5 in it. It should have a variable, and be using the & to store in said variable.
    You are missing a closing }. Fix this issue by indenting four spaces every time on the line following the {, ligning up the items for that block, and unindending four spaces every time you put a }. Like so:
    Code:
    int main( void )
    {
        abc
        def
        {
            abc
            def
            {
                abc
                def
            }
            ghi
            jkl
        }
        ghi
        jkl
    }
    Nice and clean.


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

Page 1 of 2 12 LastLast
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, 03: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, 07:03 PM
  5. Statements and Functions
    By GeekFreak in forum C++ Programming
    Replies: 5
    Last Post: 08-15-2002, 12:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21