Click to See Complete Forum and Search --> : having trouble with ifstream


shygirl
December 2nd, 2001, 05:38 PM
I need help with the error messages that I keep getting. I'm thinking that there's only one problem and they one problem caused the other problems. I think the one problem is with ifstream.

Script started on Sun Dec 02 18:27:11 2001
strauss.udel.edu% cat library.h

//header file for library class



#ifndef Library_H

#define Library_H

#include "book.h"

#include "card.h"

#include <fstream>



class Library {

private:

static int num_books;

static int num_cards;

Book *books[100];

Card *cards[100];

void printcards();

void printbooks();

void checkout();

void checkin();

public:

Library(ifstream &input);

~Library();

DoCommand(char);

Findbook(int);

Findcard(int);

static int getnum_books;

static int getnum_cards;

};



#endif

strauss.udel.edu% cat lir brary.cc

//member definition for library class



#include "book.h"

#include "card.h"

#include "library.h"

#include <string>

#include <iostream>



using std :: cout;

using std :: cout;



Library :: Library(ifstream &input) {

input.getline(title, 100, "\0");



for(int i = 0; i < 100; i++) {

books[i] = NULL;

while(strcmp(title, $$$) != 0) {

input.getline(author, 100, "\0");

input >> BookID >> status >> CardID;

input.ignore();

books[i] = new Book(title, author, BookID, status, CardID);

num_books++;

input.getline(title, 100, "\0");

}

}

input.getline(name, 100, "\0");



for(int j = 0; j < 100; j++) {

cards[j] = NULL;

while(!input.eof()) {

input.getline(phone, 100, "\0");

input >> CardID >> BookID;

cards[j] = new Card(name, phone, CardID, BookID);

num_cards++;

input.getline(name, 100, "\0");

}

}

Library :: Findbook(Book b) {

while(b[i] != NULL) {

if(BookID == b.BookID)

return BookID;

else

i++;

}

cout >> "Book not found";

}

Library :: Findcard(Card c) {

while(c[i] != eof()) {

if(CardID == c.CardID)

return CardID;

else

j++;

}

cout >> "Card not found";

}

Library :: DoCommand(char c) {

command = c;



if(command = 'B'){

while(books[i] != NULL) {

cout << "Processing book - ";

b[i]->print() ;

cout << endl;

i++;

}

cout << num_books << "books in the library" << endl;

}else if(command = 'C') {

while(cards[j] != NULL)) {

cout << "Processing card - ";

c[j]->print();

cout << endl;

j++;

}

cout << num_cards << "cards in the library" << endl;

}else if(command = 'O') {

cout << "Enter book ID: ";

cin >> BookID;

Findcard(CardID);

cout << "Enter card ID: ";

cin >> CardID;

Findbook(BookID);



if(getBookID() != 0) {

cout << "Sorry. You can't check out a book until"

<< "you return the book that you checked out"

<< "before." << endl;

}else {

if(getstatus() == 0) {

setstatus(1);

setBookID(BookID);

setCardID(CardID);

}else if(getstatus() == 1) {

cout << "Sorry, but the has already been"

<< "checked out."<< endl;

}else {

cout << "Book is at the bindery."<< endl;

}

}else if(command = 'W') {

cout << "Enter book ID: ";

cin >> BookID;

Findbook(BookID);

cout << "Enter card ID: ";

cin >> CardID;

Findcard(CardID);



if(getBookID() == 0) {

cout << "You do not have any books checked out" << endl;

}else {

if(getstatus() == 1){

setstatus(0);

setBookID(BookID);

}

}

}







strauss.udel.edu% make

CC -c library.cc

"library.h", line 20: Error: ifstream is not defined.

"library.cc", line 12: Error: ifstream is not defined.

"library.cc", line 13: Error: input is not defined.

"library.cc", line 13: Error: title is not defined.

"library.cc", line 13: Error: Badly formed expression.

"library.cc", line 17: Error: title is not defined.

"library.cc", line 17: Error: Expected an expression.

"library.cc", line 18: Error: input is not defined.

"library.cc", line 18: Error: author is not defined.

"library.cc", line 18: Error: Badly formed expression.

"library.cc", line 19: Error: input is not defined.

"library.cc", line 19: Error: BookID is not defined.

"library.cc", line 19: Error: status is not defined.

"library.cc", line 19: Error: CardID is not defined.

"library.cc", line 20: Error: input is not defined.

"library.cc", line 20: Error: Badly formed expression.

"library.cc", line 21: Error: title is not defined.

"library.cc", line 21: Error: author is not defined.

"library.cc", line 21: Error: BookID is not defined.

"library.cc", line 21: Error: status is not defined.

"library.cc", line 21: Error: CardID is not defined.

"library.cc", line 23: Error: input is not defined.

"library.cc", line 23: Error: title is not defined.

"library.cc", line 23: Error: Badly formed expression.

"library.cc", line 26: Error: input is not defined.

Compilation aborted, too many Error messages.

*** Error code 1

make: Fatal error: Command failed for target `library.o'

strauss.udel.edu% exit

strauss.udel.edu%
script done on Sun Dec 02 18:27:30 2001

gordonngai
December 2nd, 2001, 07:20 PM
Hi

you need add .h for header file

#include <fstream.h>
#include <string.h>
#include <iostream.h>

Gordonngai

Paul McKenzie
December 2nd, 2001, 08:03 PM
Is "cc" a C++ compiler or a 'C' compiler? If it is just a 'C' compiler, then your problem is that you are trying to feed C++ code into a 'C' compiler, which of course is not legal.

Test the simplest code with ifstream. Does this work?

#include <fstream>
#include <string>

using namespace std;

int main()
{
ifstream f;
}



If this works, then there is no problem with ifstream and you need to figure out why in your code you are not including the proper header. If this doesn't work, then maybe ifstream is defined in another header (it shouldn't be, but it just might be).

Try to get the simple program above to compile and link. Once you figure that out, then advance to do more code. I am guessing that you are a beginner -- if this is the case, the one advice I can give is to make sure you compile while you are writing the code. Don't write the whole program and then try to compile. It gets very difficult for anyone here to wade through 3 or 4 screenfuls of code with the problem being that the code doesn't compile.

Also, you should not use "i" as an index in your array when posting your code on CodeGuru. Half of your code is coming out as italics, just like this. As an alternative, leave a space between the "i" and the brackets, like this [ i ]. Also, use the [ ccode ] and [ /ccode ] blocks described above in the "Tip" above.

Regards,

Paul McKenzie

Paul McKenzie
December 2nd, 2001, 08:10 PM
This is not necessarily true. In C++, the standard headers *do not* have the .h. For example, there is a big difference between <string> and <string.h> -- the first describes the standard C++ string class, and the second has prototypes for the old 'C' functions like strcpy(), strcmp(), etc..

The code that the poster posted is correct (in terms of the headers having no ".h"). However, you might be right about the "cc" compiler not being ANSI compliant, and the compiler still could be using the non-standard ".h" headers.

Regards,

Paul McKenzie

NMTop40
December 3rd, 2001, 05:07 AM
foremost, for ifstream you need to include header <fstream> which you are not currently including.

NMTop40
December 3rd, 2001, 05:07 AM
unix is case-sensitive, and cc and CC are different - cc is a C compiler, while CC is a c++ compiler.

NMTop40
December 3rd, 2001, 05:12 AM
Not true and preferable not to use these headers, but to use the STL headers.

Be sure though, when using STL headers, to include the namespace.

This can be done in one of three ways:

using namespace std; // not my preference but common
// or
using namespace std::ifstream; // better
// or
std::ifstream input;
/* use namespace scope every-time. More typing but can be simpler because
it makes it clear where ifstream comes from. */

NMTop40
December 3rd, 2001, 05:47 AM
of course it should be

using std::ifstream;




without the word namespace.

James Curran
December 3rd, 2001, 06:50 AM
THe problem appears to be that you forgot the using std::ifstream;

after the include <fstream>

Truth,
James
http://www.NJTheater.com
http://www.NovelTheory.com
I don't do it for the points (OK, maybe I do), but rating a post is a good way for me to know if I helped.

Andreas Masur
December 3rd, 2001, 09:57 AM
What??? .cc is meant as an c++-file in Unix while .c is meant as a c-file...


Ciao, Andreas

"Software is like sex, it's better when it's free." - Linus Torvalds

NMTop40
December 3rd, 2001, 10:03 AM
no, the C++ compiler is called CC

Normally I define a C++ file with a .cpp or a .cxx extension. I don't know about .cc

rclemes
December 3rd, 2001, 10:04 AM
try using std::ifstream

Stitch
December 3rd, 2001, 10:17 AM
refering to the .h file extentions the ansi C libraries in C++ can be
included prefixing the name with a 'c' and dropping the ".h" then for
string.h you get#include <cstring>

Again you need to use
using namespace std;




Stitch
====================

Andreas Masur
December 3rd, 2001, 12:21 PM
Hmmm, I didn't know about a compiler called 'CC'. The only I know is 'gcc'...

I normally define a C++ file as .cpp under Windows but .cc is very common under Unix for a C++ file...

Ciao, Andreas

"Software is like sex, it's better when it's free." - Linus Torvalds