Why I get very huge output in printf on C language? - c

This is my code, I want to calculate distance between robot and monster, but the output "horizontal and vertikal" is false
#include <stdio.h>
void findPos(char *dir, int a, int b)
{
int up = 0, down = 0;
int left = 0, right = 0;
int i,x,y;
for (i = 0; dir[i] != '\0' ; i++) {
//Counts each direction
if (dir[i] == 'U' || dir[i] == 'u')
up++;
else if (dir[i] == 'D' || dir[i] == 'd')
down++;
else if (dir[i] == 'L' || dir[i] == 'l')
left++;
else if (dir[i] == 'R' || dir[i] == 'r')
right++;
//In case of illegal character in the string
else
{
printf("Position Unable to Find, Enter Correct Direction.");
break;
}
}
//Final position of robot
x = right - left;
y = up - down;
printf("Final Position of the Robot: (");
printf("%d", x);
printf(",%d", y);
print(")");
printf("\nposition between robot and monster");
printf("\nhorizontal: %d", a-x);
printf("\nvertikal: %d", b-y);
}
int main()
{
char *dir;
int a,b,t;
/* Intializes random number generator */
srand((unsigned) time(&t));
/* Print 2 random numbers from 0 to 100 */
a = rand() % 100;
b = rand() % 100;
printf("\nCoordinate of monster: ");
printf("(%d,", a);
printf("%d)", b);
//Input the direction string
printf("\nEnter the Direction String: ");
scanf("%s", &dir);
//Function call to calculate position
findPos(&dir, a,b);
return 0;
}
and this is the output
Coordinate of monster: (5,47)
Enter the Direction String: UURRRRRLLL
Final Position of the Robot: (2,2)
position between robot and monster
horizontal: 19530
vertikal: 1280463440

it seems your program is exhibiting undefined behavior, you are actually lucky its not crashing, dir is never allocated . you might want to calloc it before using , also remove the & in scanf, infact move to fgets unless inclined to use scanf
printf("\nEnter the Direction String: ");
scanf("%s", dir);

Related

for loop to print array does not execute properly for output (C)

I am trying to print the final results of the calculations using my program and put it into a clean organized table for the output, however, when the table is printed it doesn't print properly and I am very lost and my professor isn't really much help. Any help is greatly appreciated thank you!
The table is meant to look like this:
Example Table
#include <stdio.h>
void main(void) {
float sale[10], pct[10], commission[10];
char emp_name[10][20], company[15];
float sum = 0;
char c;
int i, x, sales_persons, num_of_sales;
printf("Enter your company name: ");
scanf("%s", company);
printf("\nWelcome to the %s Sales Analysis\n", company);
do {
printf("\nEnter how many sales persons from 1 to 10: ");
scanf("%i", &sales_persons);
while ((c = getchar() != '\n') && c != EOF)
; /* clear input buffer */
if (sales_persons < 0 || sales_persons > 10)
printf("Number of sales persons must be 1-10...\n\n");
} while (sales_persons < 0 || sales_persons > 10);
for (x = 0; x < sales_persons; x++) {
printf("\nEnter the name of sales person #%i: ", x + 1);
scanf("%19[^\n]", emp_name[x]);
while ((c = getchar() != '\n') && c != EOF)
;
do {
printf("Enter the number of sales for %s (1 - 20): ", emp_name[x]);
scanf("%i", &num_of_sales);
while ((c = getchar() != '\n') && c != EOF)
; /* clear input buffer */
if (num_of_sales < 0 || num_of_sales > 20)
printf("Number of sales must be 1-20...\n\n");
} while (num_of_sales < 0 || num_of_sales > 20);
do {
printf("Enter the commission percentage for %s (1-50): ", emp_name[x]);
scanf("%f", &pct[x]);
while ((c = getchar() != '\n') && c != EOF)
; /* clear input buffer */
if (pct[x] < 0 || pct[x] > 50) printf("Percent must be 0-50...\n\n");
} while (pct[x] < 0 || pct[x] > 50);
printf("\n");
for (x = 0; x < num_of_sales; x++) {
do {
printf(" Sale #%i for %s: ", x + 1, emp_name[x]);
scanf("%f", &sale[x]);
while ((c = getchar() != '\n') && c != EOF)
; /* clear input buffer */
if (sale[x] < 0) printf("All sales must be greater than 0...\n\n");
} while (sale[x] < 0);
}
sum = sum + sale[x];
commission[x] = pct[x] * sum / 100;
}
printf("\n\n\n *** %s Sales Commission Report ***", company);
printf("\n\n\nName \t\t Sales \t\t Commission");
printf("\n---- \t\t ----- \t\t ----------");
for (x = 0; x < sales_persons; x++) {
printf("\n%s \t\t %.2f\n \t\t%.2f", emp_name[x], sum, commission[x]);
}
getchar();
}
You are using the same x variable to control nested loops
for (x = 0; x < sales_persons; x++) {
/* ... */
for (x = 0; x < num_of_sales; x++) {
meaning the line
printf(" Sale #%i for %s: ", x + 1, emp_name[x]);
will access incorrect elements of emp_name, and the value of x will be num_of_sales + 1 after every outer loop.
Similarly,
sum = sum + sale[x];
commission[x] = pct[x] * sum / 100;
will be working with the wrong value of x after the inner loop completes.
Perhaps the unused variable i should be substituted for x in the inner loop?
Additionally, this format string has a stray newline in it, making the output of the program confusing:
printf("\n%s \t\t %.2f\n \t\t%.2f", emp_name[x], sum, commission[x]);
/* ^^^ excess newline character */
Other problems:
void main(void) should be int main(void).
char c; should be int c;. See here.
scanf("%s", company); is inviting undefined behaviour by not limiting the size of the input string ("%14s").
Newline characters should ideally be written at the end of lines.
A lot of repeated code should be placed in separate functions (clearing the input buffer, for example).

C program compiles with no errors. It displays garbage. I think first while loop may be to blame

This program will create a menu that can
take in 2 chars as points from a char array
compute the distance between the points
display the line in equation y = mx + b
compute the closest point to the line
exit [program will only terminate when e is selected]
There is a logical error somewhere causing none of the correct output to display. I have been banging my head against my keyboard and I'm desperately hoping I made an idiotic simple mistake. The menu displays too many times so i suspect the while loop is in error. I am not a progammer, but my math should be solid. Please help me fix my logic.
int main()
{
char PtLbl[9] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I' }; /*char and xcoordinates and y coordinates arrays hardcoded*/
double Xcoord[9] = { 50.2, 45.7, 76.7, 12, -25, 23, 34.6 };
double Ycoord[9] = { 75.8, 48, 99.1, 14, 48, -36, -123 };
double temp, m = 0, b = 0, dist;
double small = 99999999999.0;
char choice = 'z';
char pt1 = 'z', pt2 = 'z';
int in1 = -1, in2 = -1, i, finalin;
int k = 0, l = 1;
while (1)
{
printf("a) Enter Points:\nb) Compute the distance\nc) Display the line equation\nd) Find the closest point to the line equation in c\ne) Exit\n");
scanf("%c", &choice); /*Displays the menu options, takes in a menu choice, and converts it to lowercase if upper*/
choice = tolower(choice);
if (choice == 'a') /*choice a selected*/
{
k = 1;
while (in1 == -1 && in2 == -1)
{
printf("Please enter 2 points separated by one space\n");
scanf("%c %c", &pt1, &pt2);
pt1 = toupper(pt1);
pt2 = toupper(pt2);
for (i = 0; i < 9; i++) /*stores the index of the char in PtLbl array as index1 and index2*/
{
if (pt1 == PtLbl[i])
{
in1 = i;
}
if (pt2 == PtLbl[i])
{
in2 = i;
}
}
}
}
else if ((choice == 'b' || choice == 'c' || choice == 'd') && k == 0) /*since points need to be selected first, this checks if a has been selected*/
{
printf("Please enter 2 points before making a further selection\n");
continue;
}
else if (choice == 'b') /*calulates the distance between the points with the corresponding indexes in the x-coordinates and y-coordinates arrays*/
{
temp = sqrt(pow(Xcoord[in2] - Xcoord[in1], 2) + pow(Ycoord[in2] - Ycoord[in1], 2));
printf("The distance between points %c and %c is %f\n", PtLbl[in1], PtLbl[in2], temp);
}
else if (choice == 'c') /*calculates slope (m) and y intercept (b) to put in a line equations and display*/
{
m = (Ycoord[in2] - Ycoord[in1]) / (Xcoord[in2] - Xcoord[in1]);
b = (Ycoord[in1] - m * Xcoord[in1]);
printf("The equation of the line passing through %c and %c is y = %f x+ %f\n", PtLbl[in1], PtLbl[in2], m, b);
}
else if (choice == 'd') /*calculates the from the line to nearest point*/
{
if (m == 0 && b == 0) /*only calculates (m) and (b) if c hasnt been selected yet*/
{
m = (Ycoord[in2] - Ycoord[in1]) / (Xcoord[in2] - Xcoord[in1]);
b = (Ycoord[in1] - m * Xcoord[in1]);
}
for (i = 0; i < 9; i++)
{
if (i == in1 || i == in2)
{
continue;
}
dist = abs(m*Xcoord[i] + (-1)*Ycoord[i] + b) / sqrt((m*m) + 1); /*smallest distance is recorded and index is stored*/
if (dist < small)
{
small = dist;
finalin = i;
}
}
printf("The closest point to the line y = %f x + %f is %c at a distance of %f\n", m, b, PtLbl[finalin], small); /*displays data from d*/
}
else if (choice == 'e') /*program only exits when choice e is selected*/
{
exit(0);
}
else
{
printf("Please enter a valid selection"); /*prompts user to try again if input invalid*/
continue;
}
}
}
Obvious error -- you're not ignoring whitespace (like newlines) in your scanf calls, so you're code will get confused and thrown off by them. You want to put spaces before the %c directive in your format strings to ignore whitespace:
scanf(" %c", &choice);
and
scanf(" %c %c", &pt1, &pt2);

About Tic-Toc-Toe Game in C language

I wrote a full code for this game using do and while in C language .. but I stop in point which I don't know how I can type a code to decide if the game is drawn or not..
Thanks
This is my code
#include <stdio.h>
#include <stdlib.h>
char matrix[3][3];
char check(void);
void init_matrix(void);
void get_player1_move(void);
void get_player2_move(void);
void disp_matrix(void);
int main(void)
{
char done,choise;
printf("Welcome to the tic-tac-toe game!!!\n\n");
printf("Rule for playing the game is:\n\n");
printf("Each player must put the value of raw and column like: 1 2 to put his symbol in\nthe tic-tac-toe board.\n\n");
printf("The tic-tac-toe board looks like as follows:\n\n");
init_matrix();
disp_matrix();
printf("Are you ready to start the game? ");
scanf(" %c",&choise);
if (choise == 'y'){
do {
get_player1_move();
done = check(); /* if winner or not */
if(done!= ' ') break; /* winner!*/
disp_matrix();
get_player2_move();
disp_matrix();
done = check(); /* if winner or not */
if(done!= ' ') break; /* winner!*/
} while(done== ' ');
if(done=='X') printf("Player 1 won the game!!!!!\n");
else printf("Player 2 won the game!!!!!\n");
}
else {
printf("\n\nThank you!!!\n\n");
printf("We hope you will play the game anther time....");
}
return 0;
}
/****************************************************/
void init_matrix(void)
{
int i, j;
for(i=0; i<3; i++)
for(j=0; j<3; j++) matrix[i][j] = ' ';
}
/****************************************************/
void get_player1_move(void)
{
int x, y;
printf("Enter row and column input for player 1: ");
scanf("%d%*c%d", &x, &y);
x--; y--;
if(matrix[x][y]!= ' '){
printf("You can not choose this row and clumn!! Try again\n");
get_player1_move();
}
else matrix[x][y] = '1';
}
/****************************************************/
void get_player2_move(void)
{
int x, y;
printf("Enter row and column input for player 2: ");
scanf("%d%*c%d", &x, &y);
x--; y--;
if(matrix[x][y]!= ' '){
printf("You can not choose this row and clumn!! Try again\n");
get_player2_move();
}
else matrix[x][y] = '2';
}
/****************************************************/
void disp_matrix(void)
{
int t;
for(t=0; t<3; t++) {
printf(" : : ");
printf("\n %c : %c : %c",matrix[t][0],matrix[t][1], matrix [t][2]);
if(t!=2) printf("\n ----:----:----\n");
}
printf("\n\n");
}
/****************************************************/
char check(void)
{
int i;
for(i=0; i<3; i++) /* check rows */
if(matrix[i][0]==matrix[i][1] && matrix[i][0]==matrix[i][2])
return matrix[i][0];
for(i=0; i<3; i++) /* check columns */
if(matrix[0][i]==matrix[1][i] && matrix[0][i]==matrix[2][i])
return matrix[0][i];
/* test diagonals */
if(matrix[0][0]==matrix[1][1] && matrix[1][1]==matrix[2][2])
return matrix[0][0];
if(matrix[0][2]==matrix[1][1] && matrix[1][1]==matrix[2][0])
return matrix[0][2];
return ' ';
}
if(done=='X') should be if(done == '1') because matrix contains 1 and 2, not X and O.
To tell if the game is a draw, keep a count of the number of moves, and break out of the loop when it reaches 9. You only have to check this after player 1 moves, because player 2 always moves on even numbers.
There's no need to use do{...} while (done == ' ') because the code always breaks out of the loop when done != ' '. So just use while(1) to make an infinite loop.
int count = 0;
while (1) {
get_player1_move();
done = check(); /* if winner or not */
disp_matrix();
if(done!= ' ' || ++count == 9) break; /* winner or draw */
get_player2_move();
disp_matrix();
done = check(); /* if winner or not */
if(done!= ' ') break; /* winner!*/
++count;
}
if (done == ' ') {
printf("It's a draw!\n");
} else {
printf("Player %c won the game!!\n", done);
}
Set a boolean variable "draw" as false and if no player wins and all 9 moves are done and no free block is available set the "draw" as true. And calculate the result based on "draw" that either the game is a draw or not.

how to store values elements of datasets into different arrays in c

I have a convoluted plan for a program.
I would like to have datasets of three values be stored in different arrays, i.e the first value be stored in the first array, the second value in the second array and the third in the third array.
For example:
"How often would you like to repeat the program"
2
"Enter the first value:"
1
"Enter the second value:"
2
"Enter the third value:"
3
"Enter the first value:"
4
"Enter the second value:"
5
"Enter the third value:"
6
The output should be something like this
a_arr = [1 4]
b_arr = [2 5]
c_arr = [3 6]
I have come up with this code but I can't seem to get it to work.
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 255
//validation function
int a_arr[MAX_SIZE]; // Declare an array of MAX_SIZE
int b_arr[MAX_SIZE]; // Declare an array of MAX_SIZE
int c_arr[MAX_SIZE]; // Declare an array of MAX_SIZE
int i;
int checkInput0(void);
float checkInput1(void);
float checkInput2(void);
float checkInput3(void);
a_arr[] = checkInput1;
b_arr[] = checkInput2;
c_arr[] = checkInput3;
int main()
{
int repeats = 0, counter = 0;
//Amount of triangles
repeats = checkInput0();
// create arrays for all values
do {
i = counter;
for(i=0; i<repeats; i++)
{
scanf("%f", &a_arr[i]);
}
for(i=0; i<repeats; i++)
{
scanf("%f", &b_arr[i]);
}
for(i=0; i<repeats; i++)
{
scanf("%f", &c_arr[i]);
}
counter++;
}while(counter < repeats);
do {
printf("%f\n",a_arr );
printf("%f\n",b_arr );
printf("%f\n",c_arr );
counter++;
}while(counter < repeats);
return 0;
}
// Validate Value of a
float checkInput1(void){
float option1,check1;
char c;
do{
printf("Enter the first side of the triangle");
if(scanf("%f%c",&option1,&c) == 0 || c != '\n') {
while((check1 = getchar()) != 0 && check1 != '\n' && check1 != EOF);
printf("\t[ERR] Invalid number for the triplet.\n");
}else {
break;
}
}while(1);
// printf("returning the value of option, which is %f", option);
return option1;
}
// Validate Value of b
float checkInput2(void){
float option2,check2;
char c;
do{
printf("Enter the second side of the triangle");
if(scanf("%f%c",&option2,&c) == 0 || c != '\n'){
while((check2 = getchar()) != 0 && check2 != '\n' && check2 != EOF);
printf("\t[ERR] Invalid number for the triplet.\n");
}else{
break;
}
}while(1);
//printf("returning the value of option, which is %f", option2);
return option2;
}
// Validate Value of c
float checkInput3(void){
float option3,check3;
char c;
do{
printf("Enter the third side of the triangle");
if(scanf("%f%c",&option3,&c) == 0 || c != '\n'){
while((check3 = getchar()) != 0 && check3 != '\n' && check3 != EOF);
printf("\t[ERR] Invalid number for the triplet.\n");
}else{
break;
}
}while(1);
// printf("returning the value of option, which is %f", option);
return option3;
}
Note: the functions are there for validation such that the input is a number.
All help is greatly appreciated
I think you may have complicated this.
You can do something like this.
printf("How many...");
scanf("%d", &num); //num is the user input
int i;
for(i=0; i<num; i++)
{
printf("enter first number");
a_arr[i] = checkInput1();
printf("enter first number");
b_arr[i] = checkInput2();
printf("enter first number");
c_arr[i] = checkInput3();
}
The last 3 declarations before main have no sense.
Answering the question, the pseudocode is simple. You have 3 arrays, so when the iterator of scans reach i = 3n-1, being n= 0,1,2,3,4..., then the index is increased.
E.g., scanf 0, 1 and 2 are all saved in index 0 of the respective array (2 = 3n-1, n being 1); scanf 3,4 and 5 are all saved in index 1 of the respective array (5 = 3n-1, n being 2) and so on...
I think you are wasting lot of memory doing this statically, simple malloc can save lot of memory and time(speed of execution i meant)
printf("How many...");
scanf("%d", &num); //num is the user input
int * a_arr = (int *)malloc( num * sizeof(int));
int * b_arr = (int *)malloc( num * sizeof(int));
int * c_arr = (int *)malloc( num * sizeof(int));
int count;
for(count=0; count<num; count++)
{
printf("enter first number");
scanf("%d",a_arr[count]);
printf("enter first number");
scanf("%d",b_arr[count]);
printf("enter first number");
scanf("%d",c_arr[count]);
}
//print or whatever you wanna do processing on it

Program written in C, loops indefinately or crashes after a while

this is both my first time asking a question and also one of my first times writting such a big programm. As you might guess im new at programming.
Alright the source code:
#include <stdio.h>
typedef struct{
int **a;
int size;
}_board;
typedef _board* board;
typedef struct{
int row,col;
}position;
int main () {
int i, j, turn=1, victory = 0, num=0;
_board b;
char P1symbol, P2symbol, mark, boardarray[b.size][b.size];
position p;
printf("WELCOME TO THE GAME OF TIC TAC TOE!\n");
do {
printf("\nwill player one, use X or O as his symbols? select by pressing x or o\n");
scanf(" %c", &P1symbol);
if (P1symbol == 'x' || P1symbol == 'o') {
num = 1;
}
} while ( num == 0);
if (P1symbol == 'x') {
P2symbol = "o";
}
else {
P2symbol = "x";
}
do {
printf("\n now choose the size of the game board, type a numeral and press enter");
scanf("%d", &b.size);
}while (b.size <= 0);
for (i=0; i=b.size; i++){
for (j=0; j=b.size; j++){
boardarray[i][j] = "-";
}
}
do {
do {
boardsketch(boardarray, b.size);
if (turn%2 == 1) {
printf("player 1, please choose a box to input you mark on");
mark = P1symbol;
}else{
printf("player 2, please choose a box to input you mark on");
mark = P2symbol;
}
printf("type the coordinates i,j, which correspond to the row and collumn number");
printf("make sure the numbers are valid, not taken, and between 0 and %d", b.size);
scanf("%d %d", &p.row, &p.col);
}while (p.row > b.size && p.row < 0 && p.col > b.size && p.col <0 && boardarray[p.row][p.row] != "-");
turn++;
boardarray[p.row][p.col] = mark;
} while (wincheck(boardarray, p.row, p.col, b.size) != 1);
return 0;
}
int wincheck(int row, int col, int size, char boardarray[size][size])
{
if (boardarray[row][col] = boardarray[row -1][col -1] = boardarray[row +1][col +1]) {
return 1;
}
if (boardarray[row][col] = boardarray[row -1][col] = boardarray[row +1][col]) {
return 1;
}
if (boardarray[row][col] = boardarray[row][col -1] = boardarray[row][col +1]){
return 1;
}
if (boardarray[row][col] = boardarray[row -1][col +1] = boardarray[row +1][col -1]){
return 1;
}
}
void boardsketch(int size, char boardarray[size][size]) {
int i, j;
for (i=0; i=size; i++) {
for (j=0; j=size; j++) {
if (boardarray[i][j] == '-') {
printf("| ");
} else {
printf("%c |", &boardarray[i][j]);
}
}
}
}
Now the program's purpose is to simulate a game of tic tac toe (with the addition of the user, deciding the size of the game board). My problem is that, altough compilation IS achieved the program does 2 wierd behaviors when reaching a specific line, that line being:
do {
printf("\n now choose the size of the game board, type a numeral and press enter");
scanf("%d", &b.size);
}while (b.size <= 0);
If i input a value that doesnt obey to b.size <= 0, the printf above, repeats indefinately, if i DO put a correct value, the programm doesnt resume. What am i doing wrong? again im new at programming sooooo... go easy on me :D
There are compiler errors in your code. I don't know how you got it to compile and build it the first place.
Compiler errors:
You have:
P2symbol = "o";
Type of "o" is char const*. The type of P2symbol is char. What you need is
P2symbol = `o`;
Few lines after that, you have:
P2symbol = "x";
That needs to be changed to:
P2symbol = `x`;
Few lines after that, you have:
boardarray[i][j] = "-";
It suffers from the same compiler error. You need to change it to:
boardarray[i][j] = `-`;
Your declaration and definition of boardsketch does not match with the way you are calling it. Your call is:
boardsketch(boardarray, b.size);
You have defined it as:
void boardsketch(int size, char boardarray[size][size]) {
....
}
You need to change either the call or the function definition so that they match. Also, you should declare the function before it is used. Add
void boardsketch(int size, char boardarray[size][size]);
before the start of main.
The definition and call of wincheck suffers from the same error. It also should have a declaration before it's usage.
A few lines after that call to boardarray, you have the line:
}while (p.row > b.size && p.row < 0 && p.col > b.size && p.col <0 && boardarray[p.row][p.row] != "-");
The last part of that statement suffers from the char and char const* mismatch. You need to change it to:
}while (p.row > b.size && p.row < 0 && p.col > b.size && p.col <0 && boardarray[p.row][p.row] != '-');
Run Time Errors:
You have:
_board b;
char P1symbol, P2symbol, mark, boardarray[b.size][b.size];
The problem with that is b.size is not initialized. It could be anything. Using it to declare broadarray is problem. Imagine the chaos that will ensue if the b.size were to be initialized to a negative number. For sane and predictable behavior, you should initialize b properly before using its data.
A few lines below, you are asking for size to be input by the user.
do {
printf("\n now choose the size of the game board, type a numeral and press enter");
scanf("%d", &b.size);
}while (b.size <= 0);
There is a logic error here. You are asking for the size of the board after you have already created boardarray. What you could do is gather the initial input and use them to call another function where the core of the game play happens.
/* Function that contains the core part of playing the game */
void playgame(char P1symbol, char P2symbol, int size)
{
int i, j, turn=1, victory = 0;
char mark, boardarray[size][size];
position p;
for (i=0; i=size; i++){
for (j=0; j=size; j++){
boardarray[i][j] = '-';
}
}
do {
do {
boardsketch(size, boardarray);
if (turn%2 == 1) {
printf("player 1, please choose a box to input you mark on");
mark = P1symbol;
}else{
printf("player 2, please choose a box to input you mark on");
mark = P2symbol;
}
printf("type the coordinates i,j, which correspond to the row and collumn number");
printf("make sure the numbers are valid, not taken, and between 0 and %d", size);
scanf("%d %d", &p.row, &p.col);
}while (p.row > size && p.row < 0 && p.col > size && p.col <0 && boardarray[p.row][p.row] != '-');
turn++;
boardarray[p.row][p.col] = mark;
} while (wincheck(p.row, p.col, size, boardarray) != 1);
}
Now, main can be simplified to:
int main () {
char P1symbol;
char P2symbol;
int size;
int num = 0;
printf("WELCOME TO THE GAME OF TIC TAC TOE!\n");
do {
printf("\nwill player one, use X or O as his symbols? select by pressing x or o\n");
scanf(" %c", &P1symbol);
if (P1symbol == 'x' || P1symbol == 'o') {
num = 1;
}
} while ( num == 0);
if (P1symbol == 'x') {
P2symbol = 'o';
}
else {
P2symbol = 'x';
}
do {
printf("\n now choose the size of the game board, type a numeral and press enter");
scanf("%d", &size);
}while (size <= 0);
playgame(P1symbol, P2symbol, size);
return 0;
}
Ah, the problem is your for loop after the do while. You are assigning your counters instead of evaluating the limits. Asigning them will result to true every time. Try this instead:
for (i=0; i<b.size; i++){
for (j=0; j<b.size; j++){
boardarray[i][j] = "-";
}
}
Also, do not create an array with undefine value b.size...

Resources