YES! for the code you wrote right there it doesn't matter much, but it's the same argument with as code tage, try reading this without indents:
Code:
main()
{
...
if (!infile) {cout << "ERROR" << endl << endl; return 1;}
for (i=0; (!infile.eof()) && (i <= 12); i++)
{
j=0;
ch = infile.get();
while ( (ch != ',') && (!infile.eof()) )
{
dummybuffer[j++] = ch;
//cout << ch << '\t' << j << '\t' << dummybuffer[j] << endl;
ch = infile.get();
}
dummybuffer[j] = '\0'; //string terminator
stock[i].SetName(dummybuffer); //sets the name of the item
j=0;
ch = infile.get();
while ( (ch != ',') && (!infile.eof()) )
{
dummybuffer[j++] = ch;
ch = infile.get();
}
dummybuffer[j] = '\0'; //string terminator
stock[i].ChangeQuan(atoi(dummybuffer)); //changes how many of the item
j=0;
ch = infile.get();
while ( (ch != ',') && (!infile.eof()) )
{
dummybuffer[j++] = ch;
ch = infile.get();
}
dummybuffer[j] = '\0'; //string terminator
stock[i].ChangeValue(atof(dummybuffer));
}
infile.close(); // end of outputting to structure
for (i=0; i<12; i++) // this for loop outputs everything
{
cout << stock[i].name << "," << stock[i].GetQuan() << "," << stock[i].GetSValue() << "," << stock[i].GetValue() << endl;
}
return 0;
}
not fun!
Now with:
Code:
main()
{
...
if (!infile) {cout << "ERROR" << endl << endl; return 1;}
for (i=0; (!infile.eof()) && (i <= 12); i++)
{
j=0;
ch = infile.get();
while ( (ch != ',') && (!infile.eof()) )
{
dummybuffer[j++] = ch;
//cout << ch << '\t' << j << '\t' << dummybuffer[j] << endl;
ch = infile.get();
}
dummybuffer[j] = '\0'; //string terminator
stock[i].SetName(dummybuffer); //sets the name of the item
j=0;
ch = infile.get();
while ( (ch != ',') && (!infile.eof()) )
{
dummybuffer[j++] = ch;
ch = infile.get();
}
dummybuffer[j] = '\0'; //string terminator
stock[i].ChangeQuan(atoi(dummybuffer)); //changes how many of the item
j=0;
ch = infile.get();
while ( (ch != ',') && (!infile.eof()) )
{
dummybuffer[j++] = ch;
ch = infile.get();
}
dummybuffer[j] = '\0'; //string terminator
stock[i].ChangeValue(atof(dummybuffer));
}
infile.close(); // end of outputting to structure
for (i=0; i<12; i++) // this for loop outputs everything
{
cout << stock[i].name << "," << stock[i].GetQuan() << "," << stock[i].GetSValue() << "," << stock[i].GetValue() << endl;
}
return 0;
}
Seeing where loops and if statements ebgin and end is a real pain without.