General Improvements for my W.I.P Game?

Hello forum, this probably isn't my smartest idea but it's 4AM and i cannot make a proper judgement currently. I've spent the whole night creating a little WIP game for no real reason, but i do wish to complete it and I would like to know if, moving on, I can use any, uh, better methods. (full disclosure, I've only just started C++ programming with no prior programming experience). Anyways, enough with the babble, here it is.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <iostream>
#include <cstdlib>
using namespace std;

int main(){

    // Base Stats
    char pointSelection;
    int skillPoints;
    int charisma = 0;
    int strength = 0;
    int carry = 20 + (strength * 4); //unused as of right now
    int gold;
    int itemWorth[8] = {rand()% 25 + 150, rand()% 4 + 25,rand()% 18 +10 ,rand()% 100 + 90, rand()% 50 + 20, rand()% 5 + 5,rand()% 40 + 200, rand()% 2 + 1}; // unused as of right now
    int exp;
    int expRequire = 6;
    int sellBonus = rand() %charisma * 3; // unused as of right now
    int sellPrice;


    //location stats
    bool town;

    //Game starts

    cout<<"welcome to -insert test game name here-" << endl;
    cout<<"pick up stuff and then sell it!"<<endl;
    skillPoints = 5;
    while (skillPoints > 0){
        cout<<"please allocate your " << skillPoints << " skill points now" << endl;
        cout<<"s for strength, c for charisma"<<endl;
        cout<< "Strength is equal to "<< strength <<" and charisma is equal to "<< charisma<<endl;
        cin>>pointSelection;
        if (pointSelection == 'c'){
                charisma++;
                skillPoints--;
            pointSelection = 0;
        }
        else if (pointSelection == 's'){
                strength++;
                skillPoints--;
            pointSelection = 0;
        }
        else{
            cout<<"invalid selection, please put only 'c' or 's'..."<<endl;
        }
    }

    //EXP related stuff

if (exp >= expRequire){
    skillPoints++;
    exp = 0;
    expRequire = expRequire * 3 / 2;
}

if (sellPrice/(25 * (charisma + strength - 4) >= 1 )){
    exp = (exp + sellPrice/(25 * (charisma + strength - 4)));
}

    //Basic Commands
    bool play = 0; //temp core gameplay loop maintainer
    while(play = 0){
    char order;
    char h,t,b,p,s,i; //p, s, and i are unused as of right now
    cout<<"what would you like to do at this current moment?"<<endl;
    cin>>order;
    if(order = 'h'){
        cout<< "h for help, t for traveling, b for banking, p to pick up an item off the ground, s for your stats, i for inventory."<<endl;
    }
    else if(order = t){
        cout<<"are you sure you want to move forth? y for yes n for no."<<endl;
        cin>>order;
        if(order = 'y'){
            //code for generating a new area empty as of right now due to it being 4 am
        }
    }
    else if(order == 'b' && town == 1){
        int deposit;
        int score;
        cout<< "money in retirement is" << score << "want to deposit?"<<endl;
        cin>>order;
        if (order = 'y'){
           cout<<"how much shall you put away?" << endl;
            cin>>deposit;
                if(gold-deposit < 0){
                    cout << "invalid amount marked for transfer" <<endl;
                }
            else {
            score = score + deposit;
            gold = gold - deposit;
            }
        }
    }
    else{
        cout<<"invalid order, type h for help"<<endl;
    }
    }

return 0;
}


Pretty rough stuff, eh? Probably brings you back to simpler times or something. Yes, I know it's messy and I should probably fix it...but y'know, 4 am is 4 am!


<EDIT>
Should probably say it is error free at the current time! Also I am not pure beginner, i've done various small projects and this is my first attempts at a larger one!
Last edited on
Looking it back over once more i've noticed i could make it so I only have cin<<order to worry about instead of the other (frankly useless) ints correct?

Quick note! I know me using namespace is painting a huge target on my back(why is that?) so please remember, 4 am coding is not my gig. :)
Last edited on
You never call srand() to establish a random starting point for the RNG. Your program is going to return the same sequence of random numbers every time you run it. That's fine for debugging, but if you want an interesting game, you should really set a random starting point for the RNG. You should call srand() ONCE at the beginning of main(). i.e. before line 14.
http://www.cplusplus.com/reference/cstdlib/srand/

Lines 63, 68, 71,74: You're using the assignment operator (=). You want the equality operator (==).

I know me using namespace is painting a huge target on my back

http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-in-c-considered-bad-practice
Last edited on
Should probably say it is error free at the current time!

Although your program has no errors it should have several warnings, if your compiler settings are properly set. You should never ignore warnings, treat them like errors and fix them.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
||In function ‘int main()’:|
|63|warning: suggest parentheses around assignment used as truth value [-Wparentheses]|
|68|warning: suggest parentheses around assignment used as truth value [-Wparentheses]|
|71|warning: suggest parentheses around assignment used as truth value [-Wparentheses]|
|74|warning: suggest parentheses around assignment used as truth value [-Wparentheses]|
|83|warning: suggest parentheses around assignment used as truth value [-Wparentheses]|
|65|warning: unused variable ‘h’ [-Wunused-variable]|
|65|warning: unused variable ‘b’ [-Wunused-variable]|
|65|warning: unused variable ‘p’ [-Wunused-variable]|
|65|warning: unused variable ‘s’ [-Wunused-variable]|
|65|warning: unused variable ‘i’ [-Wunused-variable]|
|12|warning: unused variable ‘carry’ [-Wunused-variable]|
|14|warning: unused variable ‘itemWorth’ [-Wunused-variable]|
|17|warning: unused variable ‘sellBonus’ [-Wunused-variable]|
|51|warning: ‘exp’ may be used uninitialized in this function [-Wmaybe-uninitialized]|
|57|warning: ‘sellPrice’ may be used uninitialized in this function [-Wmaybe-uninitialized]|
|71|warning: ‘t’ may be used uninitialized in this function [-Wmaybe-uninitialized]|
|78|warning: ‘town’ may be used uninitialized in this function [-Wmaybe-uninitialized]|


Ah thanks guys! I've patched up my code and can safely say there are no errors and no more warnings(well, I have to keep the unused variable for now till I finish more of the code) and the srand function is back after I forgot to put it back in.

However I guess with the GNU compiler you can't really help with the warnings saying
|51|warning: ‘exp’ may be used uninitialized in this function [-Wmaybe-uninitialized]

Is there a way that I could remove it or is it just stuck there?
Last edited on
The warning is there for a reason.

Line 15: exp is an uninitialized variable. Hint: It's value is garbage.

Line 51: You try and test exp without ever having set it. What do you expect the outcome of this test to be?




Ah ok i see now, thanks!
Topic archived. No new replies allowed.