variables not initialized?

closed account (zT4NhbRD)
I'm being told my code won't compile because my variables aren't initialized but when I run it (using xCode) it runs just fine? I looked it over and everything looks initialized to me... Maybe I'm missing something really obvious though?

Last edited on
columnReserved on line 22 isn't initialized.
closed account (zT4NhbRD)
Ah gotcha, I never really used char. Do I just set that to = 0 to initialize it like other variables?

Also is that the only variable I missed initializing?
Last edited on
Ah gotcha, I never really used char. Do I just set that to = 0 to initialize it like other variables?


You should initialize all variables to whatever you want them to be.

1
2
3
4
char columnReserved;

print( columnReserved );  // <- what does this mean?  What are you trying to print?
  //  You never made 'columnReserved' contain anything so you'll just get garbage 


If the answer is "I want that print statement to print a null character for the column", then yes, you should initialize it with 0. But if that's not what you want, then you should initialize it with what you do want.
closed account (zT4NhbRD)
Got it, thanks very much!
Ah gotcha, I never really used char. Do I just set that to = 0 to initialize it like other variables?

Yes, but you declared it as a char so if you want it to be a number then you should make it an int.
closed account (zT4NhbRD)
I need to it to be a char there isn't a specific thing I need it to be, char becomes whatever the user inputs... so how would I initialize it?

Sorry if that doesn't make any sense or is a dumb question. Still learning c++!
char x = ' ';

P.s. You would have found the answer to this question much faster if you googled it. Always google things before posting.
closed account (zT4NhbRD)
Thanks :)
Topic archived. No new replies allowed.