Thread: C Programming help beginner

  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

  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, 02:40 AM
  2. Same old beginner question...
    By Sharmz in forum C Programming
    Replies: 15
    Last Post: 08-04-2008, 11:48 AM
  3. What are some good beginner programs I shouold make?
    By oobootsy1 in forum C# Programming
    Replies: 6
    Last Post: 08-09-2005, 02: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