how do i access a variable initialized in a for loop or a function outside of it?
in the code below, i can only cout name inside of the for loop. how can i get it to
print name outside the loop?
1 2 3 4 5 6 7 8 9 10
for (int i = 0; i < arraySize; i++)
{ int firstComma, secondComma, thirdComma;
string row = array[i];
firstComma = row.find(",");
int weight = stoi(row.substr(0, firstComma-1));
secondComma = row.find(",", firstComma+1);
string color = row.substr(firstComma+2, secondComma -1);
string name = row.substr(secondComma+2);
cout << name << endl;
}
weight, color and name can only be accessed within the loop. Often in cases like these you'd use a struct of the required elements and have it populated by the extract function. This struct would be defined outside of the loop. Or have a function that takes a std::string arg and parses it into a returned struct.