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

Pages: 1-

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 5:11

sorry for italian, i guess it doesn't make any difference for you anyway

Name: Anonymous 2011-08-23 5:15

What does it do?
Is it for making pizza or maybe some sort of pasta?

Name: Anonymous 2011-08-23 5:23

It's for doing your mum

Name: Anonymous 2011-08-23 5:26

Use [code][/code] tags, they're there for a reason.

Name: Anonymous 2011-08-23 5:37


#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);
  };
}


>>5 thank you, it's been a while since i last used a bbcode

Name: Anonymous 2011-08-23 5:44

Are you learning from the K-R github thing?

Name: Anonymous 2011-08-23 5:50


player no_player[o];


Really, this works?  What C standard?

Using scanf() with a "%s" is generally a recipe for disaster.  For example, what if the user types more than 30 characters.

Name: Anonymous 2011-08-23 5:51

nope, from an italian site called html.it and cprogramming.com, although i think that eventually i gonna buy a complete book

Name: Anonymous 2011-08-23 5:57

>>8 it actually works. I'm not sure if i understood the question but i use the gcc compiler version 4.3.2.
I also tried to imput a >30 characters value in scanf() and it gave me a segmentation fault as i expected

Name: Anonymous 2011-08-23 6:07

>>10
That's because it happened to be right at the end of allocated memory. Otherwise it'll just overwrite something else and cause weird behavior later on.

Better use %30s.

Name: Anonymous 2011-08-23 6:18

also if i input a string containing a space in scanf(), it splits it in two differt inputs. Why is this?

Name: Anonymous 2011-08-23 6:31

>>11
>%30s
How does that work?
>>12
>""
?

Name: Anonymous 2011-08-23 9:35

>>13
%30s= % parameter 30= bytes s=string, 30s limits the %s to specified bytes

Name: Anonymous 2011-08-23 9:36

Name: Anonymous 2011-08-23 9:40

Italians are the worse programmers.
Signed, an Italian.

>>1
Go to stack overflow or reddit.

Name: n3n7i 2011-08-23 9:58

instead of using int i, and int e
can use int i, and i+1 or i-1

Never seen a loop like that though, neat trick =)

Name: Anonymous 2011-08-23 12:15

>>16
i've been programming for 3 days, get off my ball fucker

>>17
yes eventually i figured it out, at first i thought it would increase i value by 2 units each loop

Name: Anonymous 2011-08-23 12:54

Fixed

#include <stdio.h>

int main(void) {
        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);
        };
       
        return 0;
}

Name: doh 2011-08-23 12:58

>>19
Oops, shouldn't have semi-colons at the end of those for blocks.

Name: Anonymous 2011-08-23 13:11

I AM THE KING OF FARTS, BOW DOWN AND SMELL MY HEAVENLY ODORS

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.

Name: Anonymous 2011-08-23 13:22



#include <stdio.h>

// Good code doens't need comments
int main()
{
    typedef struct {
        char player_name[30];
    } s_player;
   
    int playerAmount;
   
    printf("Enter the number of players:");
    scanf("%d", &playerAmount);

    s_player player[playerAmount];
   
    if (playerAmount == 1)
        printf("You have chosen the single player mode\n");
    else if ( playerAmount >= 1)
        printf("You have chosen the mode to %d players\n", playerAmount);
    else
        printf("enter the correct value\n");

    for(int i = 0; i < playerAmount; i++)
    {
        printf("Enter the name of player %d:", i);
        scanf("%s", &player[i].player_name);
    }
   
    for(int i = 0; i < playerAmount; i++)
        printf("player %d: %s\n",i + 1 , player[i].player_name);
   
    return 0;
}

Name: Anonymous 2011-08-23 14:44

>>22
Thanks, that's helpful to know

Name: Anonymous 2011-08-23 16:15


import player_values.py


was it really that hard?

Name: Anonymous 2011-08-24 6:37

>>22

You can't do that for-loop variable declarement in C.

Name: Anonymous 2011-08-24 7:27

>>26
This isn't 1980 anymore, you know. Every compiler worth its name supports C99 nowadays.

Name: Anonymous 2011-08-24 8:16

>>22
Is 0 as return code for main in the standard?

Name: Anonymous 2011-08-24 10:32

Mammia mia figlio di puttana il code e` brutto porco dio

Name: Anonymous 2011-08-24 12:02

>>28
Yes. Unless you want to report that your program has failed in some way, in which case you would return some other value by convention.

Name: Anonymous 2011-08-24 12:07

>>28
You can use the EXIT_SUCCESS macro, if you prefer.  But it will always evaluate to 0.

Name: Anonymous 2011-08-24 16:07

>>30,31
No it doesn't you stupid piece of shit, that is OS specific.
Holy shit how sub tier can you get? You use an OS and it expands to 0 once and then you expect it to always expand to zero because it did so on your platform, try doing something that is more at your level like working the cash register at targets you mental midget.

Name: Anonymous 2011-08-24 16:10

>>32
cash register at targets you mental midget
Bend over.

Name: Anonymous 2011-08-24 16:12

The use of EXIT_SUCCESS and EXIT_FAILURE is slightly more portable (to non-Unix environments) than the use of 0 and some nonzero value like 1 or -1.  In particular, VMS uses a different convention.

Name: Anonymous 2011-08-24 16:13

>>1
you forgot your {code} tags, that's why it doesn't work

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