Jump to content

What is wrong with my code in C?

Guest
Go to solution Solved by Guest,

*found it, forgot brackets and the loop with ab is shit*

Apart from helping others with computer stuff I also do some programming. The program runs fine though but at the end it shows 

**stack smashing detected** 

So there must be something wrong. I don't know where. Hopefully people have time to read through all of it. Note: I am a beginner

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>

void greet (char* a);

void angerdice (char* b, int c);

int dicerolling1 ();


int main ()

{
srand (time(NULL));

int playerno;

printf ("Enter number of players: ");

scanf ("%d", &playerno);

int sel;

char playername[6][100];

int i;


for (i = 1; i <= playerno; i++)
{
	
	printf("\nEnter player %d name: ", i);

	scanf ("%s", playername[i]);
	
	greet (playername [i]);

}


printf("\nPlayer name inputted.\n");




printf("Roll the dice to determine who plays first.\n\n");

int dicecount [6];


for (i = 1; i <= playerno; i++)
{
	int ii;
	printf("%s, press 1 to roll the dice: ", playername [i]);
	
	scanf("%d",&ii);
	
	while (ii != 1)
	{
	angerdice (playername [i], ii);
	scanf("%d",&ii);
	}
	
	printf("%s rolled ",playername [i]);
	dicerolling1 ();
	printf ("%d\n\n", dicerolling1() );
	
	dicecount [i] = dicerolling1();
	
	
	
	}

int maxa;
maxa = dicecount [i];;
i = 1;
int count [6];
int ab = 1;
int maxpl;
while (i <= playerno)
{

	if (dicecount [i]>maxa)
	{
	maxa = dicecount [i];
	maxpl = i;
	}
	else if (dicecount [i] == maxa)
	{
	count[ab] = i;
	ab++;
	}
	else { }

	i++;
}
if (ab == 1)
printf ("Congratulations to %s! You will begin first.\n", playername [maxpl]);
else 
{
printf("The game can not decide who is going first because ");
printf ("2 or more players happen to have the same number on their dice.\n These players will now roll the dice again.");

if (ab == 2){
printf ("%s and " , playername [count[1]]);
printf ("%s" , playername [count[2]]);
}
else if (ab == 3)
{
printf ("%s, %s ", playername [count[1]], playername [count[2]]);
printf ("and %s" , playername [count[3]]);
}
else if (ab == 4)
{
printf("%s, %s, %s ", playername [count[1]], playername [count[2]], playername [count[3]]);
printf("and %s", playername [count[4]]);
}
else if (ab == 5)
{
printf("%s, %s, %s ", playername [count[1]], playername [count[2]], playername [count[3]]);
printf("%s and %s", playername [count[4]], playername [count[5]]);
}
else
{
printf ("Everyone");
}

printf (" will roll again.\n");


}


;




return 0;
} //end of main


void greet (char* a)
{

	int grmessage;
	grmessage = rand () %5;
	if (grmessage == 0)
	printf("Welcome to the game, %s!\n" , a);
	else if (grmessage == 1)
	printf("Hello %s and welcome!\n" , a);
	else if (grmessage == 2)
	printf("%s has joined us! Say hello!\n" , a);
	else if (grmessage == 3)
	printf("Pleasure to have you in this game %s!\n" , a);
	else if (grmessage == 4)
	printf("Why hello there %s!\n" , a);
	else 
	printf("Say hello to %s who has entered the game.\n" , a);

}

void angerdice (char* b, int c)
{

	int a1;
	a1 = rand () %4;
	
	if (a1 == 1)
	printf ("That's not how you roll the dice, %s! Try again, press 1 to roll the dice: " , b);
	else if (a1 == 2)
	printf ("Press 1 to roll the dice, %s, not %d! Try again: ", b,c);
	else if (a1 == 3)
	printf("Wrong number, try again %s: ",b);
	else if (a1 == 4)
	printf("Don't enter a random number like a %d, enter a real number 1 %s: ",c,b);
	else 
	printf("%s, please enter number 1, this should not take much time: ",b);
	
}






int dicerolling1 ()

{

srand (time(NULL));
int roll1;

roll1 = rand () %10;
roll1 = roll1 +2;



return roll1;
}

I implement the program to work with 6 players. So that could be an issue?

Link to comment
Share on other sites

Link to post
Share on other sites

*found it, forgot brackets and the loop with ab is shit*

Link to comment
Share on other sites

Link to post
Share on other sites

I just want to say that your inconsistency in your style format is bugging me.

 

That is all.

[Out-of-date] Want to learn how to make your own custom Windows 10 image?

 

Desktop: AMD R9 3900X | ASUS ROG Strix X570-F | Radeon RX 5700 XT | EVGA GTX 1080 SC | 32GB Trident Z Neo 3600MHz | 1TB 970 EVO | 256GB 840 EVO | 960GB Corsair Force LE | EVGA G2 850W | Phanteks P400S

Laptop: Intel M-5Y10c | Intel HD Graphics | 8GB RAM | 250GB Micron SSD | Asus UX305FA

Server 01: Intel Xeon D 1541 | ASRock Rack D1541D4I-2L2T | 32GB Hynix ECC DDR4 | 4x8TB Western Digital HDDs | 32TB Raw 16TB Usable

Server 02: Intel i7 7700K | Gigabye Z170N Gaming5 | 16GB Trident Z 3200MHz

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, DeadEyePsycho said:

I just want that your inconsistency in your style format is bugging me.

 

That is all.

This code suppose to be the beginning of my program so I did not put effort in indenting shit, now I put it too much effort 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×