GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 11-Mar-2004, 12:02
warny_maelstrom warny_maelstrom is offline
Junior Member
 
Join Date: Jan 2004
Posts: 41
warny_maelstrom will become famous soon enough

Pointers-general confusion of pointer type.


Iv been working on a spellchecker for quite a while and im stucking try to get the program to simply take in one word at a time. Im sure theres a problem with the pointers being of the wrong type or iv written it badly at some point, im now left with the error "ERROR: An expression of type '<ptr>char' cannot not be converted to the function return of type 'int'" its pretty obvious wot the problem is but i just cant seem to solve it. Any help would b welcome.
CPP / C++ / C Code:
#include <stdio.h> 
#include <string.h>
#include <stdlib.h>

FILE *ref;
char line[100]; 
char* new_word;
int* pos;
char* split_line(char* line, int* pos);

int main()
        {
        ref = fopen ("reference.txt", "r");
        fgets (line, 100 ,ref);
        while( new_word==split_line(line, pos) )
        printf("%s\n",new_word);
        fclose(ref);
         
        char temp[100];
        char* word;
        int  count = 0;      //Tracks array postion of temp

        while( (*(line + *pos)!=' ')&&(*(line + *pos))!=',')
                {
                temp[count] = *(line + *pos);       //Assign character by character
                count++;
                *pos++;
                printf("%c\n",temp[count]);
                }
        temp[count] = '0';                      //Null terminate the word.
        word = (char*) malloc( strlen(temp) + 1);     //Mallocate just enough room for the word
        strcpy(word,temp);                    //Copy the temporary storage into word
        return(word);
       }
  #2  
Old 11-Mar-2004, 12:15
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi Warny. It looks like you are trying to get the code working that I gave you. I am on my way out the door, so I didn't look at it too close, but I noticed the biggest problem is that you haven't split out the split_word function. Basically this:

CPP / C++ / C Code:

        while( (*(line + *pos)!=' ')&&(*(line + *pos))!=',')
                {
                temp[count] = *(line + *pos);       //Assign character by character
                count++;
                *pos++;
                printf("%c\n",temp[count]);
                }
        temp[count] = '0';                      //Null terminate the word.
        word = (char*) malloc( strlen(temp) + 1);     //Mallocate just enough room for the word
        strcpy(word,temp);                    //Copy the temporary storage into word
        return(word);


should be included in the split_word function that you make. Right now you have declared it, but it doesn't exist.

I will look at this more later, but I just wanted to clarify that point.

ciao,
d
  #3  
Old 11-Mar-2004, 12:45
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,245
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by warny_maelstrom
Iv been working on a spellchecker for quite a while and im stucking try to get the program to simply take in one word at a time. Im sure theres a problem with the pointers being of the wrong type or iv written it badly at some point, im now left with the error "ERROR: An expression of type '<ptr>char' cannot not be converted to the function return of type 'int'" its pretty obvious wot the problem is but i just cant seem to solve it. Any help would b welcome.
CPP / C++ / C Code:
#include <stdio.h> 
#include <string.h>
#include <stdlib.h>

FILE *ref;
char line[100]; 
char* new_word;
int* pos;
char* split_line(char* line, int* pos);

int main()
        {
        ref = fopen ("reference.txt", "r");
        fgets (line, 100 ,ref);
        while( new_word==split_line(line, pos) )
        printf("%s\n",new_word);
        fclose(ref);
         
        char temp[100];
        char* word;
        int  count = 0;      //Tracks array postion of temp

        while( (*(line + *pos)!=' ')&&(*(line + *pos))!=',')
                {
                temp[count] = *(line + *pos);       //Assign character by character
                count++;
                *pos++;
                printf("%c\n",temp[count]);
                }
        temp[count] = '0';                      //Null terminate the word.
        word = (char*) malloc( strlen(temp) + 1);     //Mallocate just enough room for the word
        strcpy(word,temp);                    //Copy the temporary storage into word
        return(word);
       }
Where is the error happening? Place a comment on the line.
Also, comment your code so we know what is happening. What is "reference.txt"? The dictionary? The text to spell check?
split_line() seems to be undefined. And what is the first while loop supposed to do? Just to check the split_line() function?
  #4  
Old 11-Mar-2004, 21:26
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi Warny.

I have looked over the code and modified it to work. This is a pretty involved procedure and there may be easier and more straight forward ways to do this, but I like using the plain old char class and writing my own low level functions.

Again I urge you to study this and ask questions where you don't understand it. If you just can't understand, post back and see if you can find an alternative solution.

CPP / C++ / C Code:
#include <stdio.h> 
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

char* split_line(char* line, int* pos)
{
	char	temp[100];
	char*	word;
	int		count = 0;      //Tracks array postion of temp
	
	//Clear whitespace - Need to pass all initial spaces or commas encountered.
	while( ( isspace( *(line+*pos) ) )
			|| ( (*(line + *pos))==',') )
		(*pos)++;
	
	//If after clearing space it is a null return 0
	if(*(line+*pos) == 0) 
		return 0;
	
	
	//Loop and copy string until whitespace or comma.
	while( ( !isspace( *(line+*pos) ) ) && ( (*(line + *pos))!=',') ){
		temp[count] = *(line + *pos);       //Assign character by character
		count++;							//Index temp count position
		(*pos)++;							//Index full line position	
	}
	temp[count] = 0;                      		//Null terminate the word.
	word = (char*) malloc( strlen(temp) + 1);     //Mallocate just enough room for the word
	strcpy(word,temp);                    //Copy the temporary storage into word
	
	return(word);
}



int main()
{
	FILE*	ref;
	char	line[100]; 
	char*	new_word;
	int		pos;
	
	ref = fopen ("split.cpp", "r");
	while(fgets(line, 100 ,ref)){
		printf("line: %s\n",line);
		pos = 0;
		while( new_word=split_line(line, &pos) )
        	printf("\tword: %s\n",new_word);
	}
	
	fclose(ref);
         
	return 0;
}
 
 

Recent GIDBlogDeveloping GUIs with wxPython (Part 3) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:40.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.