C++ Coding Tips

Discussion in 'Programmer's Corner' started by TheFox, Apr 15, 2010.

  1. TheFox

    Thread Starter Active Member

    Apr 29, 2009
    66
    5
    I am actively trying to learn C++. I was wondering, f I posted come code, would you guys help, by giving some tips to make my code run better, or be more readable?

    For example:
    Code ( (Unknown Language)):
    1.  
    2. #define block(x) x*.03
    3. #define attack(x,y) y-x
    4. #include <iostream>
    5. #include <string>
    6. typedef long unsigned int lui;
    7. using namespace std;
    8. unsigned int stat_dex,stat_int,stat_sta,stat_str;
    9. unsigned short int weapon_min,weapon_max;
    10. unsigned int level;
    11. string player_class;
    12.  
    13. struct user
    14.     {
    15.         string user_class;
    16.         lui user_dex;
    17.         lui user_str;
    18.         lui user_sta;
    19.         lui user_int;
    20.         lui user_level;
    21.         lui user_weapon_min;
    22.         lui user_weapon_max;
    23.         lui user_armor_min;
    24.         lui user_armor_max;
    25.     };
    26. struct skills
    27.     {
    28.         lui Asal;
    29.         lui Spirit_Bomb;
    30.     };
    31.  
    32. int ranger(
    33.            lui ranger_level,
    34.            lui ranger_stat_str,
    35.            lui ranger_stat_dex,
    36.            lui ranger_stat_sta,
    37.            lui ranger_stat_int,
    38.            lui ranger_min_dmg,
    39.            lui ranger_max_dmg,
    40.            lui ranger_armor_min,
    41.            lui ranger_skill_asal,
    42.            lui ranger_skill_sb
    43.            )
    44. {
    45.     double ranger_dodge_rate;
    46.     unsigned char ranger_hit_rate=1;
    47.     unsigned char ranger_block_rate=8;
    48.     lui ranger_damage;
    49.     lui ranger_health;
    50.     lui ranger_mp;
    51.     lui ranger_fp;
    52.    
    53.     //Block rate
    54.     ranger_dodge_rate=ranger_stat_dex;
    55.     ranger_dodge_rate=rand()%ranger_stat_dex*.05;
    56.     //attack
    57.     ranger_damage=ranger_stat_dex*ranger_min_dmg/8;
    58.     //hp
    59.     ranger_health=ranger_stat_sta*ranger_level;
    60.     //mp
    61.     ranger_mp=ranger_level*ranger_stat_int/2;
    62.     //fp
    63.     ranger_fp=ranger_level*ranger_stat_sta*1.5;
    64.    
    65.     double attack_bonus=ranger_level*1.15;
    66.  
    67.     cout<<"Attack: "<<ranger_damage+attack_bonus<<"\n";
    68.     cout<<"Defence: "<<ranger_stat_sta*2+ranger_armor_min<<"\n";
    69.     cout<<"HP: "<<ranger_stat_sta*ranger_level<<"\n";
    70.     cout<<"MP: "<<ranger_mp<<"\n";
    71.     cout<<"FP: "<<ranger_fp<<"\n";
    72.     cout<<"Chance of being hit: "<<block(ranger_stat_dex)/1<<"%\n";
    73.     cout<<"Chance of blocking: "<<(block(ranger_stat_dex)/8*10)+(block(ranger_stat_dex)/1)<<"%\n";
    74.     //cout<<ranger_dodge_rate<<" Dodge: ";
    75.  
    76.     //Skill
    77.     cout<<"---------------------Skills-----------------------------------\n";
    78.     cout<<"Asal: "<<ranger_mp/10*ranger_skill_asal<<"\n";
    79.     cout<<"Spirit bomb: "<<(ranger_mp/100+(ranger_damage/2)*ranger_skill_sb)*.2<<"\n";
    80.     return 0;
    81. }
    82.  
    83. int main ()
    84. {        
    85.     user jim;
    86.     skills jims;
    87.  
    88.     cout<<"Level?";
    89.     cin>>jim.user_level;
    90.  
    91.     cout<<"Dex?: ";
    92.     cin>>jim.user_dex;
    93.  
    94.     cout<<"Sta: ";
    95.     cin>>jim.user_sta;
    96.  
    97.     cout<<"Str: ";
    98.     cin>>jim.user_str;
    99.  
    100.     cout<<"int: ";
    101.     cin>>jim.user_int;
    102.  
    103.     cout<<"Min atk of your weapon":;
    104.     cin>>jim.user_weapon_min;
    105.  
    106.     cout<<"Max atk of your weapon: ";
    107.     cin>>jim.user_weapon_max;
    108.  
    109.     cout<<"minimum def of armor: ";
    110.     cin>>jim.user_armor_min;
    111.  
    112.     cout<<"maximum def of armor: ";
    113.     cin>>jim.user_armor_max;
    114.  
    115.     cout<<"asal level: ";
    116.     cin>>jims.Asal;
    117.  
    118.     cout<<"Spirit bomb Level: ";
    119.     cin>>jims.Spirit_Bomb;
    120.  
    121.     for(char i=0; i!=2; i++)
    122.     cout<<"--------------------------------------------------------------\n";
    123.     ranger(jim.user_level,jim.user_str,jim.user_dex,jim.user_sta,jim.user_int,jim.user_weapon_min,jim.user_weapon_max,jim.user_armor_min,jims.Asal,jims.Spirit_Bomb);
    124.     for(char i=0; i!=2; i++)
    125.     cout<<"--------------------------------------------------------------\n";
    126.     while(1)
    127.     {
    128.         int name=0;
    129.         name++;
    130.     }
    131. }
    132.  
     
  2. guitarguy12387

    Active Member

    Apr 10, 2008
    359
    12
    You should be more specific. What is the code doing? Is it just running on a PC or is it for DSP/MCU?

    For example, from a real-time perspective, printing statements (cout, etc) are expensive. Mod operator is expensive.

    Thats largely a matter of personal preference. Personally, i prefer the main() at the top with function declarations too. Just keeps it more organized IMHO. But again, personal preference. You could move comments to the same line if you want. Just so your code is less lines to look through..... again, pretty trivial/preferential.

    What exactly is this doing haha?
     
    TheFox likes this.
  3. TheFox

    Thread Starter Active Member

    Apr 29, 2009
    66
    5
    My bad, I thought I replied to this, a few days ago, and just realized, that it didn't take, or I didn't hit send; either way, it got left unanswered.

    1)The code is creating a characters stats, for a game.
    2)It would be for windows.
    3)As for why that's there, I forgot to remove it.

    As for what I am asking for; if I were to pass It off to someone who knows c++, they could maintain it with little to no problem, or help.
     
  4. guitarguy12387

    Active Member

    Apr 10, 2008
    359
    12
    Ohh okay. yeah its fine i'd think.
     
  5. TheFox

    Thread Starter Active Member

    Apr 29, 2009
    66
    5
    Thank you very much for your input. I guess it means my code is not totally unreadable. Being I'm basically teaching my self, I just wanted to make sure I conferm to other's standards.


    Jim
     
  6. retched

    AAC Fanatic!

    Dec 5, 2009
    5,201
    312
    If you want to ensure that someone else will understand your code without having to disect it completely, use REMARK statements saying what each variable is and what each function does.

    That will take a lot of guess work out of this.

    You may want to look into some developer sites that have guidelines on contributions and code design. This is done so everyone follows the same layout. That way if 300 people work on the code, it wont look like it.
     
    TheFox likes this.
  7. guitarguy12387

    Active Member

    Apr 10, 2008
    359
    12
    Yeah, a lot of it is personal preference.

    But comments definitely help. Usually as long as you pay attention to having good code style, you will be fine. There's no standard... just be consistent.
     
    TheFox likes this.
  8. davebee

    Well-Known Member

    Oct 22, 2008
    539
    46
    TheFox likes this.
Loading...