Sorry, I was imagining reading from a file. Frankly, as Cleophus noted, that cin>>salesamt; example is odd. I think a more “real world” example would use reading from a file. Something like the following, where file.dat is a file containing the data and comments from my last post.
(I’m wondering if you’ve gotten to reading from files yet? fin below is just like cin, except it’s reading from a file instead of the command line.)
float a, b, c;
char ch;
fstream fin("file.dat",ios::in);
while (!fin.eof()) {
ch = fin.getc();
if (ch == '#') {
fin.ignore(100,'
');
} else {
fin >> a >> b >> c;
//
// Additional processing...
//
...
}
}
...
- I’m not sure if getc() is the right function for reading a single character. I never do that.
In the code snip, each time through the loop handles one line of the data file. The while(!fin.eof()) is just checking whether we’ve read to the end of the file. Only the three data lines are read into a, b, and c and processed.