C++ Coding Tips

Thread Starter

TheFox

Joined Apr 29, 2009
66
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:
Rich (BB code):
#define block(x) x*.03
#define attack(x,y) y-x 
#include <iostream>
#include <string>
typedef long unsigned int lui;
using namespace std;
unsigned int stat_dex,stat_int,stat_sta,stat_str;
unsigned short int weapon_min,weapon_max;
unsigned int level;
string player_class;

struct user
    {
        string user_class;
        lui user_dex;
        lui user_str;
        lui user_sta;
        lui user_int;
        lui user_level;
        lui user_weapon_min;
        lui user_weapon_max;
        lui user_armor_min;
        lui user_armor_max;
    };
struct skills
    {
        lui Asal;
        lui Spirit_Bomb;
    };

int ranger(
           lui ranger_level,
           lui ranger_stat_str, 
           lui ranger_stat_dex, 
           lui ranger_stat_sta,
           lui ranger_stat_int, 
           lui ranger_min_dmg, 
           lui ranger_max_dmg, 
           lui ranger_armor_min,
           lui ranger_skill_asal,
           lui ranger_skill_sb
           )
{
    double ranger_dodge_rate;
    unsigned char ranger_hit_rate=1;
    unsigned char ranger_block_rate=8;
    lui ranger_damage;
    lui ranger_health;
    lui ranger_mp;
    lui ranger_fp;
    
    //Block rate
    ranger_dodge_rate=ranger_stat_dex;
    ranger_dodge_rate=rand()%ranger_stat_dex*.05;
    //attack
    ranger_damage=ranger_stat_dex*ranger_min_dmg/8;
    //hp
    ranger_health=ranger_stat_sta*ranger_level;
    //mp
    ranger_mp=ranger_level*ranger_stat_int/2;
    //fp
    ranger_fp=ranger_level*ranger_stat_sta*1.5;
    
    double attack_bonus=ranger_level*1.15;

    cout<<"Attack: "<<ranger_damage+attack_bonus<<"\n";
    cout<<"Defence: "<<ranger_stat_sta*2+ranger_armor_min<<"\n";
    cout<<"HP: "<<ranger_stat_sta*ranger_level<<"\n";
    cout<<"MP: "<<ranger_mp<<"\n";
    cout<<"FP: "<<ranger_fp<<"\n";
    cout<<"Chance of being hit: "<<block(ranger_stat_dex)/1<<"%\n";
    cout<<"Chance of blocking: "<<(block(ranger_stat_dex)/8*10)+(block(ranger_stat_dex)/1)<<"%\n";
    //cout<<ranger_dodge_rate<<" Dodge: ";

    //Skill
    cout<<"---------------------Skills-----------------------------------\n";
    cout<<"Asal: "<<ranger_mp/10*ranger_skill_asal<<"\n";
    cout<<"Spirit bomb: "<<(ranger_mp/100+(ranger_damage/2)*ranger_skill_sb)*.2<<"\n";
    return 0;
}

int main ()
{        
    user jim;
    skills jims;

    cout<<"Level?";
    cin>>jim.user_level;

    cout<<"Dex?: ";
    cin>>jim.user_dex;

    cout<<"Sta: ";
    cin>>jim.user_sta;

    cout<<"Str: ";
    cin>>jim.user_str;

    cout<<"int: ";
    cin>>jim.user_int;

    cout<<"Min atk of your weapon":;
    cin>>jim.user_weapon_min;

    cout<<"Max atk of your weapon: ";
    cin>>jim.user_weapon_max;

    cout<<"minimum def of armor: ";
    cin>>jim.user_armor_min;

    cout<<"maximum def of armor: ";
    cin>>jim.user_armor_max;

    cout<<"asal level: ";
    cin>>jims.Asal;

    cout<<"Spirit bomb Level: ";
    cin>>jims.Spirit_Bomb;

    for(char i=0; i!=2; i++)
    cout<<"--------------------------------------------------------------\n";
    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);
    for(char i=0; i!=2; i++)
    cout<<"--------------------------------------------------------------\n";
    while(1)
    {
        int name=0;
        name++;
    }
}
 
tips to make my code run better
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.

or be more readable?
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.

while(1)
{
int name=0;
name++;
}
What exactly is this doing haha?
 

Thread Starter

TheFox

Joined Apr 29, 2009
66
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.
 

Thread Starter

TheFox

Joined Apr 29, 2009
66
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
 

retched

Joined Dec 5, 2009
5,208
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.
 
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.
 
Top