Thread: In need of help. Please.

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    69

    In need of help. Please.

    Ok I have written so code that is in a text format from a file. It is supposed to separate the words and then eliminate "the", "and", "an", "for", "a", and "of".

    I'm trying to KISS. But I'm hung up on allocating the string of characters to memory. I can get the string of charcters to print in but am have troubles getting it to run past my calloc memory allocation. Can you please help. I am not sure where I'm going wrong. This is my code.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define FLUSH while (getchar() != '\n')
    
    int main (void) 
    {
    //	Local Declarations
    	char   strng[1000];
    	char** pWords;         // array of pointers to char
    	FILE* spIn;
    
        int size = 100;
    	int wordIndex;
    
    //	Statements
    	printf("Data to Manipulate: \n\n");
    	if (!(spIn = fopen ("c:\\sample.txt", "r")))
    	 {
    		printf("\aError could not open.\n");
    	    system("Pause");
    		exit (100);
    	 }//if
    	while (fgets (strng, sizeof (strng), spIn))
    	 {
    	    printf("%s", strng);
    	 }
    	printf("\n\n");
    
        // Allocate array in heap.
    	// One extra element added for loop control 
    	pWords = (char*)calloc (size + 1, sizeof *(char*));
    	
    		
    	wordIndex = 0;
    	while (wordIndex < size 
    	    && fgets(strng, sizeof(strng), spIn))
    	    {
    	     *(pWords + wordIndex)  = (char*) 
    	          calloc (strlen(strng) + 1, sizeof(char));
    	     strcpy (*(pWords + wordIndex), strng);
    	     wordIndex++;
    	   } // while
    	
    	*(pWords + wordIndex) = NULL;
    	printf("\nList of words are: \n");
    	wordIndex = 0;
    	while (*(pWords + wordIndex))
    	   {
    	    printf("%3d: %s", 
    	            wordIndex, *(pWords + wordIndex));
    	    wordIndex++;
    	   } // while
    	system("Pause");
    	return 0;
    }	// main
    And here is the File used for in.
    ****
    Creating a national seal was a more difficult task then our founding fathers
    believed it would be. The idea was to create a national seal that would
    illustrate the principles and ideals of our new nation. The challenge was
    agreeing upon the symbols that would best represent the thirteen states as a
    whole, the government including Congress, the Senate, and the Commander and
    Chief while representing the ideals and principles fought for and resulted in
    blood shed. The process of designing the seal was one of heated debate and
    disagreement. The design of the seal ended up being a six-year project
    resulting in several proposed designs from three separate committees.
    ****
    Thank you in advance. I can'yt move on until I figure this out and I'm at my wits end. PLease Please help.

    Thank you!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Shouldn't this:
    Code:
    pWords = (char*)calloc (size + 1, sizeof *(char*));
    be this:
    Code:
    pWords = calloc (size, sizeof (char*));
    You want pWords to have enough room for 100 pointers-to-char (you don't need to null-terminate an array! you've got a counter that tells you how many words you have), and then you can allocate memory later to pWords[0], pWords[1], etc. (which is going to be easier to write and read than your pointer arithmetic version) once you know what each word is.
    And didn't your compiler complain about that char* that you cast calloc to being assigned to a char**?

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    69

    I will try

    My compile kept saying I needed to add a ). While I kept trying to adjust the code on the line I kept getting a variety of reads but could not determine the problem. I will try this. Do I need to eliminate the NULL reference altogether?

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    69

    Still having a problem.

    I copy and pasted your recommended code to my program. Ran the debug and this is what I get again. This is the same error message I've gotten regardless of what I have tried.

    Code:
                    // Allocate array in heap.
    	// One extra element added for loop control 
    	pWords = calloc (size, sizeof (char*));
    Error message again same line of code. What am I doing wrong?

    c:\documents and settings\administrator.home-inceydwazx\my documents\visual studio 2005\projects\chapter11\chapter11\test.cpp(37) : error C2440: '=' : cannot convert from 'void *' to 'char **'
    Conversion from 'void*' to pointer to non-'void' requires an explicit cast
    Generating Code...

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You are compiling C++, not C.
    In C++, you need a cast, in C you do not.
    Since this is just C, you can rename your test.cpp file to test.c to compile it as C.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > #define FLUSH while (getchar() != '\n')
    You should also check for EOF.

    It'd probably be best if this was a function instead of a macro.

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    69

    In need of help.

    I was trying to make this into function and call upon them but, I'm not sure I'm understanding how could Use some help please.

    Thank you.

  8. #8
    Registered User
    Join Date
    Nov 2007
    Posts
    69

    I tried casting the type.

    ok. I've casted the type so that calloc to take care of the type casting of void to char. However then I get the error can't convert from char to char** I have tried dereferencing the char and other coding scenerios but I'm getting a big zip.

    Please help.

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    If you're going to compile this as C++ code, the casting stuff should look like this:
    Code:
    pWords = (char **)calloc (size, sizeof *pWords);
    You need to rewind the file pointer before you start populating your array.
    Code:
    fseek(spIn, 0, SEEK_SET);
    Last edited by rags_to_riches; 02-10-2008 at 08:26 PM. Reason: Forgot to address last post

  10. #10
    Registered User
    Join Date
    Nov 2007
    Posts
    69

    What.

    I am sorry what do you mean by rewind the file pointer. And where would I do this? I appreciate you help but I'm lost. Could you explain more.

    Thank you.

  11. #11
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    You open the file at the beginning to read and print each line in the text file. Once you've done this, the file pointer, spIn, is at the end of the file. This causes the first fgets, when you're starting to read your data into the pWords array, to fail and the array does not get populated at all.

    By putting the fseek call before you reset your wordIndex counter to 0, the array population will work as you expect.

  12. #12
    Registered User
    Join Date
    Nov 2007
    Posts
    69
    Your advice worked and I see what you mean. Thank you. Now I'm trying to get the program to print individual non repeat of the words. It's printing full sentences. Can you help. Could you advise me to the method I should approach.

    Thank you I do appreciate your help.

  13. #13
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    You should probably be looking at strstr to find the words.

  14. #14
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    Quote Originally Posted by silhoutte75 View Post
    I was trying to make this into function and call upon them but, I'm not sure I'm understanding how could Use some help please.

    Thank you.
    Code:
    int ch;
    
    while( ( ch = getchar() ) != '\n' && ch != EOF )
        ; /* the work is done in the loop */
    Of course a macro is still possible.

    Code:
    #define FLUSH(x) while( ((x) = getchar() ) != '\n' && (x) != EOF );
    in function call:
    Code:
    int ch;
    FLUSH(ch)
    Last edited by Aia; 02-10-2008 at 09:40 PM.
    When the eagles are silent, the parrots begin to jabber. ~Winston Churchill

  15. #15
    Registered User
    Join Date
    Nov 2007
    Posts
    69
    Would I use a str to str comparison for that? I think I'm running into a problem. Could you help.

Popular pages Recent additions subscribe to a feed