View Full Version : error message....why?
GSXdan
09-10-2003, 05:31 PM
I am writing a function that will remove all non-letters from words that have been read in to an array. It is giving me this error:
ANSI C++ forbids comparison between pointer and integer
here is the code:
words was defined in the main cpp file as char words[200][20];
void RemoveNonLetters(char words[][20]) {
char temp[100];
int x, y;
x = 0;
y = 0;
for(x=0; x < strlen(words[x]); x++)
if(words[x] >= 'A' && words[x] <= 'Z' ||
words[x] >= 'a' && words[x] <= 'z') {
temp[y] = words[x];
y++;
}
temp[y] = 0;
strcpy(words[x], temp);
}
}
thanks
ps - i am having one of those days where i cant think of anything, so this is probably an easy fix
Hi GSXdan,
the error message is caused by the statement:
words[x] >= 'A'
word is a pointer to a pointer to char (or char**) so words[x] is a pointer to char (or char*), while 'A' is a char, and you cannot compare between them.
what you need to do is to create 2 loops,something like this:
for (int i = 0; i < 200; i++) {
for (int j = 0; j < 20; j++) {
if (words[i][j] >= 'A' && words[i][j] <= 'Z') {
// words[i][j] is the jth chracter of word i
}
}
}
Another note: the statement: temp[y] = words[x]; doesn't look like something that will work to me, fix your code, and if you still have problems, came back and post, we'll try to help :)
GSXdan
09-10-2003, 07:57 PM
wow it would help if i were taught the material right!!! i just tried the example my teacher gave me, and guess what, it doesnt work :/ So let me explain what i have to do and we can go from there.
PART 1
I need to write a program that inputs one word at a time, remvoe all punctuation that may appear at the end of the word. Then i have to convert all capital letters to lowercase. After reading in the word, store it in a character array. Continue doing that until EOF.
words is declared as char words[200][20]
PART 2
output words...
sort so words are in alphabetical order
output.....
delete all duplicates in the array
output....
find all palindromes in array
output.....
output a table indicating the number of one-letter, two-letter, three-letter words, etc that appear in the array.
Only catch is that we cant use subscripts, it has to be done with pointers only( word[i] would have to be *(word+i)
so it is basically string manipulation. while writing that i noticed it said one word at a time, so i will have to take a little different approach. we have to have a main cpp file, lab2, and words.cpp file for all functions and a words.h file for declarations. Ill post again once if i have some problems, but feel free to give me any advice on easy ways to do any of these functions.
thanks
GSXdan
09-10-2003, 09:04 PM
what #include file needs to be in to use string functions? <cstring>? i though there was something like strmanip or something like that?
thanks ^dan
GSXdan
09-10-2003, 09:17 PM
ok, here is a situation:
In the input function, I am sending the words array and a counter for the number of words. How do I return two different file types? what would the function header be?
char InputWords(char words[][20], int numwords) {
ifstream inf;
int x = 0;
int numletters = 0;
char temp[20];
numwords=0;
inf.open("lib/words.dat");
while(! inf.EOF) {
inf >> temp;
numletters = strlen(temp);
RemoveNonLetters(temp, numletters);
ChangeCase(temp, numletters);
strcpy(words[x], temp);
x++;
numwords++;
}
return;
}
i guess im rusty from summer break :)
thanks ^dan
first, the header for string functions is string.h
second, what do you mean by returning two file types? do you mean returning two different values or something?
one way to do it is to create a structure with the two return types you want, let's say you want to return an integer and a char, use:
struct ReturnStruct {
int val1;
char val2;
};
and your function header will be:
ReturnStruct someFunction();
somewhere in the function:
ReturnStruct mystruct;
mystruct.val1 = 1;
mystruct.val2 = 'a';
return mystruct;
and check your other thread :)
HTH
GSXdan
09-14-2003, 09:58 AM
thanks for your help.
^dan
vBulletin® v3.7.0, Copyright ©2000-2008, Jelsoft Enterprises Ltd.