Thread: Some basic things

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by dwks View Post
    Code:
    for (int i = 0; i < strlen(my_str); i++)
    Consider calculating strlen() outside of that loop -- it's rather expensive in time to compute for every iteration of the loop.
    But then again, most of the times, it shouldn't have noticable performance impact at all unless your code is really time critical.

    Quote Originally Posted by dwks View Post
    Code:
    std::string my_str; // We're assuming the text is stored in my_str
    We're assuming we're in the C++ forum unfoundedly, eh?
    LOL, yes! But it doesn't matter if it's std::string or char* or whatever. The princip if the same.

    Quote Originally Posted by Nathalie View Post
    Okay, thanks for explaining once again! One tiny question more: how do you print the result of the a^b thing?
    To the console screen?
    In C++, I'd use cout, but since this C... meh.
    Code:
    fprintf(stdout, "&#37;i", num)
    ?
    Last edited by Elysia; 11-10-2007 at 02:20 PM.

  2. #17
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Nope. fprintf() takes for its first parameter a FILE* stream, which you can specify as stdout; but printf() automatically prints to stdout. [edit] No fair editing. [/edit]

    I'm sure you can figure it out. Just modify your own code! (I replaced the quotes.)
    Code:
    printf("min &#37;d", min);
    And you'd want a newline on the end, most likely.

    [edit]
    But then again, most of the times, it shouldn't have noticable performance impact at all unless your code is really time critical.
    There probably wouldn't be much of a difference, it's true, but is it really that much harder to declare a variable, save the result in that, and use that variable in the loop expression? strlen() has to iterate over every character in the string, comparing them to 0. It's likely more time-consuming than the rest of your loop put together. [/edit]
    Last edited by dwks; 11-10-2007 at 02:23 PM.
    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.

  3. #18
    Registered User
    Join Date
    Nov 2007
    Posts
    24
    Hard to follow a bit, but thanks anyway. Although I have another small problem, which is that for some reason whatever C Compiler I use, it refuses to build or compile anything. I downloaded and tried Dev-C++ as you recommended, and although it seems easy to use, this too says: " C:\Dev-Cpp\Makefile.win [Build Error] [main.o] Error 1 " when I hit F9.

    I did this with a standard Hello World code I found online, which shouldn't contain any errors. This explanation may be a bit vague, but any idea what is going on?

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by dwks View Post
    [edit]
    There probably wouldn't be much of a difference, it's true, but is it really that much harder to declare a variable, save the result in that, and use that variable in the loop expression? strlen() has to iterate over every character in the string, comparing them to 0. It's likely more time-consuming than the rest of your loop put together. [/edit]
    Each one to their own, I guess.

  5. #20
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by Nathalie View Post
    Hard to follow a bit, but thanks anyway. Although I have another small problem, which is that for some reason whatever C Compiler I use, it refuses to build or compile anything. I downloaded and tried Dev-C++ as you recommended, and although it seems easy to use, this too says: " C:\Dev-Cpp\Makefile.win [Build Error] [main.o] Error 1 " when I hit F9.

    I did this with a standard Hello World code I found online, which shouldn't contain any errors. This explanation may be a bit vague, but any idea what is going on?
    What is your path? Dev-C++ requires the path to be modified before it can compile anything. On Windows Vista this can be a real pain, and you have to do it yourself.

    What operating system are you using? Did you try modifying the path yourself already?

    ETA: dwks, that means I have to replace every “%d” by some other text? What kind of? I didn't write that code, I asked somebody else for help but he was unable to explain or go on any further.
    Here's a brief summary of how printf() works.

    Any characters except '%' printf() prints verbatim to stdout (which is usually the screen, but can be redirected to a file if the user wishes). When printf() sees a '%', it checks the next character. If it's an 's', printf() is expecting a string; a 'd' or an 'i', it looks for an int. It then takes the next parameter that was passed to it, treating it as an int or a string or whatever, and prints that.

    For example:
    Code:
    const char *name = "John";
    int age = 42;
    printf("Hello, %s! I hear you are %d years old.\n", name, age);
    Oh yeah, I forgot about backslashes. When the compiler sees a backslash in a string, it treats the next character specially. "\n" is a newline (like what happens when you press <enter>), "\t" is a tab, and "\\" is a backslash itself.

    printf()'s format specifiers are easy to find online: http://www.cplusplus.com/reference/c...io/printf.html
    And escape sequences like "\n" are too: http://www.cppreference.com/escape_sequences.html
    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.

  6. #21
    Registered User
    Join Date
    Nov 2007
    Posts
    24
    Oh, I didn't know about the paths, that must be the reason. Personally I use Windows Vista, but my friend has Windows XP. Perhaps it is easier to make it work on that? In the end I just wanted to figure it out for him, but I don't need it (although I am interested in programming and wouldn't mind trying).

    If you could explain how to configure the path (XP method is fine), I will have him try it when he gets home.

    Your %s and %d example is really clear, thanks. But didn't you say they don't work in C?

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I think you're confused.
    dwks mentioned that your weird quotes (") didn't work as they should.
    &#37;d and %s is used in function that's available in both C and C++.

  8. #23
    Registered User
    Join Date
    Nov 2007
    Posts
    24
    Oh, sorry about that! Must be because it is late in the evening where I live.

  9. #24
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by Nathalie View Post
    Oh, I didn't know about the paths, that must be the reason. Personally I use Windows Vista, but my friend has Windows XP. Perhaps it is easier to make it work on that? In the end I just wanted to figure it out for him, but I don't need it (although I am interested in programming and wouldn't mind trying).

    If you could explain how to configure the path (XP method is fine), I will have him try it when he gets home.
    For XP, see the bottom of this page. http://www.computerhope.com/issues/ch000549.htm

    You'll want to add C:\Dev-C++\Bin to the end of the path. Or where ever you installed Dev-C++.

    I can't find anything about Vista, and I can't remember how I did it. But next time I'm on my Vista computer I can figure it out for you if you like.

    I think it was Start->Right click on Computer->Properties->Advanced->Environment Variables or something. Who knows.
    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.

  10. #25
    Registered User
    Join Date
    Nov 2007
    Posts
    24
    Thanks! I'll report back whether it worked, probably tomorrow or so. As for Vista, these are the environment variables (my PC is in Dutch, though). I am not sure what to do with them however. If you find some spare time while on your Vista PC, I'd appreciate the help.

  11. #26
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Add ;path_to_dev_cpp\Bin to the end of the PATH variable.

  12. #27
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Since Dev-C++ appears to be installed in C:\Dev-Cpp, add
    Code:
    ;C:\Dev-Cpp\Bin
    to the end of your PATH variable. In the bottom scrollbox, scroll until you see PATH, then click edit (or whatever the Dutch for that is!), and type that at the end.
    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.

  13. #28
    Registered User
    Join Date
    Nov 2007
    Posts
    24
    Quote Originally Posted by dwks View Post
    Since Dev-C++ appears to be installed in C:\Dev-Cpp, add
    Code:
    ;C:\Dev-Cpp\Bin
    to the end of your PATH variable. In the bottom scrollbox, scroll until you see PATH, then click edit (or whatever the Dutch for that is!), and type that at the end.
    I did so, unfortunately still getting the same error. Maybe I need to reboot or so? I will go to bed soon anyway, so I can check that in the morning.

  14. #29
    Registered User
    Join Date
    Nov 2007
    Posts
    24
    We got the compiler to work, but are now stuck with the powers program. We need a program that calculates a^b when a and b are given (and they can be both positive and negative). The code we have so far:

    Code:
    #include <stdio.h>
                    int i,m;
                    int k=1
                    int j=0;
                    int main()
    
                    {
                    printf("base....:D");
                    scanf("%d",&k);
                    printf("\n exponent......:D");
                    scanf("%d",&i);
                    for(j=0,j<i,j++)
                    {
                    m*=k;
                    }
                    printf("%d to the power of %d is %d ",m,i,k);
                    return 0;
                    }
    Does anyone know why it isn't working? Also, how can we make it work for negative numbers as well as positive?

  15. #30
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Does anyone know why it isn't working?
    I believe you have a couple of compile errors: you are missing the terminating semi-colon on line 3, and your for loop is not correct (hint: commas versus semi-colons).

    Aside from these errors, you might want to move your variables to main() such that they are no longer global variables. It makes no difference in this case, but it is good practice.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. noob with basic q's
    By SimplyComplex in forum C++ Programming
    Replies: 8
    Last Post: 11-19-2006, 01:17 PM
  2. Basic things I need to know
    By maxorator in forum C++ Programming
    Replies: 53
    Last Post: 10-15-2006, 04:39 PM
  3. what are your thoughts on visual basic?
    By orion- in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 09-22-2005, 04:28 AM
  4. visual basic vs C or C++
    By FOOTOO in forum Windows Programming
    Replies: 5
    Last Post: 02-06-2005, 08:41 PM
  5. Basic Window Creation, Dev C++ 4.9.9.0 Linking Error
    By Tronic in forum Windows Programming
    Replies: 2
    Last Post: 11-27-2004, 06:03 PM