Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: OT: C++ code question.

Author: Daniel Clausen

Date: 03:30:51 11/06/03

Go up one level in this thread


On November 06, 2003 at 06:02:37, macaroni wrote:

>Hi, thanks for helping me, but I still can't get it to work :(
>
>#include <iostream.h>
>#include <fstream.h>
>using namespace std;
>int main()
>{
> int BookSize=0,x;
> std::string BookLines[50000];
> ifstream in("SEEBook");
> if (!in)
> {
>  cout << "Book not found!\n";
>  return 1;
> }
> while(in.getline(BookLines[BookSize],120))
> {
>  BookSize++;
> }
> cin >> x;
> return 0;
>}
>
>this gives me a compiler error....i'm a really bad c++ programmer. it doesn't
>like the 'std::string BookLines[50000];' bit, don't know why, don't know how
>that line of code should work or what it does, but anyway,
>thanks again

Some remarks:
(1)
In order to use "string" you have to include the corresponding header file, in
this case "#include <string>"

(2)
When including C++ system header files (like iostream etc) I would skip the
".h". The versions with .h are the older versions. Maybe someone can shed some
more light on this issue and/or explain that better.

(2)
After using "using namespace std;" you don't have to specify the namespace in
front of string anymore, you can just use "string BookLines....".

(3)
I'm terrible when it comes to the input/output classes in C++. With ifstream I
use

   while(getline(in, BookLines[BookSize]))

instead of

   while(in.getline(BookLines[BookSize],120))

Note: You can skip the 120 since the string-object will resize itself
automatically so you don't have to worry about too long lines. Of course, if you
WANT to read 120 chars max, that another issue.

(4)
I would use the list or the vector class to keep the strings as opposed to a
fix-sized C-array, but depending on how much you know the STL of C++ you're
maybe still better with the simple array.

Hope this helps

Sargon



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.