Thread: whats wrong with this code?

  1. #1
    Registered User smahdi1991's Avatar
    Join Date
    Nov 2009
    Posts
    32

    whats wrong with this code?

    hello.
    i am a complete beginner in C, so please bear with me .
    well i wrote a simple program , but i have no idea why it is not working! its pretty simple you know !

    here is my code
    Code:
                  //in the name of god
    
    #include<stdio.h>
    int main()
    {
            int x,y;
            char c;
            printf("\nPlease Enter 2 operands and then your desigred operator(+/*)");
            scanf("%d%d%c",&x,&y,&c);
            
            printf("x = %d,,y = %d  Operator:%c\n",x,y,c);
            
            if (c == '*' )
            {
                   printf("x * y = :%d",x*y);        
            }
            
            else if (c == '+' )
            {
                   printf("x + y:%d",x+y);         
            }
            else
            printf("error happened!");
    
            return 0;
    }
    what is wrong with
    Code:
    scanf("%d%d%c",&x,&y,&c);
    i noticed that if i write
    Code:
    scanf("%d%c%d",&x,&c,&y);
    it would work perfectly! but i really dont know what is the difference between the two!
    would anyone help me ?
    thanks
    Last edited by smahdi1991; 11-01-2009 at 04:08 AM.

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Cause you are putting "3 3 +" and it reads " " as a character :P
    If you put "3 3+" (without space) it works fine

  3. #3
    Registered User
    Join Date
    Oct 2009
    Location
    Indonesia
    Posts
    68
    why error because you scanf %d%d%s in one time,
    the system would be confuse, which one is the first %d,the second, the third when you input at once..
    how if you write the scanf 3 times?
    so
    Code:
    scanf("%d",&(int variable));
    scanf("%d",&(int variable));
    scanf("%s",(stringvariable)); //or you can gets(variabelstring); also

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Do NOT use gets. Use fgets in such case.
    To explain in better english, consider that you enter:
    33+
    scanf will think that 33 is actually one number, not two.
    If you do, 3+3 on the other hand, it will think the first number is 3 (because there is a non-number afterwards) and then a character and later another number.
    Make sense?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Location
    Indonesia
    Posts
    68
    in my compiler(I use visual C), I can use that, because I use"fflush(stdin);"
    so the other buffer from scanf, being erased..
    I don't really know what your compiler is, but it's still same topic "C" code.. hehehe

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    fflush stdin is BAD.
    gets is also NASTY.
    Avoid both.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Oct 2009
    Location
    Indonesia
    Posts
    68
    hmmm I must use it,, or, when I run my program, it will out automaticly when I input..
    any other idea? to avoid it?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Kinshara
    in my compiler(I use visual C), I can use that, because I use"fflush(stdin);"
    so the other buffer from scanf, being erased..
    I don't really know what your compiler is, but it's still same topic "C" code.. hehehe
    If you are talking about the use of gets(), then the problem is that you have no way of limiting the amount of input read. You have to trust that your users do not enter more input than you expect. Thus, you can use gets(), but you should not.

    Likewise, you can use fflush(stdin), but it results in undefined behaviour, since fflush() is only defined for output streams and update streams whose most recent operation was not input. However, you can get away with fflush(stdin) on specific compilers (like the one that you are using), but your code will not be portable.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Kinshara View Post
    hmmm I must use it,, or, when I run my program, it will out automaticly when I input..
    any other idea? to avoid it?
    Yes, you can flush the input buffer. There are lots of examples on the board.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Oct 2009
    Location
    Indonesia
    Posts
    68
    how can I flush the input buffer without useing fflush(stdin)?? can you show me example? or the link to the board?
    sory if I disturb you

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Oct 2009
    Location
    Indonesia
    Posts
    68
    is any negative site if we use fflush stdin for our computer?

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What does that mean? Does it have any bad effect on the computer or something? Is that what you are asking?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Oct 2009
    Location
    Indonesia
    Posts
    68
    yeah..
    sorry if my language is bad!..

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The problem is that it is undefined. No one knows that is going to happen if you use it.
    Your code could crash. It might not work. It might work.
    That is the beauty of undefined. So do not use it, quite simply put.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's wrong with my code?
    By x2x3i5x in forum C Programming
    Replies: 6
    Last Post: 09-28-2009, 11:52 AM
  2. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  3. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 10:11 AM
  4. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 06:32 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM