Variable used without being initialized...help me plz

Feb 19, 2021 at 6:26pm
Okay...so this time We are having issues with our pricing on server..
The exe keeps hitting an error that ive tried and failed to catch..

can someone maybe take a look at the code an maybe fix whats wrong?
Our game server is crashing everytime someone uses the find menu to buy stuff...

CODE: https://pastebin.com/bg8VNbA3
Error Example: https://i.imgur.com/w8MuoLS.jpg
Feb 19, 2021 at 6:50pm
here is one issue: you define it but give it no value, and its only chance to get a value is INSIDE a condition. Presumably, the condition COULD be false sometimes, and the variable NEVER get a value. If you then USE the variable, you have a problem!
solution:
int kaina{defaultvalue}; //defaultvalue can be like {0} or {} or {42} or {variable} etc.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

int kaina;  //FIX ME
                                    ifstream infile("pricedata.txt");
                                    for (string line; getline(infile, line);)
                                    {
                                        if (line.length() > 3 && line[0] != '/' && line[1] != '/')
                                        {
                                            auto ex = explode("|", line);
                                            if (ex[0] == to_string(id))
                                            {
                                                kaina = atoi(ex[1].c_str());
                                                break;
                                            }
                                        }
                                    }
                                    /*if (kaina == 0) continue;*/ //second kaina fix? 
Last edited on Feb 19, 2021 at 6:54pm
Feb 19, 2021 at 10:53pm
aaahh i missed one. lol okay thank you. ill give it a try.

there few other err too, but i have to debug them before i can post.
really love that this site is around.
Feb 19, 2021 at 11:03pm
wait... kaina = atoi(ex[1].c_str()); should be getting the price data from the file shouldnt it?

ill upload below file to show format.

example>> http://138.201.184.108/pricedata.txt
Last edited on Feb 19, 2021 at 11:03pm
Feb 20, 2021 at 3:08am
I don't know what the code does or anything about 'should'.
all I know is that if the loop does not execute or if the loop executes and the if statement is false, you have a condition where it 'could' fail to initialize the variable. If you used it after that, it will complain. If the file is empty, it will not assign a value.

there may be other such constructs in your code. I didn't look at every possible logical failure but instead I am showing you an example of what not to do. In any of your blocks, if you have an uninitialized value and it is only assigned conditionally, it can fail there. I will leave you to look for them all. As a side note, its a lot harder to debug stuff when you reuse the same variable name over and over in different scopes.
Last edited on Feb 20, 2021 at 3:11am
Feb 20, 2021 at 8:03am
Why don't you use auto in your code?
With auto you can never forget to initialize a variable.
Topic archived. No new replies allowed.