For loop- Processing a line.

How do you process a line using a for loop?
Last edited on
What kind of line you want to see?

I'm writing a program that reads all the lines of an input file. I'm required to use a while loop to read each line of the file, a for loop to process each line, and if statements involving boolean operators in order for me to analyze each character. So far, i have a while loop to read all the lines in a file that looks like this:
cout << line << endl;
I don't think i am supposed to hard code the "cout << line << endl;" since i have to process the line using a for loop. Which I am stuck on how to use a for loop.
Also, i need to print out the words "Line 1", "Line 2", "Line 3", etc. I tried using the increment but its reading the 1st line in the file as different lines.
Line 1: (First line in the file)
Line 2: (Second line in the file)
Line 3: (Third line in the file)
Line 4: (Fourth line in the file)
However, the first line in the file has 4 lines but i just want to consider it as Line 1.
Last edited on
"process" can mean anything. What do you actually need to do with a line?
Look at each character of the line to "count" something?

a line has 4 lines

What are the rules that determine when a logical line continues to multiple physical lines?

For example, the statement cout << line << endl;
can be reformatted to
cout << line
     << endl;

because the C/C++ parser ignores (some) whitespace. It stills sees one statement.

Similarly, a=7;b=42; is parsed into two statements despite them being on the same physical line.

i need to print out the words

Then do so:
size_t count=0;
while( std::getline( infile, line ) )
  std::cout << "Line " << ++count << ": ";
  cout << line << endl;
I have figured it out, Thank you for your help! (:
Registered users can post here. Sign in or register to post.