[C++] My First Program =]

Page 1 of 2 12 LastLast
Results 1 to 15 of 25
  1. #1
    Account Upgraded | Title Enabled! gReGiTh is offline
    MemberRank
    Apr 2004 Join Date
    1,360Posts

    [C++] My First Program =]

    I started learning C++ 2 days ago. I've been learning by looking at examples.

    So far, I've made a command line calculator. Check it out and lemme know what you think. It could probably help out other people who are new to programming.

    There is a problem though, it seems to require something non-standard in order to run.. I'm not sure what that something is.. but my computer has it.

    Name:
    Calculatizor
    Source: Nopaste - 6/8/07
    Download: http://gregith.googlepages.com/calculatizor.rar

    Screenshot:
    http://gregith.googlepages.com/clcsreeeny.PNG


  2. #2
    Omega username1 is offline
    MemberRank
    Jul 2004 Join Date
    5,867Posts

    sage

    ...

    I'll be interested once you write a calculator that can evaluate 358+67*17-2/(6-4)+8.

    And WTF is with all the gotos? Have you been learning C++ by looking at Asm code?

    srsly, your code is awful. The flow is convoluted.

  3. #3
    Account Upgraded | Title Enabled! jetgod is offline
    MemberRank
    Mar 2007 Join Date
    Scotland, UKLocation
    1,098Posts

    Re: [C++] My First Program =]

    Only thing that amkes it half-decent is that it;s your first one, but don't overuse the goto. Very irritating.

  4. #4
    Account Upgraded | Title Enabled! gReGiTh is offline
    MemberRank
    Apr 2004 Join Date
    1,360Posts

    Re: sage

    Quote Originally Posted by username1 View Post
    ...

    I'll be interested once you write a calculator that can evaluate 358+67*17-2/(6-4)+8.

    And WTF is with all the gotos? Have you been learning C++ by looking at Asm code?

    srsly, your code is awful. The flow is convoluted.
    Lol, what more did you expect? Anyway, I did that because I couldn't figure out how to get other functions to work... If I put the other functions after main(), then I can't use them in main(), cause they haven't been defined yet. And if I put them before main(), they will execute, but then main() won't execute, it will just be skipped.. and I can't tell each function to go to the next one, cause the next one hasn't been defined yet.

    I'm sure there's probably a better solution to this, but I figured goto worked as a temporary solution.

    I don't see how it's convoluted though.. I tried to make it very easy to add more functions to choose from, and able to be used in different programs without a whole lot needing to be changed.

  5. #5
    Omega username1 is offline
    MemberRank
    Jul 2004 Join Date
    5,867Posts

    sage

    Quote Originally Posted by gReGiTh View Post
    Lol, what more did you expect? Anyway, I did that because I couldn't figure out how to get other functions to work... If I put the other functions after main(), then I can't use them in main(), cause they haven't been defined yet. And if I put them before main(), they will execute, but then main() won't execute, it will just be skipped..
    You must be trolling. Read a book, like "The C Programming Language" by Kernighan and Ritchie.

    If this is real, I suggest you stop writing any further programs until you've read that book -.-

  6. #6
    All is well... Wh005h is offline
    MemberRank
    Feb 2006 Join Date
    TejasLocation
    1,984Posts

    Re: sage

    Quote Originally Posted by username1 View Post
    You must be trolling. Read a book, like "The C Programming Language" by Kernighan and Ritchie.

    If this is real, I suggest you stop writing any further programs until you've read that book -.-
    I personally have more fun learning it by myself. It's more entertaining to just go to one of those sites that lists functions and snippets and what not and learn from those. Sure you don't get the full experience but you learn as you go and when you figure something out it seems much more meaningful.

    But you know more than I do that's for sure. Kudos =p
    Wh005h knows little to none in C++

  7. #7
  8. #8
    Account Upgraded | Title Enabled! jetgod is offline
    MemberRank
    Mar 2007 Join Date
    Scotland, UKLocation
    1,098Posts

    Re: sage

    Quote Originally Posted by gReGiTh View Post
    Lol, what more did you expect? Anyway, I did that because I couldn't figure out how to get other functions to work... If I put the other functions after main(), then I can't use them in main(), cause they haven't been defined yet. And if I put them before main(), they will execute, but then main() won't execute, it will just be skipped.. and I can't tell each function to go to the next one, cause the next one hasn't been defined yet.
    Use function prototypes. Period.
    What compiler are you using? A hand made one no doubt that's barely done. In fact, I'll rewrite this program when I get the time to show you how it's done.

  9. #9
    Account Upgraded | Title Enabled! gReGiTh is offline
    MemberRank
    Apr 2004 Join Date
    1,360Posts

    Re: sage

    Quote Originally Posted by jetgod View Post
    Use function prototypes. Period.
    What compiler are you using? A hand made one no doubt that's barely done. In fact, I'll rewrite this program when I get the time to show you how it's done.
    Visual C++ 2005 Express.

  10. #10
    Account Upgraded | Title Enabled! King Izu is offline
    MemberRank
    Dec 2006 Join Date
    833Posts

    Re: [C++] My First Program =]

    Nopaste - No description

    That compiled perfectly fine.

  11. #11
    Account Upgraded | Title Enabled! gReGiTh is offline
    MemberRank
    Apr 2004 Join Date
    1,360Posts

    Re: [C++] My First Program =]

    Quote Originally Posted by King Izu View Post
    Nopaste - No description

    That compiled perfectly fine.
    Thanks for the effort. I'll check that out right away.

    And username1, no I'm not trolling.

    I completely agree with what wh005h said. Trial and error > reading books to learn. I don't need to be told to RTFM, I'm doing that already. I enjoy trying to get as much done as possible with the little snippets of code I understand, then looking at more code and finding new stuff, figuring out what it does and how to use it properly.

    Of course this program is gonna be god awfull. It was a historical moment for me because I finally got the program to do what I wanted it to do without any major problems. I posted the code as soon as it worked properly and compiled without error. That doesn't mean it's done. Doesn't even mean I think it's a good, or even a decent program.

  12. #12
    Omega username1 is offline
    MemberRank
    Jul 2004 Join Date
    5,867Posts

    sage

    Quote Originally Posted by gReGiTh View Post
    I completely agree with what wh005h said. Trial and error > reading books to learn. I don't need to be told to RTFM, I'm doing that already.
    Apparently you DO need to RTFM moar, as you completely failed at understanding the concept of program flow and how it's done in C, as evidenced by your quote above. This is a perfect example of why you *must* read the books. No exceptions.

    Here's an expression evaluator that *can* parse something like 3+5*10-200/(175*2)+45, and it also illustrates the proper use of goto:
    Code:
    #define P(a,b) (*p==a||*p==b)
    #define D (d?m/n:m*n)
    char *p;
    long r(){
     long a=0,m=1,n=0,d=0,e=1;
    
    a:
     *p-=40;
     if P(3,5) {
      if(e) m*=4-*p;
      else a+=D,n=0,m=4-*p,d=0,e=1;
      p++;
      goto a;
     }
     if P(2,7) {
      m=D;n=0;d=*p-2;e=1;
      p++;
      goto a;
     }
     e=0;
     if(!*p) {
      p++;
      n=r();
      p++;
      goto a;
     }
     if(*p-1&&*p+40) {
      n=n*10+*p-8;
      p++;
      goto a;
     }
     return a+D;
    }
    
    main(int c,char**v) {
     p=v[1];
     printf("%ld",r());
    }
    Umm... not much better. Trades the use of goto with excessive whitespace and needless use of an array.

  13. #13
    Account Upgraded | Title Enabled! jetgod is offline
    MemberRank
    Mar 2007 Join Date
    Scotland, UKLocation
    1,098Posts

    Re: [C++] My First Program =]

    Quote Originally Posted by King Izu View Post
    Nopaste - No description

    That compiled perfectly fine.
    Add to the top:
    Code:
    using namespace std;
    Then you can shorten the std::cout to just cout

  14. #14
    Account Upgraded | Title Enabled! gReGiTh is offline
    MemberRank
    Apr 2004 Join Date
    1,360Posts

    Re: sage

    Code:
    #define P(a,b) (*p==a||*p==b)
    #define D (d?m/n:m*n)
    char *p;
    long r(){
     long a=0,m=1,n=0,d=0,e=1;
    
    a:
     *p-=40;
     if P(3,5) {
      if(e) m*=4-*p;
      else a+=D,n=0,m=4-*p,d=0,e=1;
      p++;
      goto a;
     }
     if P(2,7) {
      m=D;n=0;d=*p-2;e=1;
      p++;
      goto a;
     }
     e=0;
     if(!*p) {
      p++;
      n=r();
      p++;
      goto a;
     }
     if(*p-1&&*p+40) {
      n=n*10+*p-8;
      p++;
      goto a;
     }
     return a+D;
    }
    
    main(int c,char**v) {
     p=v[1];
     printf("%ld",r());
    }
    Thanks username1, This code is really helpful. :)
    Btw I am RTFM'ing.

  15. #15
    Alpha Member evill33t is offline
    MemberRank
    Sep 2005 Join Date
    /int/Location
    2,603Posts

    Re: [C++] My First Program =]

    well normally you never have a reason to use goto :S



Page 1 of 2 12 LastLast

Advertisement