Thread: Problem with Scanf and if else statement

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    20

    Problem with Scanf and if else statement

    Hi guys, I have a problem with this question, where the amount is constantly $0 and at the end if the user enters Y/N it just closes without saying "Transaction Completed/Cancelled".

    Any tips on how to fix this?
    Thanks

    Code:
    #include<stdio.h>
    #include<iostream>
    #include<math.h>
    
    
    int main()
    {
        int numofbees;
        float price = 5;
        char query;
        char Y;
        char N;
    
    
        printf("How many bees would you like to purchase?\n");
                  scanf("%d", &numofbees);
            printf("That will be $%d, confirm? (Y/N)\n", price*numofbees);
                  scanf("%c", &query);
            if(query == Y)
                printf("Transaction completed.");
            else if(query == N)
                  printf("Transaction cancelled.");
    
    
            getchar();
            getchar();
            return 0;
        }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    There are a couple of things wrong with your program. Your first problem is being caused by the first scanf() leaving the end of line character in the input buffer, which your second scanf() accepts for it's entry. To fix this problem you need to ignore that character, the easiest way would be to add a space before your "%c" in your second scanf():
    Code:
    scanf(" %c", &query);
    The next problem is with your if statements,
    Code:
     if(query == Y)
    You are comparing query to the value held in your Y variable. You have not initialized this variable, it could therefore hold anything. But you really don't need a variable, you can compare query to a const character like:
    Code:
     if(query == 'Y')
    Note the single quotes. You have the same problem with the second if statement. I myself would only use a simple else, not the else if. That way if the user does not enter Y you will get the second message. Also be aware of the fact that C is case sensitive, Y does not equal y.

    Jim
    Last edited by jimblumberg; 12-19-2011 at 02:19 PM.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    What are the values contained within your variables Y and N?
    "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 2011
    Posts
    25
    In addition to what's being mentioned, you don't need math.h and you should get rid of the elseif() and just change it to a single if() statement.

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    20
    I see, I have managed to solve the problem regarding Transaction Accepted/Cancelled. However, I still can't find what is the problem which is withholding it from executing the proper price.

    EDIT: In addition, regarding what jimblumberg said, would it be more suitable to use 1/2 instead of (Y/N) as && and || operators didn't work as planned.
    Last edited by bonett09; 12-19-2011 at 02:52 PM.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Why didn't the && and || operators work. Show the code where you tried using them.

    Jim

  7. #7
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Quote Originally Posted by bonett09 View Post
    I see, I have managed to solve the problem regarding Transaction Accepted/Cancelled. However, I still can't find what is the problem which is withholding it from executing the proper price.

    EDIT: In addition, regarding what jimblumberg said, would it be more suitable to use 1/2 instead of (Y/N) as && and || operators didn't work as planned.
    You declared price as float and numofbees as int. When you multiply them together numofbees will be cast 'up' as float and the result will be float. %d format specifier is therefore wrong.

  8. #8
    Registered User
    Join Date
    Dec 2011
    Posts
    20
    Quote Originally Posted by jimblumberg View Post
    Why didn't the && and || operators work. Show the code where you tried using them.

    Jim
    Code:
    #include<stdio.h>
    #include <iostream>
    
    
    int main()
    {
        int numofbees;
        float price = 5;
        char query;
    
    
        printf("How many bees would you like to purchase?\n");
            scanf("%d", &numofbees);
        printf("That will be $%f, confirm? (Y/N)\n", price*numofbees);
            scanf(" %c", &query);
    
    
            if(query == 'y'&&'Y') {
                printf("Transaction completed.");
        }
            else {
                printf("Transaction cancelled.");
        }
            getchar();
            getchar();
            return 0;
        }
    It accepts anything as a 'Yes'/Transaction Complete.
    Last edited by bonett09; 12-19-2011 at 06:34 PM. Reason: error

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    In the following snippets both sides of the && must be complete. Also in this case you probably want to use the || (or) instead of and (&&).
    Code:
    if(query == 'y'&&'Y')
    Like:
    Code:
    if(query == 'y' || guery == 'Y')
    Jim

  10. #10
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Your test is wrong on 2 counts.
    Code:
    if(query == 'y'&&'Y')
    That if statement says: if query's value is 'y' AND character 'Y'. Character 'Y' is non-zero and is always true. So the only time that statement should evaluate to true is when you enter 'y'. So what you need is the equality test == on both sides of the logical operator &&. But, the second problem is that query can't hold the 'y' AND 'Y' at the same time. You should be using the OR operator.
    Code:
    if( query == 'y' || query == 'Y' )

  11. #11
    Registered User
    Join Date
    Dec 2011
    Posts
    20
    Thanks guys, that solved it

  12. #12
    Registered User
    Join Date
    Dec 2011
    Posts
    69
    I just went ahead and altered the original code to provide a better result.

    Code:
    #include <stdio.h>
    
    
    int main()
    {
        int numofbees, price = 5;
        char query;
     
        printf("How many bees would you like to purchase?\n");
                  scanf("%d", &numofbees);
        printf("That will be $%d.00, confirm? (Y/N)\n", price*numofbees);
                  scanf(" %c", &query);
        query == 'Y' || query == 'y' ? printf("Transaction completed.") :
                  printf("Transaction cancelled.");
    
    
            getchar();
            getchar();
            return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-30-2011, 09:59 AM
  2. error with scanf statement in codeblocks
    By narendrav in forum C Programming
    Replies: 1
    Last Post: 03-19-2011, 11:25 AM
  3. scanf with switch statement, weird behavior
    By ademkiv in forum C Programming
    Replies: 1
    Last Post: 11-22-2010, 10:49 AM
  4. scanf statement prob
    By vijith in forum C Programming
    Replies: 1
    Last Post: 06-24-2010, 12:22 AM
  5. problems with scanf/if statement plz help
    By dezz101 in forum C Programming
    Replies: 22
    Last Post: 03-30-2008, 01:12 PM