Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

My new c code

Name: Anonymous 2011-08-23 5:08

Hi guys, i just started learning c, so i'm pretty much a noob. I was wondering i my code (which actually works) is messed, and things could have been done better
here it is:

#include <stdio.h>

main()
{
typedef struct giocatore {char player_name[30];} player;
int o;
printf("Inserire il numero di giocatori:");
scanf("%d", &o);
player no_player[o];

if (o == 1)
    printf("Avete scelto la modalità giocatore singolo\n");
else if ( o >= 1)
    printf("Avete scelto la modalità a %d giocatori\n", o);
else
    printf("inserire un valore corretto\n");

int i, e = 0;

for(i = 1; i <= o; i++, e++){
  printf("Inserire il nome del giocatore %d:", i);
  scanf("%s", &no_player[e].player_name);
  };
for(i = 0, e = 1; i < o; i++, e++){
  printf("Giocatore %d: %s\n",e , no_player[i].player_name);
  };
}

Name: Anonymous 2011-08-23 13:19



#include <stdio.h>

int main()
{
    // restructured your struct to make it more readable
    typedef struct {
        char player_name[30];
    // added "s_" to imply this type is a structure
    } s_player;
   
    // renamed "o" to this so you can read the code and know what this var counts
    int playerAmount;
   
    printf("Enter the number of players:");
    scanf("%d", &playerAmount);
   
    // COMMENTS, COMMENTS EVERYWHERE!
    s_player player[playerAmount];
   
    if (playerAmount == 1)
        printf("You have chosen the single player mode\n"); // translated
    else if ( playerAmount >= 1)
        printf("You have chosen the mode to %d players\n", playerAmount);
    else
        printf("enter the correct value\n");
   
    // I prefer to use "i" as a local int. This means THIS "i" can't be used outside THIS loop
    for(int i = 0; i < playerAmount; i++)
    {
        printf("Enter the name of player %d:", i);
        scanf("%s", &player[i].player_name);
    } // no semi colon needed after closing brackets
   
    // only one line of code for this loop, so I omitted the brackets.
    // also removed "e", because it was unnecessary and replaced it with "i+1"
    for(int i = 0; i < playerAmount; i++)
        printf("player %d: %s\n",i + 1 , player[i].player_name);
   
    // having the same loop like this will only screw up readability, but not the code
    // for(int i = 0; i < playerAmount; i++) printf("player %d: %s\n",i , player[i].player_name);
   
    return 0; // standard compliant returning of 0
} // end of main()


Your code was pretty crappy, but hey, so was mine when I first learned C.

Protip: Get a decent C book for beginners (online tutorials tend to miss a *lot* of needed info), like "C Programming: A Modern Approach" or "C By Example". If you're a poorfag, download them as .pdf from any kind of warez/torrent/whatev site.

After you've learned the basics, you'd also want to get the K&R book: "The C Programming Language" as a reference book.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List