Re: [Dev-C++] Problems with a for loop
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Jason H. <jas...@bt...> - 2002-03-10 14:13:08
|
Using C style for loops isn't as easy as the more traditional =
FOR..TO..DO.. or FOR..TO..STEP..DO.. style of loops. The basic C for =
loop is constructed:
for(initialiser; condition; change) { ...do... }
I won't go into detail, but the main thing you have to remember is that =
the condition should remain true until the ending condition is met. If =
the condition isn't true to start with then the loop won't go. In you =
loop you have:
// Add line numbers:
for(int i =3D filesize; i < 0; i - 1)
cout << i << ": " << v[i] << endl;
Assuming filesize is some positive integer then, you are in effect =
trying to see if filesize is smaller-than zero, which it won't be, and =
so the condition evaluates to false and no loop takes place. Another =
error in you code is the change section, where you are trying to count =
down on i with: i - 1; however in C/C++ this is not a complete statement =
(not technically true, but you know what I mean). What you probably =
meant is i-- (or --i, doesn't matter in this case). The post- and pre- =
increment and decrement operators are fairly unique in C/C++ in that =
they modifiy the value that you are working with as well as returning a =
result. Normally in most statements when you perform a calculation you =
need to assign the result of the calculation to something, another =
variable. Your statment calculates i - 1, but then discards the value. =
What you may have wanted was i =3D i - 1; where you reassign the value =
back into itself with the change. Equivalent is i -=3D 1; or as noted =
i--;
So what you actualy want is:
for(int i =3D filesize; i > 0; i--)
As for the validity of doing it in that way I'll leave it up to you, but =
here are some more observations I have made.
It is important to ensure that you are using the right comparison and =
start values (hint!). In this version we counted down from filesize to =
1. In an array you have to remember that the first element starts at 0 =
and goes up to its length - 1, in fact the vector class has a member =
function 'size()' which will return the value you are calculating in =
filesize.
A common error in coding like this is to overrun or underrun a loop by =
1, and it is hard to notice when you have as errors caused by this =
aren't normally catastrophic, but subtle and hard to trace.
Also I notice that the for loop you are using would print the contents =
of the array in reverse. It is more common to want to walk forward =
through an array, but it depends on the situation, for forward you could =
use:
for(int i =3D 0; i < v.size(); i++)....
The code you supplied appears to have a few syntax errors in it, so I =
assume the code you have been using isn't exactly the same. The while =
block is missing its closing bracket '}', and the 'filesize' variable is =
never declared i.e. int filesize =3D 0; There may be others.
Hope this helps, Jason.
----- Original Message -----=20
From: Paul Stone=20
To: dev...@li...=20
Sent: Sunday, March 10, 2002 1:25 AM
Subject: [Dev-C++] Problems with a for loop
Greetings,
Iam trying to complete the tutoils from tinking C++, chapter 2 and =
seam to be having a problem with a for loop couting down.=20
The program is meant to read a file and display it starting with the =
last line of the file, it reads the file ok and copies it to an array =
but when it gets to the for loop it just skips over it,=20
I have tried changing the values in the for statement but the loop is =
still never enterd and it works if the loop counts up from zero to the =
size of the fileCan anyone help?=20
Code listed below:
//Backvector.cpp
//Copy the lines of a file into an array and
// diplay then starting with the last line.
#include <cstdlib>
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main(int argc, char *argv[])
{
vector<string> v;
ifstream in("Backvector.cpp");
string line;
while(getline(in, line)) {
v.push_back(line); // Add the line to the end
filesize++; // Get number of lines in file
// Add line numbers:
for(int i =3D filesize; i < 0; i - 1)
cout << i << ": " << v[i] << endl;
system("PAUSE");
return 0;
} ///:~
Thanks for any help
Paul Stone
|