Thread: Started over please help.

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

    Started over please help.

    Ok started over with my code. First trying to make it easier to read.
    Second was hard to read so I goot confused. Now I am trying to separate my project into proper function calls. I need my program to read the file, then parse it into separte words. Max number of words is a 100 and not repeat any word. I have started but am stuck.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define FLUSH while (getchar() !='\n')
    #define STR_LEN 81
    
    //Function Declarations
    void initialize(int* index, char strng[]);
    
    
    int main(void)
    {
    //	Local Declarations
    	char   strng[1000];
    	char** pWords;
        FILE* spIn;
        
        int size = 100;
    	
    //	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("&#37;s", strng);
    	 }
    	printf("\n\n");
    	FLUSH;
    
    	//Allocate array in heap.
    	pWords = (char **)calloc (size, sizeof *pWords);
    	printf("tests\n");
    
    	int index = 0;
        initialize(&index,  strng);
    	fgets(strng, sizeof (strng), spIn);
    	while (spIn)
    	 { 
    		 sscanf(spIn, "%16[^'EOF']%4d", *(strng), &index);
    		 index++;
    	 }//while
    
    
    	system("PAUSE");
    	return 0;
    }
    /*================================initialize===================================
    Build the table for the string of characters. 
       Pre- file in
       Post words counted 
    */
    void initialize(int* index, char strng[])
    {
    	//local Declarations
    	 int j;
    	 //pWords = 0;
    
    	 for(j = 0; j < 100; j++)
    	 printf("%4d = *strng[j]", j, *strng);
    
    	 system("Pause");
    	 return;
    }//initialize
    Can't get past my fuction call for initialize. What am I missing please help.

    Thank you.
    Last edited by silhoutte75; 02-11-2008 at 11:31 PM. Reason: more changes

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    You've defined wordIndex as an int. However, in the initialize function prototype, and in the function declaration, you say you are passing a pointer to an int. Pick one.

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Also, in the last printf call in the code, you're missing the arguments for the &#37;d format specifier. This means the value displayed could be anything, but as integers are passed by value to printf, it shouldn't really result in a segfault.

    If you're having errors, post them too - and remember to modify the posted code on the boards every time you modify it according to our suggestions.

    EDIT:

    Also, in your initialize function, why are you zeroing all the string? Passing the string as a pointer means you can modify the memory it points to, which is what you're doing. I can't imagine that you want it all to be zero?
    Last edited by IceDane; 02-11-2008 at 09:38 PM.

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    69
    Run-Time Check Failure #3 - The variable 'wordIndex' is being used without being initialized.

    Ok added back in the line for initialization, made the changes as suggested and they worked perfectly. NOw for my next problem. Is the runtime error I get when I put the initialize back in. Can you help. Thank you.

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

    Still stumped.

    I have been unable to figure out the problem with the initialization code.

    Code:
    int wordIndex = 100;
        initialize(wordIndex,  *(strng));
    
    
    
    	system("PAUSE");
    	return 0;
    }
    /*================================initialize===================================
    Build the table for the string of characters. 
       Pre- file in
       Post words counted 
    */
    void initialize(int* wordIndex, char strng[])
    {
    	//local Declarations
    	 int j;
    	 //pWords = 0;
    
    	 for(j = 0; j < 1000; j++)
    	 printf("%4d = *strng[j]", j, *(strng));
    
    	 system("Pause");
    	 return;
    }//initialize
    Please help I'm really pressed for time. Due in less than 30 minutes and I feel very frustrated. Been at it all day.

  6. #6
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    try
    Code:
    initialize(&wordIndex,  strng);
    Pointers are a B**** especially for beginners. Even experienced programmers can have problems.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  7. #7
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Quote Originally Posted by silhoutte75 View Post
    I have been unable to figure out the problem with the initialization code.
    Code:
    int wordIndex = 100;
        initialize(wordIndex,  *(strng));
    You are calling initialize with a dereference pointer in the latest code.
    Try this:
    Code:
        initialize(&wordIndex, strng);
    Inside initialize:
    Code:
    void initialize(int* wordIndex, char strng[])
    {
    	//local Declarations
    	 int j;
    	 //pWords = 0;
    
    	 for(j = 0; j < 1000; j++)
    	 printf("&#37;4d = *strng[j]", j, *(strng));  /* print every character of strng ? */
    
    	 system("Pause");
    	 return;
    }//initialize
    Maybe try this if you want to do what i write in the comment.
    Code:
             printf("%4d = %c", j, *strng);
    Your statement prints literally 0000 = *strng[j]0001 = *strng[j]... etc
    Last edited by xuftugulus; 02-11-2008 at 10:55 PM.

  8. #8
    Registered User
    Join Date
    Nov 2007
    Posts
    69
    Ok did you suggestion and it is still getting hung up there at the initializer. I am stuck still.

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

    This is what I'm trying to do.

    Code it so that it prints not each character but each word separtely.

  10. #10
    Registered User
    Join Date
    Nov 2007
    Posts
    69
    Would you be so kind as to help. I need help please...

  11. #11
    Registered User
    Join Date
    Nov 2007
    Posts
    69
    c:\documents and settings\administrator.home-inceydwazx\my documents\visual studio 2005\projects\11chapter\11chapter\chapter11assign-45.cpp(45) : error C2664: 'sscanf' : cannot convert parameter 1 from 'FILE *' to 'const char *'
    Code:
    int index = 0;
        //initialize(&index,  strng);
    	//fgets(strng, sizeof (strng), spIn);
    	while (spIn)
    	 { 
    		 sscanf(spIn, "&#37;16[^'EOF']%4d", strng, &index);
    		 index++;
    	 }//while
    A piece at a time. It prints up to 100 indexs like specified but get *strng[j] for every index. I don't get words and I don't know what it is that I'm doing incorrectlly pleae help.
    Last edited by silhoutte75; 02-11-2008 at 11:44 PM. Reason: Let out info.

  12. #12
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355

    Lightbulb

    Quote Originally Posted by silhoutte75 View Post
    Code it so that it prints not each character but each word separtely.
    Then you need a complete rewrite of your code. You are loading into the 1000 byte buffer, lines from the file until you run out of them. Then you allocate correctly an array to store 100 words. But that is not used.
    My guess is the you want to first print the words in initialize() and then try to see what to do.
    You could do this:
    Code:
    int initialize(char **words, char *strng) {
        char *ws = strng;
        char *c;
        int wn = 0;
    
        while( *ws  && isspace(*ws))
            ws++;        /* Skip leading whitespace */
        if(*ws == 0)
            return 0;    /* We found only space - 0 means 0 words*/
        c = ws;
        while(*c != 0) {
            while(*c && !isspace(*c)) c++;   /* Skip word characters */
            *c = 0;       /* Mark end of word */
            words[wn] = ws;   /* Save the found word */
            wn++;
            ws = c;
            while(*ws && isspace(*ws))
            ws++;        /* Skip whitespace */
            c = ws;
        }
        return wn;   /* Return number of words read */
    }
    The above code, will indeed split your strng variable to words. It will use the memory that
    belongs to strng to store these words. If you need to read more data into the buffer you call strng, you will have to make copies of the stored words. Where i note that i save the found word, i should use something like.
    Code:
    words[wn] = malloc(c-ws);
    strcpy(words[wn], ws);
    Also the use of pointers may make it hard for you to read, but it is the most elegant
    solution.
    Oh, and don't forget:
    #include <ctype.h>
    is needed for calling isspace()....
    Good luck with whatever those words are going to be used to!
    Last edited by xuftugulus; 02-12-2008 at 01:50 AM.

  13. #13
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    sscanf() is used to read formatted input from a string.
    taken from stdio.h
    int sscanf(const char *s, const char *format, ...);
    You are giving it a FILE * which is not a const char *.

    Ok i see the modified code.
    Code:
    fgets(strng, sizeof (strng), spIn);
    	while (spIn)
    	 { 
    		 sscanf(spIn, "%16[^'EOF']%4d", *(strng), &index);
    		 index++;
    	 }//while
    First you need to rewind the file. You have exhausted it when you print the contents.
    I also understand you use sscanf to read from strng, but you have totally messed up.
    Understand that strng, is a pointer to some memory and *strng, is not a pointer but the value at the first position at that memory. You can not pass that to a function, like sscanf which is expecting a pointer and not expect the program to crash. Also if you are reading the variable index from the file, why increment it then? What should index be after the file is read? If i understand what the file you read contains, i might be able to help.
    Last edited by xuftugulus; 02-12-2008 at 12:13 AM.

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

    File being read

    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.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You mentioned you started over due to unreadable code? This is usually why we use indentation. Have a read it. It can help a lot in making readable code.
    But hey xuftugulus, you don't think 2 spaces for indentation is a little cheap?
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help getting started..matrices
    By BobDole11 in forum C Programming
    Replies: 7
    Last Post: 11-15-2008, 09:51 PM
  2. Help getting started :(
    By blackocellaris in forum C Programming
    Replies: 4
    Last Post: 11-05-2006, 06:50 PM
  3. Getting started
    By panzeriti in forum Game Programming
    Replies: 3
    Last Post: 06-28-2003, 10:13 AM
  4. Getting Started
    By UnclePunker in forum Game Programming
    Replies: 3
    Last Post: 04-11-2003, 12:34 PM
  5. How to get started?
    By anoopks in forum Linux Programming
    Replies: 0
    Last Post: 01-14-2003, 03:48 AM