Yay I wrote my first program

Discussion in 'Mac Programming' started by ghall, Oct 10, 2007.

  1. ghall macrumors 68040

    ghall

    Joined:
    Jun 27, 2006
    Location:
    Rhode Island
    #1
    I'm learning C++ for a project at school, and I have successfully wrote my own calculator app. It's very basic (gotta' start somewhere) and runs in command line. Check it out if you want, and tell me what you think. I have also attached the source code if anyone wants to critique me, or if anyone has any suggestions. It has quite a few bugs as well. Please keep in mind, this is the first program I have ever written.
     

    Attached Files:

  2. sord macrumors 6502

    Joined:
    Jun 16, 2004
    #2
    Few comments:
    scanf is very unsafe, though fine if you know you will get the proper input and aren't worrying about the user at all (just for future notice)

    Also, stdio.h, scanf, and printf are for C, you may want to look into C++ inputs/outputs

    case 'meaning of life' is actually an error, you can't turn a string into a character, especially not that way. Your compiler should say things like:
    main.cpp:26:9: warning: character constant too long for its type
    main.cpp:26: warning: overflow in implicit constant conversion

    times2 method is unused

    No need to initialize x and y

    int main typically takes 2 parameters - int argc, and char *argv[]

    I know, its your first program so please take these as constructive critisism -- welcome to the wonderful world of programming! I've been doing it for 11 years now and still love it (though I get paid for it)
     
  3. ghall thread starter macrumors 68040

    ghall

    Joined:
    Jun 27, 2006
    Location:
    Rhode Island
    #3
    I though I had to. That's what I was told anyway. Could it have something to do with the fact that I originally wrote the code to run on Linux?
     
  4. sord macrumors 6502

    Joined:
    Jun 16, 2004
    #4
    No you do not have to, and no it does not matter which operating system.
    By declaring int x and int y, you are declaring 2 4 byte segments of memory. Then, regardless of what the values are, you are writing to them (via the scanf).

    You may be thinking of if you want x or y to be 0. You can never assume the value of x or y by just declaring it. If you set it before doing anything else (like with your scanf) you have nothing to worry about, but you can't assume that "int x; x++;" will end up as 1 - it will be quite random.
     
  5. ghall thread starter macrumors 68040

    ghall

    Joined:
    Jun 27, 2006
    Location:
    Rhode Island
    #5
    Okay, so I fixed it and now that section looks like this:

    Code:
    int main (){
    	int x=0;
    	int y=0;
    	char function;
    Is this right?
     
  6. sord macrumors 6502

    Joined:
    Jun 16, 2004
    #6
    It will work, just as it used to, however you are still instantiating the variables when you don't need to.

    Consider this:
    1) You declare x (which becomes basically random because its whatever was in memory at that spot)
    2) You change the value of x to the value of scanf
    3) ...

    Now consider this:
    1) You declare x and set it to 4
    2) You change the value of x to the value of scanf
    3) ...

    If you are going to assign x in step 2, there's no need to set it in step 1. (Note that this is not a big deal at all, but your computer will have to load x from memory, set it to 4, then store it in memory if you give it a value)

    So you can do:
    Code:
    int main(int argc, char *argv[])
    {
         int x;
         int y;
         ...
    What I was saying about assigning it to 0 is if you were expecting it to already be 0. For example:
    Code:
    int x;
    while (somecondition)
    {
         x++;
    }
    If somecondition lasts 4 times, you might want to think x would be 4, however it really won't be because it was initialized as whatever number was in memory.
     
  7. sord macrumors 6502

    Joined:
    Jun 16, 2004
    #7
    As a quick experiment, keep your variables set, and check the file size of your binary, then don't set them, your program will probably be around 12 bytes smaller.
     

Share This Page