Thread: My Projects Thread

Page 1 of 2 12 LastLast
Results 1 to 10 of 17
  1. #1 My Projects Thread 
    Registered Member

    Join Date
    May 2012
    Posts
    475
    Thanks given
    93
    Thanks received
    74
    Discord
    View profile
    Rep Power
    634
    About this thread:
    I've decided to start a single thread to track progress on different projects I am working on (whether it be school related or not). I'd like constructive criticism from the community, while keeping in mind that I have had almost no real experience with programming before this last semester of college. If you can give me an example of how I can do something in a better way then, by all means, share. Also, if you have any suggestions for generally improving my project (naming, conventions, whatever), please post them. All of these projects will be written in C.

    I expect a detailed reason or argument on your point, advice, whatever (if applicable/possible, of course). Links to reputable resources are very good.


    Current project:
    Character creation program
    I've decided to create a very basic character creation and saving system. I'll use structs for the players and an item system later. I'll also use enums for a rights system later. I've written a small objective list...
    Spoiler for PlayerSystem.txt:

    Player system. (branch?)

    Create structs for player entity, inventory system. Create rights system that
    uses enumerated data type and implement it into the player entity struct.

    Use command line argument to search for existing character. If character file
    exists, load .txt. If not, print warning and exit program.

    Should create an item system that contains bonuses to skills. Maybe create a
    base item struct and initialize an array of item type to store all items and
    their bonuses in.

    Player structure contains:
    Hitpoints, attack, strength, defence, inventory, equipped items.


    I've begun work on it and in about 10 minutes I've come up with this:
    Spoiler for playerEntity.h:

    Code:
    /*
     * playerEntity.h declares the player entity struct.
     *
     *  Created on: Feb 29, 2016
     *      Author: Nathan Harder
     */
    
    #ifndef PLAYERENTITY_H_
    #define PLAYERENTITY_H_
    
    typedef struct Player_STRUCT {
    	char username[32];
    	int hitpoints;
    	int attack;
    	int strength;
    	int defence;
    }Player;
    
    Player initPlayerChar(Player character);
    
    void loadPlayerChar();
    
    #endif


    Spoiler for playerEntity.c:

    Code:
    /*
     * playerEntity.c defines functions declared in playerEntity.h.
     *
     *  Created on: Feb 29, 2016
     *      Author: Nathan Harder
     */
    
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    #include "playerEntity.h"
    
    /**
     * @initPlayerChar initializes a new player's skills.
     */
    
    Player initPlayerChar(Player character) {
    	character.attack = 1;
    	character.strength = 1;
    	character.defence = 1;
    	character.hitpoints = 10;
    
    	return character;
    }
    
    /**
     * Search for player file and load/return the Player.
     */
    
    void loadPlayerChar() {
    	const int CHAR_LIMIT = 17; //17 to include the null terminator
    	char char_name[CHAR_LIMIT];
    
    	printf("Please enter your character's name:\n");
    
    	do {
    		LOOP:
    		fgets(char_name, CHAR_LIMIT, stdin);
    	} while(char_name[0] == '\n');
    
    	for(int i = 0; i < strlen(char_name) - 1; ++i) {
    		if(isalpha(char_name[i]) == 0) {
    			printf("That username is not acceptable, please try again.\n");
    			goto LOOP;
    		}
    	}
    
    	printf("%s\n", char_name);
    
    	return;
    }


    Spoiler for main.c:

    Code:
    /*
     * This project creates a simple player entity system with a simple item
     * system, rights system, and character saving. This project is for
     * learning experience.
     *
     *  Created on: Feb 29, 2016
     *      Author: Nathan Harder
     */
    
    #include <stdio.h>
    #include "playerEntity.h"
    
    void startup();
    
    int main(int argc, char* argv[]) {
    	startup();
    
    	return 0;
    }
    
    void startup() {
    	char user_input;
    
    	printf("Welcome.\n");
    	printf("This is a basic character-creation program.\n");
    	printf("Have you created a character? [y/n]\n");
    
    	LOOP:
    	user_input = getchar();
    
    	switch(user_input) {
    		case 'y':
    			loadPlayerChar();
    			break;
    		case 'n':
    			//TODO: prompt for name and create file
    			break;
    		default:
    			goto LOOP;
    		}
    }


    Please understand that this is just an exercise to help me grasp the use of structs, file i/o, enums, and headers in C.
    [Only registered and activated users can see links. ]
    Damn Deri
    Reply With Quote  
     

  2. Thankful user:


  3. #2  
    Registered Member Arth's Avatar
    Join Date
    May 2011
    Posts
    753
    Thanks given
    29
    Thanks received
    39
    Rep Power
    9
    Good job! This is clear and very well written. Just a few questions. Why are you using the following libraries?
    #include <stdbool.h>
    #include <ctype.h>
    I have't checked the documentation but I'm assuming the 'put' function defined there? If so, since you are already using standard input output wouldn't you just use printf for regular output statements?
    Spoiler for Dont Click Me!:
    Reply With Quote  
     

  4. Thankful user:


  5. #3  
    Registered Member

    Join Date
    May 2012
    Posts
    475
    Thanks given
    93
    Thanks received
    74
    Discord
    View profile
    Rep Power
    634
    Quote Originally Posted by Arth View Post
    Good job! This is clear and very well written. Just a few questions. Why are you using the following libraries?

    I have't checked the documentation but I'm assuming the 'put' function defined there? If so, since you are already using standard input output wouldn't you just use printf for regular output statements?
    I forgot to remove those libraries, I was previously using them to make sure that the user was using alphabetic characters only.
    [Only registered and activated users can see links. ]
    Damn Deri
    Reply With Quote  
     

  6. #4  
    Registered Member Arth's Avatar
    Join Date
    May 2011
    Posts
    753
    Thanks given
    29
    Thanks received
    39
    Rep Power
    9
    Quote Originally Posted by Hart View Post
    I forgot to remove those libraries, I was previously using them to make sure that the user was using alphabetic characters only.
    Do you follow any specific standard? Also what flags do you use when compiling?
    i use wall and c99, sometimes pedantic.
    Spoiler for Dont Click Me!:
    Reply With Quote  
     

  7. #5  
    Registered Member

    Join Date
    May 2012
    Posts
    475
    Thanks given
    93
    Thanks received
    74
    Discord
    View profile
    Rep Power
    634
    Quote Originally Posted by Arth View Post
    Do you follow any specific standard? Also what flags do you use when compiling?
    i use wall and c99, sometimes pedantic.
    Whatever the default is that comes with Ubuntu and GCC.
    [Only registered and activated users can see links. ]
    Damn Deri
    Reply With Quote  
     

  8. #6  
    Registered Member

    Join Date
    May 2012
    Posts
    475
    Thanks given
    93
    Thanks received
    74
    Discord
    View profile
    Rep Power
    634
    Sorry for the double post.

    What would be the difference between using:
    Code:
    char* userInput[256];
    and not using a pointer? I mean I understand the difference, but I see a lot of example programs from others using this pointer instead of a regular variable declaration.
    [Only registered and activated users can see links. ]
    Damn Deri
    Reply With Quote  
     

  9. #7  
    Registered Member

    Join Date
    May 2012
    Posts
    475
    Thanks given
    93
    Thanks received
    74
    Discord
    View profile
    Rep Power
    634
    Updated the OP with a Java version of reverseString. Not sure how to handle the possibility of a buffer overflow in Java at this time.

    I'm about ready to move on to a new project. I'm thinking a pig-latin converter would be a good project. Thoughts?

    triplePost++;
    [Only registered and activated users can see links. ]
    Damn Deri
    Reply With Quote  
     

  10. #8  
    Registered Member Arth's Avatar
    Join Date
    May 2011
    Posts
    753
    Thanks given
    29
    Thanks received
    39
    Rep Power
    9
    Quote Originally Posted by Hart View Post
    Sorry for the double post.

    What would be the difference between using:
    Code:
    char* userInput[256];
    and not using a pointer? I mean I understand the difference, but I see a lot of example programs from others using this pointer instead of a regular variable declaration.
    Code:
    char* userInput[256];
    i kind of like to think of it as 256 String variables where as,
    Code:
    char userInput[256];
    A string with 256 characters
    Spoiler for Dont Click Me!:
    Reply With Quote  
     

  11. #9  
    Registered Member

    Join Date
    May 2012
    Posts
    475
    Thanks given
    93
    Thanks received
    74
    Discord
    View profile
    Rep Power
    634
    Updated OP with new project. This one should prove a bit more of a challenge than any other.

    I'd like to ask for input on any usage of pointers/pass by reference in my program if I should do something different and use pointers or not. Any other constructive criticism is welcome, too.
    [Only registered and activated users can see links. ]
    Damn Deri
    Reply With Quote  
     

  12. #10  
    Registered Member Arth's Avatar
    Join Date
    May 2011
    Posts
    753
    Thanks given
    29
    Thanks received
    39
    Rep Power
    9
    Code:
    /*
     * This project creates a simple player entity system with a simple item
     * system, rights system, and character saving. This project is for
     * learning experience.
     *
     *  Created on: Feb 29, 2016
     */
    
    #include <stdio.h>
    #include "playerEntity.h"
    
    void startup();
    char getUserInput();
    
    int main(int argc, char* argv[]) {
    	startup();
    
    	return 1;
    }
    
    void startup() {
    	printf("Welcome.\n");
    	printf("This is a basic character-creation program.\n");
    	printf("Have you created a character? [y/n]\n");
    
    	if(getUserInput() == 'y') {
    		//TODO: prompt for character name and load file
    	} else if(getUserInput() == 'n') {
    		//TODO: prompt for character name and creation
    	} else {
    		printf("That input is not accepted.\n");
    		getUserInput();
    	}
    }
    
    char getUserInput() {
    	char userKey = '\0';
    
    	scanf("%c", &userKey);
    
    	return userKey;
    }
    Good job so far! I really have doubts when using scanf because it only reads up until the first space character. Although in a situation like this, its fine. When prompting for data be sure to use something more secure like fgets.

    Also

    Code:
    else {
    		printf("That input is not accepted.\n");
    		getUserInput();
    	}
    You are calling the getUserInput function although the program is terminating after that, you are doing nothing with the value that is returned.

    It's the least of my concerns but usually i have my function
    Code:
    initPlayerChar
    of type struct player.

    Also, its great that you're learning pointers as well as i/o. Also take a look at memory allocation. Learn malloc and a little about what happens behind the scenes in terms of stack and heap!

    Careful when you use Pass by reference and pass by address.

    Pass by reference is when the compiler makes a copy of the variable argument in the parameter. The actual value never changes.
    Pass by address is actually sending the variable address to the function. The actual value does change.

    Seems like you're on the right path with documentation, and style looks great. I personally like to follow c99 standard. If you are using the command like GCC to compile then
    the it goes as follows.
    Code:
    gcc -Wall -stc=c99 main.c playerEntity.c -o Program
    Nevertheless great job! Will be keeping up with this thread to see your progress.
    Spoiler for Dont Click Me!:
    Reply With Quote  
     

Page 1 of 2 12 LastLast

Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. For my project thread(Vote Seriously please)
    By Honey badger in forum Spam
    Replies: 9
    Last Post: 10-03-2011, 02:43 AM
  2. Locked My Project Thread
    By Arkadian in forum Complaints
    Replies: 1
    Last Post: 01-05-2010, 09:15 AM
  3. Why can I not edit my project thread??
    By Harry in forum Forum Related Help
    Replies: 3
    Last Post: 11-08-2009, 09:44 PM
  4. My project thread?
    By 'Mystic Flow in forum Forum Related Help
    Replies: 0
    Last Post: 07-15-2009, 01:25 AM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •