1

Cevelop objects to char junk as an "un-initialized variable". What is the correct way to address the issue in this case?

enter image description here

 friend std::ostream& operator<<(std::ostream& os_a, College& college_a) {
   return os_a <<  college_a.id_ + ' ' + college_a.name_;
 }

 friend std::istream& operator>>(std::istream& is_a, College& college_a) {
   char junk;
   return is_a >> college_a.id_ >> std::noskipws
       >> junk, std::getline(is_a, college_a.name_);  // name: between 1st space and endofline.
  }
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
CW Holeman II
  • 4,661
  • 7
  • 41
  • 72
  • 3
    Looks like a false positive in the diagnostic engine. – Baum mit Augen Apr 10 '18 at 20:03
  • As Baum mit Augen mentioned, it might be a mistake to complain about an uninitialised variable which does not need it for being written before used. Your objective is probably to get rid of the warning in an efficient way. For that just initialising the variable seems the most obvious solution, even without community confirmation that it is a false positive. Please explain why this is not an option for you. – Yunnosch Apr 10 '18 at 20:08
  • Is there meant to be a *comma* after `>> junk`? Shouldn't that be a *semicolon*? – Galik Apr 10 '18 at 20:09
  • @Galik Unlikely, the code after the semicolon would not be executed. – Yunnosch Apr 10 '18 at 20:11
  • @Galik Then the `getline` would never be called. The comma is correct. (Although the code is needlessly clever, of course.) – Baum mit Augen Apr 10 '18 at 20:11

1 Answers1

3

You have two choices, you can initialize junk to something or you can just get rid of it. Since you know you need to eat just a space you and use get like

return is_a >> college_a.id_, is_a.get(), std::getline(is_a,college_a.name_);

and it will do the same thing. You can also make the code a little easier to read with

is_a >> college_a.id_;
is_a.get();
std::getline(is_a,college_a.name_);
return is_a;
NathanOliver
  • 171,901
  • 28
  • 288
  • 402