I have another problem...

Let's start with said code:

#include <iostream>
#include <fstream> //I have tried using fstream.h like my teacher instructs me to do/
#include <cstdlib> //but it gives me another error: see below, in actual post.

using std::cout;
using std::cin;
using std::ios;
using std::endl;
//using namespace std;
using std::ifstream;

int main()
{
    char filename[16];
    int letters = 0;
    char letter_counter[2];
    int end_of_lines = 0;

    ifstream infile;

    cout << "Please enter the file you want to search and include its extension\n i.e. filename.txt\n>";
    cin.get(filename,16);
    cin.ignore(80,'\n');

    infile.open(filename, ios::in);

    if(!infile)
    {
        cout << "There were " << letters  << " letters, and " << end_of_lines << " end of line characters in " << filename << '.' << endl;
        return 0;
    }//if
    else
    {
        do
        {

            infile >> letter_counter;
            //if(letter_counter == 32)
            if(letter_counter == '\n') //|39|error: ISO C++ forbids comparison between pointer and integer|

            {
                end_of_lines++;
            }//if

            //if(letter_counter <= 122 && letter_counter >= 65)
            if(letter_counter <= 'z' && letter_counter >= 'A') //|45|error: ISO C++ forbids comparison between pointer and integer|
            {
                letters++;
            }//if
        }while(!infile.eof());//while
    }//else

    cout << "There were " << letters  << " letters, and " << end_of_lines << " end of line characters in " << filename << '.' << endl;
    infile.close();
    return 0;
}//main

When I compile this code, I'm getting the too commonly seen:

In function 'int main()':|
|39|error: ISO C++ forbids comparison between pointer and integer|
|45|error: ISO C++ forbids comparison between pointer and integer|
|45|error: ISO C++ forbids comparison between pointer and integer|
||=== Build finished: 3 errors, 0 warnings ===|

as you can see, I've tried to compare what's in both variables to see whether the current letter coming in from the file is an end-of-line character or a letter.
when trying to use <fstream.h>(like my teacher instructs us to use >_< ) and removing the using std::ifstream, i get the below errors:

|2|error: fstream.h: No such file or directory|
||In function 'int main()':|
|19|error: 'ifstream' was not declared in this scope|
|19|error: expected ';' before 'infile'|
|25|error: 'infile' was not declared in this scope|
|39|error: ISO C++ forbids comparison between pointer and integer|
|45|error: ISO C++ forbids comparison between pointer and integer|
|45|error: ISO C++ forbids comparison between pointer and integer|
||=== Build finished: 7 errors, 0 warnings ===|

I still get the ISO C++ error, but this time it's saying that I don't have <fstream.h>...

I've downloaded <fstream.h> and <_def.h> and reinstalled Code::Blocks twice now...

what don't usderstand is why I'm suddenly getting this error, when i've been able to use file I/O before...

If someeone could help, that would be great.

Derek

Recommended Answers

All 7 Replies

>if(letter_counter == '\n') //|39|error: ISO C++ forbids comparison between pointer and integer| letter_counter is an array of char, but '\n' is a single char. When arrays are used in value context they're converted to a pointer to the first element, so your comparison types are char* and char , which are incompatible.

It looks like you really wanted char letter_counter[2]; to be char letter_counter; , because your usage of letter_counter doesn't suggest an array in the slightest.

>|2|error: fstream.h: No such file or directory|
fstream.h is not valid ISO C++. Your teacher is probably using a pre-standard compiler, which means you have little choice but to also use that compiler. Otherwise your valid code will fail to compile on your teacher's compiler.

I've downloaded <fstream.h> and <_def.h> and reinstalled Code::Blocks twice now...

Having the headers is pointless if you don't also have the underlying libraries.

I believe <fstream.h> is either non-standard or an antiquated header for c++ (i think it's a C header); in which case, would make your instructor incorrect. Sometimes the real education occurs when you have to prove your instructor wrong.

According to wikipedia, the use of ".h" header files is depreciated: http://en.wikipedia.org/wiki/C%2B%2B_Standard_Library


It seems that you are trying to count the number of letters in a document:

if(letter_counter <= 'z' && letter_counter >= 'A') //|45|error: ISO C++ forbids comparison between pointer and integer|            {                letters++;            }//if

This is actually not a bad attempt at accomplishing what ye' wish to do. If your compiler doesn't like this comparison, you can actually use the ascii values for 'z' and 'A', which are 122 and 65, but if you look at the ascii table, there are other values in between 'z' and 'A', like: ':', ';', '<', '=', '>', etc...

So, one solution would involve the isalpha() function from cctype:

#include<cctype>

//char letter_counter[2] can only hold two characters..!  
//let's use a more flexible data type

string line_of_text;   // <--can hold an entire line of data


//These loops will read in the file, line by line
//and count the number of letters

int i=0, 
    size = 0;

while(getline(infile, line_of_text))
{
     size = line_of_text.size();

     do{
          if(isalpha(line_of_text[i]))
          {
               letter_counter++;
          }
    
          i++;

     }while(i < size);

     i=0;
}

Your teacher is probably using a pre-standard compiler, which means you have little choice but to also use that compiler.

She is, she's using Dev C++ which I have come to hate for many reasons. I use Code::Blocks, and it seems to work fine whenever she wants to compile it(I assume, since I'm taking my C++ course online..).

Sometimes the real education occurs when you have to prove your instructor wrong.

I have tried this. my first attempt was with using using namespace std; and #include <iostream> over the alternative #include <iostream.h> which she uses. She fails to see the "correct" way of coding, if there is such a thing.

Her reason for not conforming to the more modern ways is, and I quote:

It is the way that our book does it, so that's the way I wan't you to learn it.

yeah... I gave her two or three different sources that proved otherwise, and that was the best she could pull out of her hat -.-

anyways, back on topic:

So, one solution would involve the isalpha() function from cctype:

I like this solution, and mixed with changing my character array to a er.. non-array character(correct wordage?), I should be able to solve my problem and get my program to compile.

Thanks Narue and Clinton :)

She is, she's using Dev C++ which I have come to hate for many reasons.

Dev-C++ and Code::Blocks use the same underlying compiler (GCC from MinGW). The difference is in the IDE, and that Dev-C++ hasn't been actively worked on for years now, so there would be some default version differences. Standard C++ should work on Dev-C++, regardless of your instructor's request to use pre-standard headers. Though you could verify this, and mention that modern compilers are either in the process of phasing out those headers or have already done so and will refuse to compile code which uses them.

It is the way that our book does it, so that's the way I wan't you to learn it.

Tell her to check the publication date of that book. I'd bet my next paycheck that it's well over a decade old. C++ has evolved since then. You should be asking yourself why you're wasting money learning something that isn't applicable in the real world.

Tell her to check the publication date of that book.

You are correct, it's from '89 if I trecall correctly. I can get you the title if you like.

Does she know that it's 2011 and C++ has grown by two full language revisions?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.