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:
Anonymous2011-08-23 5:11
sorry for italian, i guess it doesn't make any difference for you anyway
Name:
Anonymous2011-08-23 5:15
What does it do?
Is it for making pizza or maybe some sort of pasta?
Use [code][/code] tags, they're there for a reason.
Name:
Anonymous2011-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
Using scanf() with a "%s" is generally a recipe for disaster. For example, what if the user types more than 30 characters.
Name:
Anonymous2011-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:
Anonymous2011-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:
Anonymous2011-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:
Anonymous2011-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?
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:
Anonymous2011-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:
Anonymous2011-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);
};
>>19
Oops, shouldn't have semi-colons at the end of those for blocks.
Name:
Anonymous2011-08-23 13:11
I AM THE KING OF FARTS, BOW DOWN AND SMELL MY HEAVENLY ODORS
Name:
Anonymous2011-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);
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:
Anonymous2011-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);
>>28
You can use the EXIT_SUCCESS macro, if you prefer. But it will always evaluate to 0.
Name:
Anonymous2011-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:
Anonymous2011-08-24 16:10
>>32 cash register at targets you mental midget
Bend over.
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.