Re: [Dev-C++] Problems with a for loop again
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Jason H. <jas...@bt...> - 2002-03-11 00:07:05
|
I'm glad I could help a little, and I have a small piece of advice for =
you. That is becareful in using the do..while loop; you have to remember =
that it will always go through the loop at least once, even if there are =
no lines to be displayed, which may be an error. Atual usage of =
do..while loops is a lot rarer than that of while and for loops.
Also the index value filesize isn't valid the first time around. Vectors =
start at zero and go to size() - 1, just like arrays. Which also means =
that you will miss off the first line in the vector.
Jason.
----- Original Message -----=20
From: Paul Stone=20
To: Dev C++ Mailing list=20
Sent: Sunday, March 10, 2002 7:23 PM
Subject: [Dev-C++] Problems with a for loop again
Here is the full code, pasted the wrong code in last message.
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
=20
int main(int argc, char *argv[])
{
int filesize=3D0;
vector<string> v;
ifstream in("Backvector.cpp");
string line;
while(getline(in, line))=20
v.push_back(line); // Add the line to the end=20
filesize=3Dv.size(); // Get number of lines in file
//
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!
//
do {
cout << filesize << ": " << v[filesize] << endl;
filesize=3Dfilesize-1;
} while(filesize >0);
system("PAUSE");
return 0;
} ///:~
=20
|