Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 09-10-2003, 04:31 PM   #1
Member (10 bit)
 
GSXdan's Avatar
 
Join Date: Jun 2002
Location: P-burg, Ohio, USA
Posts: 555
Send a message via AIM to GSXdan
error message....why?

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
GSXdan is offline   Reply With Quote
Old 09-10-2003, 06:13 PM   #2
aym
Registered User
 
aym's Avatar
 
Join Date: Nov 2001
Posts: 1,965
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:
PHP Code:
for (int i 0200i++) {
    for (
int j 020j++) {
        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
aym is offline   Reply With Quote
Old 09-10-2003, 06:57 PM   #3
Member (10 bit)
 
GSXdan's Avatar
 
Join Date: Jun 2002
Location: P-burg, Ohio, USA
Posts: 555
Send a message via AIM to GSXdan
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 is offline   Reply With Quote
Old 09-10-2003, 08:04 PM   #4
Member (10 bit)
 
GSXdan's Avatar
 
Join Date: Jun 2002
Location: P-burg, Ohio, USA
Posts: 555
Send a message via AIM to GSXdan
what #include file needs to be in to use string functions? ? i though there was something like strmanip or something like that?

thanks ^dan
GSXdan is offline   Reply With Quote
Old 09-10-2003, 08:17 PM   #5
Member (10 bit)
 
GSXdan's Avatar
 
Join Date: Jun 2002
Location: P-burg, Ohio, USA
Posts: 555
Send a message via AIM to GSXdan
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?

Code:
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
GSXdan is offline   Reply With Quote
Old 09-14-2003, 07:27 AM   #6
aym
Registered User
 
aym's Avatar
 
Join Date: Nov 2001
Posts: 1,965
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:
PHP Code:
struct ReturnStruct {
    
int    val1;
    
char val2;
}; 
and your function header will be:

PHP Code:
ReturnStruct someFunction(); 
somewhere in the function:
PHP Code:
ReturnStruct mystruct;
mystruct.val1 1;
mystruct.val2 'a';
return 
mystruct
and check your other thread

HTH
aym is offline   Reply With Quote
Old 09-14-2003, 08:58 AM   #7
Member (10 bit)
 
GSXdan's Avatar
 
Join Date: Jun 2002
Location: P-burg, Ohio, USA
Posts: 555
Send a message via AIM to GSXdan
thanks for your help.

^dan
GSXdan is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 08:06 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2