C Programming help beginner

This is a discussion on C Programming help beginner within the C Programming forums, part of the General Programming Boards category; Hi Guys, Firstly let me compliment on an excellent site to help beginner developers like me. I have a hangman ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    6

    Question C Programming help beginner

    Hi Guys, Firstly let me compliment on an excellent site to help beginner developers like me. I have a hangman project due today but still feel very loss with my code and would appreciate if you could guide me in the right direction.

    I'm basically trying to load a file with words into an array while doing that i want to display the array to screen to see that is correctly reading the file. I have written my algorithm and will be going through it step by step but i'm stuck on the beginning stages with an error "line 15.12: 1506-195 (S) Integral constant expression with value greater zero is required" Please your assistance in this regardswill be appreciated.
    This is my code snippet.

    Code:
    /* Author: Me
    
    ** This is my Hangman program, what a mission it was to make this work.
    
    */
    
    #include <stdio.h>
    
    #include <stdlib.h>
    
    #include <string.h>
    
    #define CHANCES 10
    
    #define MAXIMUM 8
    
    #define maxAttempts 10
    
     
    
    int main ( void )
    
     
    
    {
    
    char MAX = 8;
    
    char words[MAX];
    
    int value, j, k;
    
    int count = 0;
    
    FILE *fp;
    
     
    
    /* Read the file of words into the array*/
    
     
    
    fp = fopen ("words.txt", "w+");
    
    for  (k =0; k < MAX; k++)
    
     
    
        {
    
            fprintf(fp,"%c\n", words[k]); // then display to see if it works.
    
        }
    
    fclose(fp);
    
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,752
    You're trying to declare a variable length array.

    You need something like
    #define MAX 8

    char word[MAX];
    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.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    6

    C programmer help beginner

    Thanks Salem, will try and get back to you.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    6
    Thanks Salem, will try and get back to you

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    6
    Hi can someone please assist me regarding displaying this array to the screen, it compiled and everything but nothing displayed after fixing the error.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,752
    You're not displaying anything on screen.

    You're just writing 8 random characters to a file at the moment.

    Perhaps you were meant to add some code of your own at this comment.
    /* Read the file of words into the array*/

    Notice that what you're doing at the moment is WRITING to a file, so it seems like you're supposed to be doing something else first.
    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
    Mar 2011
    Posts
    6
    Hi Salem, 8 random words and the comment was written by me thinking that it would display.

    I'm very new using the unix platfrom too and on top of it using c programming.

    This file was placed in the /u/myname/words.txt not sure if that will automatically pull from the correct location as seen above.

    Code:
    /* Author: Me
    ** This is my Hangman program, what a mission it was to make this work.
    */
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define CHANCES 10
    #define MAXIMUM 8
    #define maxAttempts 10
    #define MAX 8
    
    int main ( void )
    
    {
    
    char words[MAX];
    int value, j, k;
    int count = 0;
    FILE *fp;
    
     /* Read the file of words into the array*/
    
    fp = fopen ("/users/rashied/words.txt", "w+");
    for  (k =0; k < MAX; k++)
    
        {
            fprintf(fp,"%s\n", words[k]);
            printf("The words in the array are %s\n","words.txt");
        }
    fclose(fp);
    }
    
    i tried displaying with the following but still no luck of displaying the data - see below.

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    6
    sorry i change before sending

    but my display was somehting like this and still did not work.

    printf("The words in the array are %s\n", words[k])

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok, think about the steps you have to complete...
    In C there is no magic that you do not create...

    1) You need an array of x words that can be up to y characters long.
    2) You need to open the file with the words in it.
    3) You must read a word from the file into the array
    4) You need to repeat #3 until all words are read
    5) Then you need to close the file

    As pseudocode...
    Code:
    char array[words][length];
    
    Open file with words
    
    While more words to read
      {   read a word from the file
           put it into the array }
    
    close the file
    Now... try to write that as real code and see what you come up with.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ADO.NET in VS2008 or gdi+ (beginner question)
    By robgar in forum C# Programming
    Replies: 3
    Last Post: 10-04-2009, 03:40 AM
  2. Same old beginner question...
    By Sharmz in forum C Programming
    Replies: 15
    Last Post: 08-04-2008, 12:48 PM
  3. What are some good beginner programs I shouold make?
    By oobootsy1 in forum C# Programming
    Replies: 6
    Last Post: 08-09-2005, 03:02 PM
  4. Windows programming for beginner (Absolute beginner)
    By WDT in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2004, 11:21 AM

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