#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0

    Question Whats wrong with this code


    WHAT IS WRONG WITH THIS CODE? IT SCAN num1 IT SCAN num2 THEN IT SHOW ENTER OPERATOR AND THEN WITHOUT SCANING OPERATOR IT SHOW WRONG OPERATOR IT DOES NOT SCAN op WHY I DONT KNOW PLEASE HELP ME GUYx :confused:

    Code:
    #include <stdio.h>
        main ()
        {
        int num1, num2;
        char op;
    
        printf("Enter 1st Number: ");
            scanf("%d", &num1);
        printf("Enter 2nd Number: ");
            scanf("%d", &num2);
        printf("Enter Operator");
            scanf("%c", &op);
    
    switch (op)
    {
    case '+':
                printf("Sum is %d", num1+num2);
                    break;
    
    case '-':
                printf("Difference is %d", num1-num2);
                    break;
    
    case '*':
                printf("Multiplication is %d", num1*num2);
                    break;
    
    case '/':
                printf("Division is %d", num1/num2);
                    break;
    
    default:
                printf("Wrong Operation");
        }
    
    
    }

    Comments on this post

    • requinix disagrees : DON'T YELL AT US
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,242
    Rep Power
    2222
    Even if you are getting frustrated, that is never an excuse to yell at us!

    The problem is that your program is doing exactly what you are telling it to do. Even though that's not what you want it to do. Remember, computers never do what we want them to do, but only what we tell them to do.

    Code:
    scanf("%d", &num2);
        printf("Enter Operator");
            scanf("%c", &op);
    When scanf converts a value from the input stream (stdin), it stops and leaves whatever follows in the input buffer, including the characters for the Enter key that you pressed when you entered that value. So when you read the decimal value for num2, scanf skips white space (spaces, newlines, tabs, etc) until it finds the next printable characters which it then tries to convert into an integer value and as soon as it hits a non-digit it stops. Then you tell scanf to read in the next character, regardless of what it is, even if it is nothing but white space, which it does. You should output the ASCII code for what op has read in; that should be included in your default case's printf("Wrong Operation");. You will most likely find it to be ASCII code 13 or 10, though it could be 32 if you had added a space after that second number. And that is exactly what you are telling scanf to do.

    What you want is for scanf to ignore all white space that occurs before a printable character. This is how you tell it to do that:
    scanf(" %c", &op);
    Please note the space that precedes the percent sign. That tells scanf to expect and to ignore any and all white space that occurs before the printable character that you want it to read in.
  4. #3
  5. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,117
    Rep Power
    1803
    Originally Posted by uzairali001
    WHAT IS WRONG WITH THIS CODE?
    What, apart from the bizarre, arbitrary and unconventional indentation you mean?

    Comments on this post

    • dwise1_aol agrees : And the implied int return type for main while never actually returning anything?
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,242
    Rep Power
    2222
    Also cross-posted: http://stackoverflow.com/questions/1...-in-c-language

    Makes me regret having helped him.

    Comments on this post

    • clifford agrees : But on Stackoverflow, you can down-vote, vote-to-close, and vote-to-delete, for questions not quite there, you can edit them if you are feeling helpful/charritable. This one was closed in minutes and has attracted a -9 score and a vote to delete.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0

    Thumbs up


    Originally Posted by dwise1_aol
    Also cross-posted:

    Makes me regret having helped him.
    thanks for helping me
    they closed my thread without helping me thats why i posted problem here
  10. #6
  11. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,117
    Rep Power
    1803
    Originally Posted by uzairali001
    they closed my thread without helping me thats why i posted problem here
    On Stackoverflow you can edit your posts to make them more acceptable to the community and thus avoid down-voting and closure. It is well to take comment advice as soon as possible to improve its quality. You were asked not to SHOUT on Stackoverflow, and you persisted in exactly that when you posted here.

    Moreover the Stackoverflow community work to avoid duplication - it is a hybrid forum/wiki format and duplicates will get quickly closed and deleted, and the misapplication of scanf() is covered extensively already - you should have searched first. The idea on Stackoverflow is that becomes a massive searchable Q&A database of high quality questions and answers, whereas in the forum format here, old questions are quickly forgotten and hard to find. You question and any answers have to be of sufficiently high quality to survive on Stackoverflow. That may seem somewhat elitist, but it makes it a far more useful resource.

    In your case I think it was perhaps somewhat harsh, it could have been edited for you, or marked it as a duplicate of an existing question. It appears that at least one user did attempt an edit, but gave up. I am not clear why, perhaps he lacked the time or inclination - no body has to help.
  12. #7
  13. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,117
    Rep Power
    1803
    Originally Posted by clifford
    [...] it could have been edited for you [...]
    Because I would not want your first experience on the excellent Stackoverflow to discourage you from using it again, I have "fixed" your post there, you should look at the changes to understand where you went wrong. I can't fix your question here. The changes include:

    • Normal typography (no shouting).
    • Conventional code formatting
    • ISO compliant code.
    • Accurate English (though not having English as a first language is not a something anyone will criticise you for).
    • Depersonalised (no pleading "help me", no addressing the "guys" or anyone in particular, just the facts - that's how Stackoverflow likes it).
    • More relevant tags.


    That said, the issue is dealt with all over the Internet, it is one of the most common issues a novice encounters and is even mentioned in the sticky-thread of this very forum. Consequently I did not vote to reopen it, but it may stop attracting down-votes. However to be honest, if you have solved the issue, to protect your SO rep further I would simply delete the question yourself ; that way it cannot be further down-voted. Even closed question can be voted on on SO.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0
    thanks dear
    actually i m not native speaker thats why i dont know how to post
    and i m not shouting
  16. #9
  17. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,117
    Rep Power
    1803
    Originally Posted by uzairali001
    and i m not shouting
    You misunderstand; in electronic textual communication, writing in ALL CAPITALS is regarded as shouting. It is not considered polite. It is also hard to read any moderately long piece of text written in that way.

    Where I am from, addressing someone as "dear" is likely to be considered rude too. Unless you wereMicheal Winner of course, who made a career from being rude in any case. ;)
    Last edited by clifford; March 2nd, 2013 at 03:52 PM.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    1
    Rep Power
    0

    Smile Nee to make minor adjustment


    Before
    Code:
    printf("Enter Operator");
    line add
    Code:
    fflush(stdin)
    Originally Posted by uzairali001
    WHAT IS WRONG WITH THIS CODE? IT SCAN num1 IT SCAN num2 THEN IT SHOW ENTER OPERATOR AND THEN WITHOUT SCANING OPERATOR IT SHOW WRONG OPERATOR IT DOES NOT SCAN op WHY I DONT KNOW PLEASE HELP ME GUYx :confused:

    Code:
    #include <stdio.h>
        main ()
        {
        int num1, num2;
        char op;
    
        printf("Enter 1st Number: ");
            scanf("%d", &num1);
        printf("Enter 2nd Number: ");
            scanf("%d", &num2);
            fflush(stdin);
        printf("Enter Operator");
            scanf("%c", &op);
    
    switch (op)
    {
    case '+':
                printf("Sum is %d", num1+num2);
                    break;
    
    case '-':
                printf("Difference is %d", num1-num2);
                    break;
    
    case '*':
                printf("Multiplication is %d", num1*num2);
                    break;
    
    case '/':
                printf("Division is %d", num1/num2);
                    break;
    
    default:
                printf("Wrong Operation");
        }
    
    
    }

    Comments on this post

    • ptr2void disagrees : No, you should never use fflush(stdin)!
  20. #11
  21. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,413
    Rep Power
    1871
    Flushing stdin isn't specified by the standard.
    read this
    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

IMN logo majestic logo threadwatch logo seochat tools logo