Hi I am currently writing a program that would take a number, then compare that number to numbers in a file that I have opened. I am having a few problems.

This is what one of the functions I am having a problem with looks like:

the function receives a pointer to a file containing the numbers along with area(the first number in the file e.g 0 200 699, the first 0 is considered area) and checks if the area is in the file

int registeredA(FILE* fp, int area);

This function returns true if the area identifier is listed in the prefix table; false otherwise. Note that the area parameter is of int data type.

Thanks any help would be appreciated

OK so I got through most of my problems but now I'm getting these errors problem when I try to use ifstream. Any ideas as to what may be causing this problem? I looked up the usage of ifstream but it wasnt very helpful.
Thanks

ERRORS

ISBNPrefix.cpp: In function 'int registeredA(FILE*, int)':
ISBNPrefix.cpp:34: error: variable 'std::ifstream inFILE' has initializer but incomplete type
ISBNPrefix.cpp: In function 'int minNoDigits(FILE*, int)':
ISBNPrefix.cpp:48: error: variable 'std::ifstream inFILE' has initializer but incomplete type
ISBNPrefix.cpp:50: error: 'in' was not declared in this scope
ISBNPrefix.cpp: In function 'int registeredAP(FILE*, int, const char*)':
ISBNPrefix.cpp:70: error: variable 'std::ifstream inFile' has initializer but incomplete type
ISBNPrefix.cpp:73: error: 'in' was not declared in this scope
ISBNPrefix.cpp:75: error: 'strlen' was not declared in this scope
ISBNPrefix.cpp:75: error: 'str' was not declared in this scope
ISBNPrefix.cpp: In function 'int close(FILE*)':
ISBNPrefix.cpp:82: error: 'inFile' was not declared in this scope
ISBNPrefix.cpp:87: error: expected ';' before '}' token

My Code

#include <iostream>
#include "ISBNPrefix.h"
using namspace std;

FILE* open(const char filename[]){
        FILE* fp;
        fp = fopen (filename, "r");
        return fp;
}
int registeredA(FILE* fp, int area){
        int areaVAL=0;
		char hi;
		char lo;
		rewind(fp);
       	ifstream inFILE(fp);
        
		while (!inFILE.eof()) {
                
				cin >> areaVAL >> lo >> hi;
                if (areaVAL == area) {
					return true;
					}
        }
        return false;
}
int minNoDigits(FILE* fp, int area){
        int areaVAL, loPub, hiPub;
		rewind(fp);
        ifstream inFILE(fp);
        while (!inFILE.eof()){
                cin >> areaVAL >> loPub >> hiPub; 
                
                if (area == areaVAL) {
                        if (hiPub <= 9) return 1;
                        if (hiPub <= 99) return 2;
                        if (hiPub <= 999) return 3;
                        if (hiPub <= 9999) return 4;
                        if (hiPub <= 99999) return 5;
                        if (hiPub <= 999999) return 6;
                        return 7;
                }       
        }
       
        return 0;
}
int registeredAP(FILE* fp, int area, const char publisher[]){
        int areaVAL=0;
		char hiPub;
		char loPub;
		rewind(fp);
        ifstream inFile(fp);
        while (!inFile.eof()) {
           
                in >> areaVAL >> loPub >> hiPub;
                if (areaVAL == area) {
                        if (strlen(loPub) <= strlen(publisher) && strlen(publisher) <= str(hiPub))
						return true;
                }
        }
        return false;
}
int close (FILE* fp){
 		while (!inFile.eof())  {
			fclose(fp);
        return true;
		} 
return false
}

Edited 6 Years Ago by falconxxxx: n/a

YOU HAVE CHANGED THE CODE, NOW THE LINE NUMBERS OF THE ERROR MESSAGES DO NOT MATCH THE CODE LINE NUMBERS :(

Error message:

ISBNPrefix.cpp:87: error: expected ';' before '}' token

Look at the following code

int close (FILE* fp){
               while (!inFile.eof())  {
                        fclose(fp);
        return true;
         } 
return false
}

There is no ';' after return false

Edited 6 Years Ago by anishakaul: n/a

Error message:

ISBNPrefix.cpp:73: error: 'in' was not declared in this scope

Type of in in the following code is missing ? Line: 54

in >> areaVAL >> loPub >> hiPub;

Edited 6 Years Ago by anishakaul: n/a

Error message:

ISBNPrefix.cpp:75: error: 'strlen' was not declared in this scope

Include "string.h" header file.

If you don't know basic programming, learn it somewhere else !!

Your error messages are too simple :(

Ugh.. didn't even notice that. Sorry for my sloppiness.
This is a much better version.

ERRORS

ISBNPrefix.cpp: In function 'int registeredA(FILE*, int)':
ISBNPrefix.cpp:16: error: variable 'std::ifstream inFILE' has initializer but incomplete type
ISBNPrefix.cpp:20: error: 'inFile' was not declared in this scope
ISBNPrefix.cpp: In function 'int minNoDigits(FILE*, int)':
ISBNPrefix.cpp:30: error: variable 'std::ifstream inFILE' has initializer but incomplete type
ISBNPrefix.cpp:32: error: 'inFile' was not declared in this scope
ISBNPrefix.cpp: In function 'int registeredAP(FILE*, int, const char*)':
ISBNPrefix.cpp:52: error: variable 'std::ifstream inFile' has initializer but incomplete type
ISBNPrefix.cpp:57: error: 'strlen' was not declared in this scope
ISBNPrefix.cpp:57: error: 'str' was not declared in this scope
ISBNPrefix.cpp: In function 'int close(FILE*)':
ISBNPrefix.cpp:64: error: 'inFile' was not declared in this scope


CODE:

#include <iostream>
#include "ISBNPrefix.h"
#include <string>
using namespace std;

FILE* open(const char filename[]){
        FILE* fp;
        fp = fopen (filename, "r");
        return fp;
}
int registeredA(FILE* fp, int area){
        int areaVAL=0;
		char hi;
		char lo;
		rewind(fp);
       	ifstream inFILE(fp);
        
		while (!inFILE.eof()) {
                
				inFile >> areaVAL >> lo >> hi;
                if (areaVAL == area) {
					return true;
					}
        }
        return false;
}
int minNoDigits(FILE* fp, int area){
        int areaVAL, loPub, hiPub;
		rewind(fp);
        ifstream inFILE(fp);
        while (!inFILE.eof()){
                inFile >> areaVAL >> loPub >> hiPub; 
                
                if (area == areaVAL) {
                        if (hiPub <= 9) return 1;
                        if (hiPub <= 99) return 2;
                        if (hiPub <= 999) return 3;
                        if (hiPub <= 9999) return 4;
                        if (hiPub <= 99999) return 5;
                        if (hiPub <= 999999) return 6;
                        return 7;
                }       
        }
       
        return 0;
}
int registeredAP(FILE* fp, int area, const char publisher[]){
        int areaVAL=0;
		char hiPub;
		char loPub;
		rewind(fp);
        ifstream inFile(fp);
        while (!inFile.eof()) {
           
                inFile >> areaVAL >> loPub >> hiPub;
                if (areaVAL == area) {
                        if (strlen(loPub) <= strlen(publisher) && strlen(publisher) <= str(hiPub))
						return true;
                }
        }
        return false;
}
int close (FILE* fp){
 		while (!inFile.eof())  {
			fclose(fp);
        return true;
		} 
return false;
}

If you don't know basic programming, learn it somewhere else !!

Your error messages are too simple :(

I'm learning.. I've been up late every night trying to make this program. I know I'm not good at all but I'll get there.

I'm learning.. I've been up late every night trying to make this program. I know I'm not good at all but I'll get there.

U cannot be a good programmer by compiling some one else's program! because u don't know the basics ! Kindly try out some small examples of ifstream then u will yourself know how simple these errors are !

#include <string>

Its string.h not string

It's actually #include <cstring> in standard C++.

For your infile stream you don't need to open up the file using open and then pass it into your inFile constructor. On line 16 you can just say ifstream inFile( ) with a null terminated string in the parentheses with the filename (so if it's a standard string it needs to be mystring.c_str() or a literal "myfilenamehere" or you can use the approach you used with open() in that function with the const). I would make an ifstream local to each function as above and not bother with the passing of the FILE pointer, or that function to open the file at all.

We've all got to learn sometime, so feel welcome to submit your questions (though it's a great learning experience to go through and figure out exactly what it is the compiler is telling you too).

Edited 6 Years Ago by jonsca: n/a

If you don't know basic programming, learn it somewhere else !!

Well I must apologize for my rude words !

This article has been dead for over six months. Start a new discussion instead.