ISO C++ forbids comparison between pointer an

Microsoft Visual studio 2008 professiona...
February 4, 2010 at 13:40:25
Specs: Windows XP
I get an error when I try to compile. The code is simple enough, I'm trying to print to the console and an external file. I mark out the error further down the page. I don't know enough about C++ to try to begin to understand why this is happening. Any help will be greatly appreciated.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

#define FIELD_WIDTH = 8;

int pause(char *str);

int main(void) {
void readWrite(ifstream &, ofstream &);

string name;
char* inFileName;
ifstream inFile;
ofstream outFile;
int countChar=0;
int countNonSpaceChar=0;
int countWord=0;
int countLine=0;
int countWhite=0;
int countTab=0;
int countOtherEscChar=0;
char x;

system("cls");
cout << "Enter your name: ";
cin >> name;

cout << name <<" enter the location of your file: ";
cin >> inFileName;

inFile.open(inFileName);
if(inFile.fail()) {
pause("Unable to open file. Please check spelling and try again.\nPress Enter <-' to exit programme...");
exit(1);
}
else {
while(inFile.good()) {
inFile.get(x);
countChar++;

This is where the error occurs. When I use single quotes the prgramme runs but the console window crashes.
if(x==" ") {

countWhite++;
if(countChar>countWhite+countTab+countLine)
countWord++;
}
else if(x=='\t') {
countTab++;
if(countChar>countWhite+countTab+countLine)
countWord++;
}
else if(x=='\n') {
countLine++;
if(countChar>countWhite+countTab+countLine)
countWord++;
}
else if(isascii(x) == 0){
countOtherEscChar++;
}
}
}

countNonSpaceChar = countChar - countWhite - countLine - countTab - countOtherEscChar;

cout << "The file contains:";
cout << countChar << "characters" << endl;
cout << countNonSpaceChar << "characters (non space)" << endl;
cout << countWord << "words" << endl;
cout << countLine << "lines" << endl;
cout << countWhite << "spaces" << endl;
cout << countTab << "tabs" << endl;
cout << countOtherEscChar << "other escape characters" << endl;
cout << "\n\n\nAll done\nPress Return <-' to exit programme" << endl;

outFile.open("output.txt", ios::out);

outFile << "The file contains:";
outFile << countChar << "characters" << endl;
outFile << countNonSpaceChar << "characters (non space)" << endl;
outFile << countWord << "words" << endl;
outFile << countLine << "lines" << endl;
outFile << countWhite << "spaces" << endl;
outFile << countTab << "tabs" << endl;
outFile << countOtherEscChar << "other escape characters" << endl;

outFile.close();

pause("");
return 0;
}

//---------------------------------------------------------------------------
int pause(char *str) {
cout << str;
cin.sync();
return cin.get();
} // End of pause() function


See More: ISO C++ forbids comparison between pointer an

Report •


#1
February 4, 2010 at 16:40:22
I had a look over your code and the reason it was crashing was this:

char* inFileName;

<snip>

cin >> inFileName;

You were sending string input into a to a pointer.

Since you were including <string> I simply turned "inFileName"
into a string and used:
"inFile.open(inFileName.c_str());".


As for the conversion problem I silenced my compiler using
if "(!strncmp(&x, " ", 1)) ".

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

#define FIELD_WIDTH = 8;

int pause(char *str);

int main(void) {
void readWrite(ifstream &, ofstream &);

string name;
string inFileName;
ifstream inFile;
ofstream outFile;
int countChar=0;
int countNonSpaceChar=0;
int countWord=0;
int countLine=0;
int countWhite=0;
int countTab=0;
int countOtherEscChar=0;
char x;

system("cls");
cout << "Enter your name: ";
cin >> name;

cout << name <<" enter the location of your file: ";
cin >> inFileName;


inFile.open(inFileName.c_str());

if(inFile.fail()) {
pause("Unable to open file. Please check spelling and try again.\nPress Enter <-' to exit programme...");
exit(1);
}
else {
while(inFile.good()) {

inFile.get(x);
countChar++;

//This is where the error occurs. When I use single quotes the prgramme runs but the console window
//crashes.

if (!strncmp(&x, " ", 1)) {

countWhite++;
if(countChar>countWhite+countTab+countLine)
countWord++;
}
else if(x=='\t') {
countTab++;
if(countChar>countWhite+countTab+countLine)
countWord++;
}
else if(x=='\n') {
countLine++;
if(countChar>countWhite+countTab+countLine)
countWord++;
}
else if(isascii(x) == 0){
countOtherEscChar++;
}
}
}

countNonSpaceChar = countChar - countWhite - countLine - countTab - countOtherEscChar;

cout << "The file contains:";
cout << countChar << "characters" << endl;
cout << countNonSpaceChar << "characters (non space)" << endl;
cout << countWord << "words" << endl;
cout << countLine << "lines" << endl;
cout << countWhite << "spaces" << endl;
cout << countTab << "tabs" << endl;
cout << countOtherEscChar << "other escape characters" << endl;
cout << "\n\n\nAll done\nPress Return <-' to exit programme" << endl;

outFile.open("output.txt", ios::out);

outFile << "The file contains:";
outFile << countChar << "characters" << endl;
outFile << countNonSpaceChar << "characters (non space)" << endl;
outFile << countWord << "words" << endl;
outFile << countLine << "lines" << endl;
outFile << countWhite << "spaces" << endl;
outFile << countTab << "tabs" << endl;
outFile << countOtherEscChar << "other escape characters" << endl;

outFile.close();

pause("");
return 0;
}

//---------------------------------------------------------------------------
int pause(char *str) {
cout << str;
cin.sync();
return cin.get();
} // End of pause() function

On another not most a c++ string operates are really nice to
work with compared to c strings.

http://www.cplusplus.com/reference/...



My site / some batch info


Report •

#2
February 5, 2010 at 10:00:55
Thanks a bunch. It's working, all I have to do now is tweak my
output file so it appears with the proper formatting. Thanks
again.

Report •

#3
February 5, 2010 at 13:14:30
if (!strncmp(&x, " ", 1))

is unnecessary. What you had originally was fine:

if (x == ' ')

The above was not the cause of the crash, so you can revert to
it.


Report •
Related Solutions


Ask Question