Dictionary into a 2d Char Array... Problems.

This is a discussion on Dictionary into a 2d Char Array... Problems. within the C Programming forums, part of the General Programming Boards category; Hi, i'm new, a uni student in the UK looking for some help on a uni assignment. I have had ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    7

    Question Dictionary into a 2d Char Array... Problems.

    Hi, i'm new, a uni student in the UK looking for some help on a uni assignment.
    I have had the work marked off with a provisional grade for if it functions correctly, but we've now got the weekend to finish the code off, make it all neat etc, and hand it in on monday.

    Thus, i need to rip out the hard-coded Dictionary, and get it to read from file.

    And this is where i come unstuck. I've tried For Loops (Not good, because the column length might be set to 10, but if the word is FRED, it's not going to stop going once it's hit D), and so far onto while loops, and i just can't get the blasted thing working.

    Here's the code (please don't laugh, i'm a 2nd year student and this is my first time with C, i'm normally a VB.NET student)

    Code:
    	if (filepointer == NULL)
    			printf("File not found"); 
    		else
    		{
    			printf("File Exists\n");
    			while (ch != EOF)
    			{
    				printf("Loading Words into Array\n");
    				printf("Dictionary %d: ", x);
    				while (ch != '.')
    				{
    					ch = getc(filepointer);
    					Dictionary[x][y] = ch;
    					printf("%c\n", Dictionary[x][y]);
    					y++;
    				}
    				Dictionary[x][y] = '\0';
    				x++;
    			}
    
    		}
    I get the theory of what i need to do.
    Insert first letter from file into the first column in the array. Increment array and insert next letter, keep going until you hit a delimiter (.) or \n depending. (i'd rather have a list so that the text file is neat, but if i need to use delimiters, so be it). Once you hit the delimiter, you then come out of the inner loop, increment the first part of the Dictionary, and go back into the loop. In theory, you eventually end up with a nice shiny list of words.

    But it doesn't work.

    If anyone can give advice, and if possible, an explanation of why not only mine doesn't work, but how to get this working (because this seems a rather unwieldy way of getting strings into an array), i would be grateful. I'm not looking for a straight answer (although, it would be nice?) just enough pointers (heh) and hints so i can work it out for myself.

    I did google this (c file to 2d array) which lead me here, but the google in question was to someone getting ints into a 2d int array, using commands specific to that.

    Thanks in advance. Hopefully once this degree is out of the way i can settle down and explore C as a language. I do like programming (i know, they all say that), i just never seem to have the time to enjoy what i'm doing. It's always "rush rush assignments, portfolios, assessments, end of module, do new stuff".

  2. #2
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    you could just do a combination of fgets and sscanf.

    Further, I believe in your version, you want fgetc, not getc.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    7
    I'm checking a few sites about sscanf (amazing how many sites have the same text on them), but i'm not sure i've got this fully.

    It analyses the string for values, and then puts the values into it's arguments (parameters, etc)...

    Ok, so i'd need something like

    Code:
    char WordBuffer[10];
    //x being an incrementer, knocking around somewhere in the loop
    sscanf(Dictionary[x], "%s", WordBuffer);
    Hmm. Can you actually put a string straight into a char array, without having to go through one line at a time? The only way we have been taught (and this is week 10, of the 12 weeks) is to go through one char at a time.

  4. #4
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    I don't see a problem with doing that. But you'd want to fgets a line in from your file, then sscanf over that line.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    7
    Ah so fgets into the word buffer, then scan over the word buffer to put it into the dictionary?

    I think the code above with my sscanf is wrong anyway. I need to go from file to wordbuffer, then wordbuffer to dictionary array. Now though it's time to read up on fgets.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    How did you declare your Dictionary array?

    You never do y=0 to reset the column index in the array. As such, at some point you'll run off the end.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Dec 2006
    Posts
    7
    Hi, for testing, the dictionary is something like

    Code:
    char *Dictionary[] = {"etc", "etc", "etc"};
    Urgh. That's true about y. That explains why before the program crashes, i get a display of "Dictionary 1982:".

    I do have another problem with this program, but i'm not sure if i can ask in this thread, or if i have to start a new one.

    Does anyone know of an easy to understand guide to pointers? We got rushed through the lecture with them, and now i'm left slightly baffled to why a char pointer, won't point to an element in a char array. (error C2440: '=' : cannot convert from 'char **__w64 ' to 'char *').

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    Well on some machines you would have failed on the first attempt to assign Dictionary[0][0] since you've initialised it with const strings.

    char Dictonary[100][10]; // 100 words, up to 10 characters in length

    > Does anyone know of an easy to understand guide to pointers?
    This one is pretty good
    http://pw1.netcom.com/~tjensen/ptr/pointers.htm
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Dec 2006
    Posts
    7
    Argh sorry. The working 'dictionary' is hardcoded in the way i put in. The one i'm using where i'm getting undone, is literally as you have said (with MAX_WORD and MAX_LETTER) as defines instead.

    Ah great, i'll go read this. Thank you!

  10. #10
    Registered User
    Join Date
    Dec 2006
    Posts
    7
    Ok i've gone through that guide, it was pretty good, although some of it was a bit over my head so far.

    I see it would be easier to use pointers to put the file into dictionary, but while *dictionary++ goes across a 2d array (i.e. goes across Dictionary[0]), i want to go down it (to Dictionary[1]). And i'm not sure how to do that.

    In that guide, it mentions something like *(dictionary + i), which in theory goes down the 2d array. But... I dunno if that's right. Sorry about this.

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    For the moment, stick with
    char Dictonary[100][10];

    Later on, when the basics are working, you can incrementally change to
    char *Dictonary[100]; // 100 variable length words
    and
    char **Dictionary; // a variable number of variable length words
    with very minimal changes to the way your code works.

    But by then, you'll know that your code works, so any problem which appears would just be down to your use of pointers in a very small area of the program.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. using selection sort with a 2D char array
    By kristentx in forum C++ Programming
    Replies: 12
    Last Post: 04-08-2006, 07:45 AM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  5. how to pass 2D array into function..?
    By IngramGc in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2001, 08:41 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21