Thread: i need a little help here

  1. #16
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    So you have to apply petty syntax tricks to make it work.
    Just use printf to build the format string and then - scanf
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  2. #17
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Elysia View Post
    Yes, I mean it can, but what sort of bizzare and newbie-unfriendly syntax is that?
    How is it any more bizarre than "%s" in the first place? I mean seriously, a percent sign? Who the hell would guess that one?

    You should try injecting a little more "H" into your "IMO". You're arguing about peanuts here.

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Whatever you say, but I can remember %s, but not %[\n..], whatever.
    There are many ways around it, but that kind of defies the use of the original function in the first place.
    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.

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Whatever you say, but I can remember %s, but not %[\n..], whatever.
    I do not buy that argument since the syntax is less complicated than many regex rules in use today. It basically specifies a list of characters to match, with the ^ negating it to not match any in the list of characters. That a preceding number specifies the number of characters to match is also easy to grasp.

    In my opinion, what makes scanf() less desirable for reading in a string as compared to fgets() is that it is more cumbersome to specify the length of the string. You either have to hardcode it into the format string, or you build the format string with sprintf() as noted by vart. With fgets(), you just pass the length of the string as an argument.
    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

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Too complex to me. Doesn't matter if it's less complex to X, if I can't remember it, then the function is really no use to me, is it (emphasis on me, here)?
    That's why we usually pass size as an argument instead.
    Disclaimer: this only applies to me in y humble opinion.
    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.

  6. #21
    Registered User
    Join Date
    Jan 2008
    Posts
    3
    ok is there anything with this now.


    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
        int c;
        char operation;
        float x;
        float y;
        float answer;
        printf(" Welcome to TJ's Calculator.\n");
        printf(" Please type the operation, and then the two numbers.\n");
        scanf("%c",&operation);
        scanf("%f",&x);
        scanf("%f",&y);
        
        
        while((c = getchar()) != '\n' && c != EOF);
        {
        if(operation='+')
        {
        answer=x + y;
        }
         else if (operation='*')
        {
        answer=x * y;
        }
         else if (operation='-')
        {
        answer=x - y;
        }
        else  
        {
        answer=x / y;
        }
    }
        printf(" The answer is %f!\n",answer);
        printf(" Thankyou for using TJ's Calculator.\n");
         while((c = getchar()) != '\n' && c != EOF);
        return(0);

  7. #22
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Missing indentation
    missing closing breket
    missing space in thankyou
    printing of uninitialized answer when operation is different from one of supported
    using of if/else sequence where you should really use switch
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #23
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    To continue . . .
    • use of single-equals assignment rather than double-equals comparison in if statements;
    • unnecessary block enclosing the if/else construct;
    • no check for zero in division, so division by zero is a real possibility;
    • unnecessary header file <stdlib.h> included. (Okay, that's really being pedantic.)

    Missing indentation
    Ah, but did you have a look at the original program? There's a world of a difference. In the positive direction.

    missing space in thankyou
    Not to mention extra spaces at the beginning of each printf() . . . .

    printing of uninitialized answer when operation is different from one of supported
    Ah, not quite. Look closely: division is the default.

    using of if/else sequence where you should really use switch
    A switch might be more suited to this particular situation, but of course if/else works as well.

    But on the whole -- it's much better than your first attempt. The only serious language issues are the use of '=' instead of '==' in the if statements and the lack of a final closing curly brace, though that could have been a copy-and-paste error. Everything else is pretty minor . . . with the possible exception of /0.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed