Thread: Creating a struct help

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    21

    Creating a struct help

    I am trying to make this scoring system into a struct, but I am not too sure what is wrong. Any nudges in the right direction?

    Code:
    #include <stdio.h>
    #include <time.h>
    
    
    
    
    
    
    void score(int []);
    void get_letters(char []);
    
    
    
    
    int main () {
        int selection;
        char letter;
        int score = 0;
    
    
      printf("Welcome to Scabble Mania!\n");
      printf("Here are your letters!\n");
    
    
        return ;
    }
    
    
    void score (int[]) {
    switch (letter) {
    case 'A':
    case 'E':
    case 'I':
    case 'L':
    case 'N':
    case 'O':
    case 'R':
    case 'S':
    case 'T':
    case 'U':
        score = 1;
        break;
    case 'D':
    case 'G':
        score = 2;
        break;
    case 'B':
    case 'C':
    case 'M':
    case 'P':
        score = 3;
        break;
    case 'F':
    case 'H':
    case 'V':
    case 'W':
    case 'Y':
        score = 4;
        break;
    case 'K':
        score = 5;
        break;
    case 'J':
    case 'X':
        score = 8;
        break;
    case 'Q':
    case 'Z':
        score = 10;
    }
    It is for a scrabble game and I need to make the scoring system, which I think I have done right, into a struct that I can call inside the program.

    Thanks

  2. #2
    Registered User
    Join Date
    Sep 2012
    Posts
    21
    Sorry for the incredibly long blank at the end of my code.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    A structure is a way of grouping variables. A scoring system, which would require code, is best done within a function (as you have done).

    I'm not quite sure how you're approaching this, but for the "score()" function, I would suggest:

    - Declaring a local variable named "score" (or something slightly different if you don't want confusion between the function name and a variable name local to that function) before using it
    - Declaring the function return an "int", and return the variable "score" at the end of it.
    - Passing a single "char" to the function (which you designated as the variable "letter" as seen in your switch statement).

    If I misunderstood your intent, please clarify exactly what you are trying to accomplish and how.

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    21
    The user will be inputting a word from 7 randomly generated letter I give them, then that word will pass through this function to determine how many points should be awarded.
    I declared a variable called points and switched score with points everywhere in the switch statement.
    I changed the function to return points.
    I am not too sure what you mean by the last point
    I think you understand my intent correctly.
    I am getting two errors now:
    on line 21: parameter name omitted
    and on line 65: returning a value with a function returning void.
    Here is the code I have now (A few things not related to this question have been added)

    Code:
    int main () {
        int selection;
        char letter;
        int score = 0;
    
    
      printf("Welcome to Scabble Mania!\n");
      printf("Here are your letters!\n");
    
    
        return ;
    }
    
    
    void score (int[]) {
        int points;
        char letter;
    switch (letter) {
    case 'A':
    case 'E':
    case 'I':
    case 'L':
    case 'N':
    case 'O':
    case 'R':
    case 'S':
    case 'T':
    case 'U':
        points = 1;
        break;
    case 'D':
    case 'G':
        points = 2;
        break;
    case 'B':
    case 'C':
    case 'M':
    case 'P':
        points = 3;
        break;
    case 'F':
    case 'H':
    case 'V':
    case 'W':
    case 'Y':
        points = 4;
        break;
    case 'K':
        points = 5;
        break;
    case 'J':
    case 'X':
        points = 8;
        break;
    case 'Q':
    case 'Z':
        points = 10;
    
    
        return points;
    }
    
    
    void get_letters(int letters [7]) {
        srand(time(0));
        int i;
        int temp;
        letters[0] = rand ()%25 + 65;
        int check;
        for (i=1; i<7; i++)
            check = 1;
            while (check = 1) {
            temp = rand()%26 + 65;
            if(temp != letters[i-1]) {
            letters[i] = temp;
            check = 0;
            break;
            }
            }
    } return;
    }
    Thank you for your help

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    21
    Not sure why it added all the white space again. It wasn't there when I posted and appeared when I hit refresh

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I am at work right now, so I can't dig into this as much as I'd like (though maybe at lunch time). However, in the mean time, some quick things I see:

    - It's accepted practice to "return 0" from main for successful program completion.

    - "score()" is returning an integer ("points"), but the function does not have a return type (you have it declared as "void"). This should be corrected.

    - Ditto for "get_letters()" - though if you're not returning anything from this function, keep the "void" but get rid of the return statement.

    - Perhaps you're missing some text at the top when you copy+paste, but I don't see any preprocessor directives (#includes). Also, if functions are defined after "main()", they must be at least declared before "main()"

    Code:
    #include <stdio.h>
    
    int addNumbers(int x, int y);  // <--- function declaration (note semicolon)
    
    int main(void)
    {
        int sum;
    
        sum = addNumbers(3,4);
    
        printf("The sum is %d\n",sum);
    
        return 0;
    }
    
    int addNumbers(int x, int y)   // <--- function definition (note no semicolon)
    {
        return x+y;
    }
    Also, in the argument to "while," you have the wrong operator (you want equality operator, ==, not the assignment operator, =).

    Code:
    while (check = 1)

  7. #7
    Registered User
    Join Date
    Sep 2012
    Posts
    21
    I have done everything you have outlined and I have no errors but one warning.
    On line 34 it says parameter name omitted, not sure how that affects my program.
    Other than that, when I print out my random letters, I get a couple of 4 letters then a box an up arrow and the u with a tail.
    Don't worry too much, you have already been a great help to me.
    Here is what I have right now: no rush for a response
    Also when I run this, my program completely skips over enter word and scanf section after it gives me my letters and just ends.
    Code:
    #include <stdio.h>
    #include <time.h>
    
    
    
    
    
    
    int score(int []);
    void get_letters(int []);
    
    
    
    
    int main () {
        int selection, points, i;
        char letter;
        int score = 0;
        int playable_letters[7];
        char word;
    
    
      printf("Welcome to Scabble Mania!\n");
      printf("Here are your letters!\n");
    
    
      get_letters(playable_letters);
    
    
      for (i=0; i<7; i++) {
      printf("%c\n", playable_letters[i]);
      }
        printf("Please enter your word\n");
        scanf("%c", word);
        score;
        printf("Your score is %d", points);
    
    
        return 0;
    }
    
    
    int score (int[]) {
        int points;
        char letter;
    switch (letter) {
    case 'A':
    case 'E':
    case 'I':
    case 'L':
    case 'N':
    case 'O':
    case 'R':
    case 'S':
    case 'T':
    case 'U':
        points = 1;
        break;
    case 'D':
    case 'G':
        points = 2;
        break;
    case 'B':
    case 'C':
    case 'M':
    case 'P':
        points = 3;
        break;
    case 'F':
    case 'H':
    case 'V':
    case 'W':
    case 'Y':
        points = 4;
        break;
    case 'K':
        points = 5;
        break;
    case 'J':
    case 'X':
        points = 8;
        break;
    case 'Q':
    case 'Z':
        points = 10;
    
    
        return;
    }
    
    
    void get_letters(int letters [7]) {
        srand(time(0));
        int i;
        int temp;
        letters[0] = rand ()%25 + 65;
        int check;
        for (i=1; i<7; i++)
            check = 1;
            while (check == 1) {
            temp = rand()%25 + 65;
            if(temp != letters[i-1]) {
            letters[i] = temp;
            check = 0;
            break;
            }
            }
    }
    }
    again, thank you

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    The latest code you posted helped me understand your intentions a lot better.

    First and foremost, the indentation of this program is very poor. It's likely that the indentation was modified when you posted here (and does not match what is actually in your file), but I get the sense that it can still use some improvement.

    When I copy+pasted this, I got a bunch of errors and warnings.

    Tip: Read your compiler documentation to figure out how to crank up the number of warnings it will generate. Treat each warning as an error and fix the code until the warnings disappear. Note that one questionable bit of code might generate several warnings/errors, so fixing one might make many of the warnings/errors go away.
    I also noticed that you were missing a brace at the end of your "score()" function, and some jumbled braces in the "get_letters" function. These gave me errors.

    Tip: Proper indentation, while meaningless to the compiler, is very important to the programmer. It helps you see the structure of program and which code is executed as a result of other code.
    There was once a time where 'C' did not require function declarations (side note: you can omit function declarations altogether and just place the function definitions before "main()" - I personally do not do it this way, but it is perfectly acceptable). Function declarations, and listing the types and names of variables as arguments to those functions, is a way for the compiler to check to see that you've written everything the way you intended to - if there was an error in typing, you'll get a warning/error. This mismatch occurs in your code between the declaration and definition of "get_letters".

    Tip: Write the function declaration above "main()", including return type, function name, and arguments to the function - in the argument list, be sure to give each argument a data type and a variable name. Then copy+paste this below "main()", delete the semicolon, and add the braces that encompass the body of the function.
    Now, before we get to the code, here is some advice for when you start writing code of substantial complexity (relative to your experience):

    - The first step is not to start writing code. The first step is to get a pen and paper (or open a text document) and start to plan the flow of your program. Simple flow charts can be a good visual aid for how you should implement your logic.

    - When you have a ghist of the logic on paper, then you can start writing code.

    - Don't try to write the entire program at once. Work on it one step at a time. Write a few lines of code, compile, check and fix any warnings and errors. Write a little more code, compile, and fix any warnings and errors. It's a lot easier to catch a bug this way.

    - In your example, first you should write the function to get random letters. Test this function by calling it from "main()" and printing out the results. If it's not working right, fix it until it does. Continue doing this for each function, one at a time. Then it's just a matter of stringing the functions together to get a properly working program.

    Okay, now to the code. I will post your exact code, only with better indentation (and I will fix the braces), and will add comments explaining issues with the code.
    Last edited by Matticus; 09-14-2012 at 11:15 AM.

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    // Note that all of my comments appear *above* the applicable line(s) of code!
    
    #include <stdio.h>
    #include <time.h>
    // For "srand()" and "rand()", be sure to #include <stdlib.h>
    
    // Your function arguments should have names, not just data types
    int score(int []);
    void get_letters(int []);
    
    
    int main () {
        // When you're done fixing up the program, get rid of any unused variables.
        int selection, points, i;
        char letter;
        int score = 0;
        int playable_letters[7];
    
        // If "word" is a string (not just a single character), it should be declared
        //   as an array of characters.  It must be large enough to hold the maximum
        //   amount of characters you expect.  If it's to be used as a string, it should
        //   be one greater than the max characters you expect (to hold the null char).
        char word;
    
        printf("Welcome to Scabble Mania!\n");
        printf("Here are your letters!\n");
    
        get_letters(playable_letters);
    
        for (i=0; i<7; i++) {
            printf("%c\n", playable_letters[i]);
        }
        printf("Please enter your word\n");
    
        // You should be scanning for a string (%s) here.
        // Note my comments above your declaration of "word" above.
        scanf("%c", word);
    
        // This line does nothing.  You are counting the score in the variable
        //   "points," so rid of the unused variable "score".
        // Also note that the "score()" function only checks one letter at a time.
        //   I'd recommend making a loop here that cycles through each character,
        //   passing it to the function, and keep a running total of "points" for
        //   for each iteration.  Be sure to initialize "points" to zero beforehand!
        score;
        printf("Your score is %d", points);
    
        return 0;
    }
     
     
    // Consider changing argument to a single character, as mentioned above.
    int score (int[]) {
        int points;
        
        // The value of "letter" should be passed to the function in its argument
        //   list, not declared here within the function.
        char letter;
        switch (letter) {
            case 'A':
            case 'E':
            case 'I':
            case 'L':
            case 'N':
            case 'O':
            case 'R':
            case 'S':
            case 'T':
            case 'U':
                points = 1;
                break;
            case 'D':
            case 'G':
                points = 2;
                break;
            case 'B':
            case 'C':
            case 'M':
            case 'P':
                points = 3;
                break;
            case 'F':
            case 'H':
            case 'V':
            case 'W':
            case 'Y':
                points = 4;
                break;
            case 'K':
                points = 5;
                break;
            case 'J':
            case 'X':
                points = 8;
                break;
            case 'Q':
            case 'Z':
                points = 10;
        } // <--- added closing brace for "switch" statement body
    
        // You should be returning the value of "points" here.
        // Note that this variable "points" is not the same as the
        //   "points" variable in "main()" (they are of different scope).
        return;
    }
    
    
    // This is not how you declared the function up top.
    // I'd recommend changing both the declaration and
    // definition to "void get_letters(int letters[])".
    void get_letters(int letters [7]) {
        // "srand()" should only be called once in the program - you are doing this
        //   correctly, but it's common to place it near the beginning "main()"
        srand(time(0));
        int i;
        int temp;
        // You want "%26" here.
        // Also, '+' has greater precedence than '%', so use parenthesis to ensure
        //   ensure this expression is evalued correctly:
        //   "( rand()%25 ) + 65;
        letters[0] = rand ()%25 + 65;
        int check;
        for (i=1; i<7; i++) { // <--- added opening brace for "for" body
            check = 1;
            while (check == 1) {
                // see notes for "rand()" a few lines up
                temp = rand()%25 + 65;
                if(temp != letters[i-1]) {
                    letters[i] = temp;
                    check = 0;
                } // <--- You want to end the "if" body before the "break"
                break;
            }
        }
    }
    // <--- removed extra brace that was here
    Last edited by Matticus; 09-14-2012 at 11:13 AM.

  10. #10
    Registered User
    Join Date
    Sep 2012
    Posts
    21
    Thank you so much, I will do this and see if I can fix it 100% otherwise.
    I'll post again if I need more help
    Again, thank you so much

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You're very welcome. Happy coding!

  12. #12
    Registered User
    Join Date
    Sep 2012
    Posts
    21
    Ok, I did all I understood. I haven't done C is a long time and it was only an intro to C class.
    Here is what I have. The program is still skipping my request for the user to input a name and is still giving me "letters" that are not actual letters
    Code:
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    
    
    
    
    
    
    int score(int []);
    void get_letters(int letters []);
    
    
    
    
    int main () {
        int selection, points, i;
        char letter;
        int score = 0;
        int playable_letters[7];
        int word [8];
    
    
      printf("Welcome to Scabble Mania!\n");
      printf("Here are your letters!\n");
    
    
      get_letters(playable_letters);
    
    
      for (i=0; i<7; i++) {
      printf("%c\n", playable_letters[i]);
      }
        printf("Please enter your word\n");
        scanf("%s", word);
    
    
        score();
    
    
        printf("Your score is %d", points);
    
    
        return 0;
    }
    
    
    int score (int[]) {
        int points;
        char letter;
    switch (letter) {
    case 'A':
    case 'E':
    case 'I':
    case 'L':
    case 'N':
    case 'O':
    case 'R':
    case 'S':
    case 'T':
    case 'U':
        points = 1;
        break;
    case 'D':
    case 'G':
        points = 2;
        break;
    case 'B':
    case 'C':
    case 'M':
    case 'P':
        points = 3;
        break;
    case 'F':
    case 'H':
    case 'V':
    case 'W':
    case 'Y':
        points = 4;
        break;
    case 'K':
        points = 5;
        break;
    case 'J':
    case 'X':
        points = 8;
        break;
    case 'Q':
    case 'Z':
        points = 10;
    }
    
    
        return points;
    }
    
    
    void get_letters(int letters []) {
        srand(time(0));
        int i;
        int temp;
        letters[0] = (rand()%26) + 65;
        int check;
        for (i=1; i<7; i++) {
            check = 1;
            while (check == 1) {
            temp = (rand()%26) + 65;
            if(temp != letters[i-1]) {
            letters[i] = temp;
            check = 0;
            }
            break;
            }
            }
    }

  13. #13
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    It doesn't even compile (What crappy compiler are you using??):
    Code:
    $ gcc -Wall -Wextra -ggdb3 -o test test.c
    test.c: In function ‘main’:
    test.c:35:5: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int *’ [-Wformat]
    test.c:38:10: error: called object ‘score’ is not a function
    test.c:18:10: warning: unused variable ‘letter’ [-Wunused-variable]
    test.c:17:9: warning: unused variable ‘selection’ [-Wunused-variable]
    test.c: In function ‘score’:
    test.c:48:1: error: parameter name omitted
    Error on line 35:
    Code:
    int word [8];
    ...
    scanf("%s", word);
    You define "word" as an array of 8 ints but on line 35 you try to read a string into it. "word" has to be an array of char if you want to read a string into it. And you should also think about buffer overflows when you correct that line.

    Error on line 38:
    Code:
    int score(int []);
    ...
    int main () {
        int score = 0;
    ...
        score();
    ...
    }
    You declare "score" as a function with file scope, but inside main you declare "score" as an int variable. Thus you are "shadowing" the function, i.e. inside main "score" is a variable and not a function.

    Errors on line 17 and line 18 should be self-explanatory.

    Error on line 48:
    At the beginning you declare "score" as a function with
    Code:
    int score(int []);
    For a declaration avoiding the parameter name is ok. But on line 48 you define the function and now you have to provide the name of the parameter. Otherwise it won't be possible to use it. So you need something like:
    Code:
    int score(int array[])
    Just skimming your code I've noticed some more errors. Hence I suggest reading through a book/tutorial to get the basics of C (which types to choose, how to define/call functions, ...) before continuing with this project

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using new while creating an array of a struct
    By thadis_4 in forum C++ Programming
    Replies: 6
    Last Post: 02-26-2012, 08:01 PM
  2. Creating struct from existing data
    By Dest in forum C Programming
    Replies: 5
    Last Post: 04-13-2010, 06:58 PM
  3. Replies: 8
    Last Post: 01-22-2010, 03:06 AM
  4. creating a struct based on user input
    By rodrigorules in forum C Programming
    Replies: 1
    Last Post: 09-15-2005, 06:16 PM