View Poll Results: Should I smoke crack while programming?

Voters
25. You may not vote on this poll
  • Yes! It gives you that "Creative Edge"

    4 16.00%
  • Why are you asking me these questions?

    5 20.00%
  • Mommy took my crack pipe! *Cries*

    2 8.00%
  • You are so stupid.

    14 56.00%

Hehe, look at this code. (Not looking for help)

This is a discussion on Hehe, look at this code. (Not looking for help) within the C++ Programming forums, part of the General Programming Boards category; We'll i got bored, and decided to write an app. Yay. Here it is in all it's ugliness. Feel free ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    31

    Hehe, look at this code. (Not looking for help)

    We'll i got bored, and decided to write an app. Yay. Here it is in all it's ugliness. Feel free to make fun of me. Or whatever.

    Code:
    /* 
       Name: D&D 3rd Ed Stat Roller.
       Author: Eric Volovski
       Description: A stat roller for 3rd edition Dungeons and Dragons.
       Date Created: 5-2-02 (12:03 AM EST - 1:53 AM EST)
       Copyright: Freeware
    */
    
    //OK, first off im way too tired to put in functions for all the stat rolls tonight, so
    // for now im just going to have to use goto's! Gasp!
    // I should also make a function for finding the lowest number... actually there problly is one...
    
    
    #include <iostream.h>
    #include <stdlib.h>
    #include <time.h>
    
    int race(int str, int con, int in, int wis, int cha, int dex);
    int roll(int str, int con, int in, int wis, int cha, int dex);
    int final(int st2, int co2, int in2, int wi2, int ch2, int de2);
    
    int main()
    {
     int str=0, in=0, wis=0, con=0, dex=0, cha=0;
     cout<<"Welcome to Eric's D&D character stat roller. (3rd Ed)"<<endl;
     srand(time(NULL));
     race(0,0,0,0,0,0);
    }
    
    // Makes necessary adjustments to stats depending on race.
    int race(int str, int con, int in, int wis, int cha, int dex)
    {
     int menu;
     cout<<"Please choose from the following races"<<endl;
     cout<<"1, Human."<<endl;
     cout<<"2, Dwarf."<<endl;
     cout<<"3, Elf."<<endl;
     cout<<"4, Gnome."<<endl;
     cout<<"5, Half-Elf."<<endl;
     cout<<"6, Half-Orc."<<endl;
     cout<<"7, Halfling."<<endl;
     cin>>menu;
     switch(menu)
     {
      case 1:
       cout<<"Human, no adjustments."<<endl;
       roll(0,0,0,0,0,0);
       break;
      case 2:
       cout<<"Dwarf, +2 Constitution & -2 Charisma."<<endl;
       roll(0,2,0,0,-2,0);
       break;
      case 3:
       cout<<"Elf, -2 Constitution & +2 Dexterity."<<endl;
       roll(0,-2,0,0,0,2);
       break;
      case 4:
       cout<<"Gnome, -2 Strength & +2 Constitution."<<endl;
       roll(-2,2,0,0,0,0);
       break;
      case 5:
       cout<<"Half-Elf, no adjustments."<<endl;
       roll(0,0,0,0,0,0);
       break;
      case 6:
       cout<<"Half Orc, +2 Strength, -2 Intelligence & -2 Charisma."<<endl;
       roll(2,0,-2,0,-2,0);
       break;
      case 7:
       cout<<"Halfling, +2 Dexterity & -2 Strength."<<endl;
       roll(-2,0,0,0,0,-2);
       break;
      default:
       cout<<"Bad input, returning to main."<<endl;
       main();
       break;
     }
    }
    
    int roll(int str, int con, int in, int wis, int cha, int dex)
    {
     int r1, r2, r3, r4;
     int st1, co1, in1, wi1, ch1, de1;
     cout<<"Generating Stats..."<<endl;
    
     //This is the Confusing part :)
    
     //====================================================STR====================
    
     r1 = 1 + rand() % 6;
     r2 = 1 + rand() % 6;
     r3 = 1 + rand() % 6;
     r4 = 1 + rand() % 6;
    
     if(r1 <= r2 && r1 <= r3 && r1 <= r4)  //If the first roll is lowest
     {
      st1 = r2 + r3 + r4 + str;         //Str = Highest three rolls + race modifier.
      goto con;
     }
    
     if(r2 <= r1 && r2 <= r3 && r2 <= r4)   //If the second roll is the lowest
     {
      st1 = r1 + r3 + r4 + str;
      goto con;
     }
    
     if(r3 <= r1 && r3 <= r2 && r3 <= r4)   //If the third roll is the lowest
     {
      st1 = r1 + r2 + r4 + str;
      goto con;
     }
    
     if(r4 <= r1 && r4 <= r2 && r4 <= r3)   //If the fourth roll is the lowest
     {
      st1 = r1 + r2 + r3 + str;
      goto con;
     }
     else
     {
      main();
     }
    
     //Yeah i know i COULD use a loop, but thats just too damn confusing.
     //I'd rather just make it longer :)
    
     //====================================================CON====================
     con:
     r1 = 1 + rand() % 6;
     r2 = 1 + rand() % 6;
     r3 = 1 + rand() % 6;
     r4 = 1 + rand() % 6;
    
     if(r1 <= r2 && r1 <= r3 && r1 <= r4)  //If the first roll is lowest
     {
      co1 = r2 + r3 + r4 + con;
      goto in;
     }
    
     if(r2 <= r1 && r2 <= r3 && r2 <= r4)   //If the second roll is the lowest
     {
      co1 = r1 + r3 + r4 + con;
      goto in;
     }
    
     if(r3 <= r1 && r3 <= r2 && r3 <= r4)   //If the third roll is the lowest
     {
      co1 = r1 + r2 + r4 + con;
      goto in;
     }
    
     if(r4 <= r1 && r4 <= r2 && r4 <= r3)   //If the fourth roll is the lowest
     {
      co1 = r1 + r2 + r3 + con;
      goto in;
     }
     else
     {
      main();
     }
    
     //====================================================INT====================
     in:
     r1 = 1 + rand() % 6;
     r2 = 1 + rand() % 6;
     r3 = 1 + rand() % 6;
     r4 = 1 + rand() % 6;
    
     if(r1 <= r2 && r1 <= r3 && r1 <= r4)  //If the first roll is lowest
     {
      in1 = r2 + r3 + r4 + in;
      goto wis;
     }
    
     if(r2 <= r1 && r2 <= r3 && r2 <= r4)   //If the second roll is the lowest
     {
      in1 = r1 + r3 + r4 + in;
      goto wis;
     }
    
     if(r3 <= r1 && r3 <= r2 && r3 <= r4)   //If the third roll is the lowest
     {
      in1 = r1 + r2 + r4 + in;
      goto wis;
     }
    
     if(r4 <= r1 && r4 <= r2 && r4 <= r3)   //If the fourth roll is the lowest
     {
      in1 = r1 + r2 + r3 + in;
      goto wis;
     }
     else
     {
      main();
     }
    
     //====================================================WIS====================
     wis:
     r1 = 1 + rand() % 6;
     r2 = 1 + rand() % 6;
     r3 = 1 + rand() % 6;
     r4 = 1 + rand() % 6;
    
     if(r1 <= r2 && r1 <= r3 && r1 <= r4)  //If the first roll is lowest
     {
      wi1 = r2 + r3 + r4 + wis;
      goto cha;
     }
    
     if(r2 <= r1 && r2 <= r3 && r2 <= r4)   //If the second roll is the lowest
     {
      wi1 = r1 + r3 + r4 + wis;
      goto cha;
     }
    
     if(r3 <= r1 && r3 <= r2 && r3 <= r4)   //If the third roll is the lowest
     {
      wi1 = r1 + r2 + r4 + wis;
      goto cha;
     }
    
     if(r4 <= r1 && r4 <= r2 && r4 <= r3)   //If the fourth roll is the lowest
     {
      wi1 = r1 + r2 + r3 + wis;
      goto cha;
     }
     else
     {
      main();
     }
    
     //====================================================CHA====================
     cha:
     r1 = 1 + rand() % 6;
     r2 = 1 + rand() % 6;
     r3 = 1 + rand() % 6;
     r4 = 1 + rand() % 6;
    
     if(r1 <= r2 && r1 <= r3 && r1 <= r4)  //If the first roll is lowest
     {
      ch1 = r2 + r3 + r4 + cha;
      goto dex;
     }
    
     if(r2 <= r1 && r2 <= r3 && r2 <= r4)   //If the second roll is the lowest
     {
      ch1 = r1 + r3 + r4 + cha;
      goto dex;
     }
    
     if(r3 <= r1 && r3 <= r2 && r3 <= r4)   //If the third roll is the lowest
     {
      ch1 = r1 + r2 + r4 + cha;
      goto dex;
     }
    
     if(r4 <= r1 && r4 <= r2 && r4 <= r3)   //If the fourth roll is the lowest
     {
      ch1 = r1 + r2 + r3 + cha;
      goto dex;
     }
     else
     {
      main();
     }
    
     //====================================================DEX====================
     dex:
     r1 = 1 + rand() % 6;
     r2 = 1 + rand() % 6;
     r3 = 1 + rand() % 6;
     r4 = 1 + rand() % 6;
    
     if(r1 <= r2 && r1 <= r3 && r1 <= r4)  //If the first roll is lowest
     {
      de1 = r2 + r3 + r4 + dex;
      goto end;
     }
    
     if(r2 <= r1 && r2 <= r3 && r2 <= r4)   //If the second roll is the lowest
     {
      de1 = r1 + r3 + r4 + dex;
      goto end;
     }
    
     if(r3 <= r1 && r3 <= r2 && r3 <= r4)   //If the third roll is the lowest
     {
      de1 = r1 + r2 + r4 + dex;
      goto end;
     }
    
     if(r4 <= r1 && r4 <= r2 && r4 <= r3)   //If the fourth roll is the lowest
     {
      de1 = r1 + r2 + r3 + dex;
      goto end;
     }
     else
     {
      main();
     }
    
     //Well, that was pleasant :)
     end:
     final(st1, co1, in1, wi1, ch1, de1);
    
    }
    
    int final(int st2, int co2, int in2, int wi2, int ch2, int de2)
    {
     system("cls");
     cout<<"You're stats are:"<<endl;
     cout<<"Strength:       "<<st2<<endl;
     cout<<"Dexterity:      "<<de2<<endl;
     cout<<"Intelligence:   "<<in2<<endl;
     cout<<"Wisdom:         "<<wi2<<endl;
     cout<<"Charisma:       "<<ch2<<endl;
     cout<<"Constitution:   "<<co2<<endl;
     system("pause");
    }
    To all of those people who actually read all that, i am sorry.

  2. #2
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    Feel free to make fun of me.
    You make this to easy.....
    Your feet smell bad.
    You have a bad haircut.
    You Mom always dresses you funny.
    (j/k)

    goto's == bad
    your int main() isn't returning a value.
    you could replace all those goto's with a set of if/else's or switch/case that breaks on success, then goes to the next one.

    //Yeah i know i COULD use a loop, but thats just too damn confusing.
    //I'd rather just make it longer
    Are you just a glutton for punishment . If this works for you (I haven't tried compiling it yet) then congrats on finishing a project (and unique one at that, ie: not TicTacToe).

  3. #3
    Registered User InFeStEd-ArCh0n's Avatar
    Join Date
    Apr 2002
    Posts
    63

    Question Gotos... bad?

    Why are goto's bad? If statements in lower level languages are just jump statements, then why are goto's so bad. They both do the same thing, do they not? I'd assume all a goto statement is is a "jmp" instruction (on x86), which could be nothing more than a "je" and "jne" for higher level if statements and while statements and so forth. So what is the big deal about gotos?
    -Mike
    {InFeStEd-ArCh0n}

  4. #4
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680

    Re: Gotos... bad?

    Originally posted by InFeStEd-ArCh0n
    Why are goto's bad? If statements in lower level languages are just jump statements, then why are goto's so bad.
    Then go make your program in a lower level language. If you want to use goto's in a higher level language then use VB or something like that.

  5. #5
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    Thanks, Monster.

    I'm not saying goto's are bad because of how it translates into a lower level language. I'm saying goto's are a bad practice in C/C++ code.

    goto's can make your code hard to follow
    For more info

    The main language we use at work is VB and even in VB we have always strayed away from them. goto's !=structured programming. There have been WAY too many discussions here on why using goto is a bad programming practice.

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    31
    Yeah i know goto's are bad... i was too tired last night to make any more functions...
    And yes the program works for me. The only problem is finding the lowest roll.
    (To roll stats in 3rd ed D&D you roll four 6 sided dice and drop the lowest.)
    r# = a roll
    I use r1 <= r2 && r1 <= r3 && r1 <= r4
    Now this COULD work, but if r1 and r2 are both 6's or 5's etc. then it will drop a six, and a six will probly not be the lowest number. But if i remove the equality part, then the statement could evaluate to false, even though the first roll is 2, becuase the second roll is 2 also. Then all the other statements are false too.

    See my problem?

  7. #7
    Registered User InFeStEd-ArCh0n's Avatar
    Join Date
    Apr 2002
    Posts
    63

    Talking I do...

    Originally posted by Monster

    Then go make your program in a lower level language. If you want to use goto's in a higher level language then use VB or something like that.
    I do program in a lower level language, I use assembly for windows programming. Thanks for the offer, though.
    -Mike
    {InFeStEd-ArCh0n}

  8. #8
    Nor
    Nor is offline
    h ֆhr s Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    lol haven't seen anything in d&d for awile.
    But don't feel bad. I've made a quick roller for Rifts, d&d and about 4 other games.
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >goto's == bad
    No, programmers_who_don't_know_how_to_use_goto + goto == bad.

    >If statements in lower level languages are just jump
    >statements, then why are goto's so bad
    In C and C++ goto is considered bad style when used in a situation that it isn't helpful. Since both languages have ways to get around goto in a more elegant manner, it's use is rarely needed.

    >So what is the big deal about gotos?
    Just another language feature for the uninformed to bicker over.

    >goto's can make your code hard to follow
    Only when used without thought. They can also make programs harder to debug. K&R describes goto as "infinitely abusable", which does *not* mean bad. It just means that it's easier to write crap code with goto than with more structured methods if you don't know what you're doing.

    >Feel free to make fun of me.
    The only real problem I have with this code (assuming it works) is that you call main. This is a practice that should be avoided because it will cause problems. If you want to return to main then use the return keyword and loop back to the beginning instead of calling the main function.

    -Prelude
    My best code is written with the delete key.

  10. #10
    Registered User
    Join Date
    May 2002
    Posts
    31
    Prelude, thanks for the the info on main. I know how to use return, it's just i never put that together in my head .

    And it works, trust me. Like i siad earlier the only problem is the lowest roll code, but the numbers still come out between 3 and 18 like they should. Anyone have a function for finding lowest number? Maybe i can find something in math.h...

  11. #11
    Unregistered
    Guest
    Y'know what the biggest problem with gotos are?? People are always arguing about them and asking questions!!! Everyone knows they are *generally* a bad idea, even if they don't know why - just avoid using them, there are better ways around a problem. Usually anyway

  12. #12
    Registered User Liam Battle's Avatar
    Join Date
    Jan 2002
    Posts
    114
    OMG YOU USED GOTO's
    LB0: * Life once school is done
    LB1: N <- WakeUp;
    LB2: N <- C++_Code;
    LB3: N >= Tired : N <- Sleep;
    LB4: JMP*-3;

  13. #13
    Registered User
    Join Date
    May 2002
    Posts
    31
    OK, can we PLEASE get off the subject of goto's? It's more annoying than... building legos without instructions.

  14. #14
    Registered User InFeStEd-ArCh0n's Avatar
    Join Date
    Apr 2002
    Posts
    63

    Angry

    Have you ever tried the Kinex building instructions? They're bad news!
    -Mike
    {InFeStEd-ArCh0n}

  15. #15
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    except for the missing return 0 i dont see anything wrong in your program (havent yet compiled it)

    also i was wondering is it good/better to give var names in prototype or just their data type.
    i dont give var names in func prototypes is it wrong?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 09:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 08:34 AM
  3. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 11:20 AM
  4. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21