if/else statements in for loop not working - c

I'm trying to make a program that asks for user input and the for loop should check if the input of both user id and pin matches any of the ten pre-made account's user id and pin, like an authorization system
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
struct account{
int uid;
int pin;
int user_bal;
};
int main()
{
int scan_uid, scan_pin;
int att = 3;
bool loop = true;
struct account user[10];
user[0].uid = 1234;
user[0].pin = 123456;
user[1].uid = 4181;
user[1].pin = 308592;
user[2].uid =1111;
user[2].pin =111111;
user[3].uid =2222;
user[3].pin =222222;
user[4].uid =4444;
user[4].pin =444444;
user[5].uid =5555;
user[5].pin =555555;
user[6].uid =6666;
user[6].pin =666666;
user[7].uid =7777;
user[7].pin =777777;
user[8].uid =8888;
user[8].pin =888888;
user[9].uid =9999;
user[9].pin =999999;
for (int i; i <= 9; i++){
user[i].user_bal = 1000;
}
do{
printf("\nEnter your user ID: ");
scanf("%d", &scan_uid);
printf("Enter your pin: ");
scanf("%d", &scan_pin);
printf("\n--------------------------------------------\n");
att--;
for (int i; i <= 9; ++i){
//printf("\n%d", i);
//printf("\n%d", user[i].uid);
//printf("\n%d", user[i].pin);
//printf("\n%d", scan_uid);
//printf("\n%d", scan_pin);
if (user[i].uid == scan_uid && user[i].pin == scan_pin){
loop = false;
}
else{
printf("\nThe username or password is incorrect!");
printf("\nYou have %d attempt(s) left.", att);
if (att > 0)
{
printf("\nPlease try again.\n");
}
else if (att == 0)
{
printf("\nUnauthorized Access.");
printf("\nReport for stolen credit card uploaded.");
}
}
}
}while (att > 0 || loop == false);
return 0;
}
I tried the relatively same code in python and it works perfectly there. I also checked if the "i" is correct and incremented and if it scanned the user input correctly. all ok. But i've hit a brick wall trying to solve why it just skips the 'if/else' and just scans input again.
I also tried an 'else if ' that does the opposite(!=) of the initial 'if' statement, with no luck.
Thanks.

Others have already pointed out that you need to initialize int i with int i = 0 in your for loop.
However, I would additionally like to point out several other mistakes here. First, your loop condition appears to be incorrect:
while (att > 0 || loop == false)
You want to continue the loop in the case where they haven't entered the correct value yet and where they still have more attempts left. However, as written, this will continue to loop even if the user enters the correct password. I think that this should actually be
while (att > 0 && loop == true)
Also, most languages don't require you to explicitly compare to true and false, so the following is stylistically better:
while (att > 0 && loop)
Also, as written, it prompts the user to try again; however, it doesn't prompt the user for input again after the first time, so it's impossible for the user to try again. You need to prompt the user for input again inside the loop if their input was incorrect.

A few issues ...
As Dan said, initialize i to 0. This will fix the segfault.
When you do loop = false; you need break; immediately afterwards.
And, your do/while condition is wrong. Change while (att > 0 || loop == false); into while ((att > 0) && loop);
The placement of the "incorrect passwords" related printf is incorrect. It should come after the for.
Otherwise, the same error message will be repeated 9 times for a single incorrect answer.
And, we want to exit the do/while loop [immediately after the for loop] if we see loop become false.
Here is the refactored code. It is annotated:
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
struct account {
int uid;
int pin;
int user_bal;
};
int
main(void)
{
int scan_uid,
scan_pin;
int att = 3;
bool loop = true;
struct account user[10];
user[0].uid = 1234;
user[0].pin = 123456;
user[1].uid = 4181;
user[1].pin = 308592;
user[2].uid = 1111;
user[2].pin = 111111;
user[3].uid = 2222;
user[3].pin = 222222;
user[4].uid = 4444;
user[4].pin = 444444;
user[5].uid = 5555;
user[5].pin = 555555;
user[6].uid = 6666;
user[6].pin = 666666;
user[7].uid = 7777;
user[7].pin = 777777;
user[8].uid = 8888;
user[8].pin = 888888;
user[9].uid = 9999;
user[9].pin = 999999;
for (int i; i <= 9; i++) {
user[i].user_bal = 1000;
}
do {
printf("\nEnter your user ID: ");
scanf("%d", &scan_uid);
printf("Enter your pin: ");
scanf("%d", &scan_pin);
printf("\n--------------------------------------------\n");
att--;
// NOTE/BUG: i is uninitialized
#if 0
for (int i; i <= 9; ++i) {
#else
for (int i = 0; i <= 9; ++i) {
#endif
// printf("\n%d", i);
// printf("\n%d", user[i].uid);
// printf("\n%d", user[i].pin);
// printf("\n%d", scan_uid);
// printf("\n%d", scan_pin);
if (user[i].uid == scan_uid && user[i].pin == scan_pin) {
loop = false;
// NOTE/FIX: no need to continue loop if we get a match
#if 1
break;
#endif
}
}
#if 1
// stop if everything matched
if (! loop)
break;
#endif
printf("\nThe username or password is incorrect!");
printf("\nYou have %d attempt(s) left.", att);
if (att > 0) {
printf("\nPlease try again.\n");
}
else if (att == 0) {
printf("\nUnauthorized Access.");
printf("\nReport for stolen credit card uploaded.");
}
} while (att > 0);
return 0;
}
In the above code, I've used cpp conditionals to denote old vs. new code:
#if 0
// old code
#else
// new code
#endif
#if 1
// new code
#endif
Note: this can be cleaned up by running the file through unifdef -k

First of all, you should initialize i to 0
To browse all array contents, you should increment i after the instruction NOT before (i++) instead of (++i)
Condition while loop is WRONG
Look at this version of your code, It works properly:
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
struct account{
int uid;
int pin;
int user_bal;
};
int main()
{
int scan_uid, scan_pin;
int att = 3;
bool loop = true;
struct account user[10] = {0};
user[0].uid = 1234;
user[0].pin = 123456;
user[1].uid = 4181;
user[1].pin = 308592;
user[2].uid =1111;
user[2].pin =111111;
user[3].uid =2222;
user[3].pin =222222;
user[4].uid =4444;
user[4].pin =444444;
user[5].uid =5555;
user[5].pin =555555;
user[6].uid =6666;
user[6].pin =666666;
user[7].uid =7777;
user[7].pin =777777;
user[8].uid =8888;
user[8].pin =888888;
user[9].uid =9999;
user[9].pin =999999;
for (int i = 0; i <= 9; i++){
user[i].user_bal = 1000;
}
do{
printf("\n--------------------------------------------\n");
printf("\nEnter your user ID: ");
scanf("%d", &scan_uid);
printf("Enter your pin: ");
scanf("%d", &scan_pin);
printf("\n--------------------------------------------\n");
att--;
for (int i = 0; i <= 9; i++){
//printf("\n%d", i);
//printf("\n%d", user[i].uid);
//printf("\n%d", user[i].pin);
//printf("\n%d", scan_uid);
//printf("\n%d", scan_pin);
if (user[i].uid == scan_uid && user[i].pin == scan_pin)
{
loop = false;
break;
}
}
if (loop && att)
{
printf("\nThe username or password is incorrect!");
printf("\nYou have %d attempt(s) left.", att);
}
else if (! att && loop)
{
printf("\nUnauthorized Access.");
printf("\nReport for stolen credit card uploaded.\n");
}
else if (!loop)
{
printf("\nMatches !\n");
}
} while ((att > 0) && (loop == true));
return 0;
}

Related

Why doesn't the if statement print 'well done' and why does the loop repeat out questions()

I wanted to make a game where in which you get three turns to guess the selected number, from the computer. However it will never say well done even if you get the number right (already tested using printf statements) and the loop will also mess up by either continuing or running twice.
I've tried removing the break.
I've put in printf statements to check if randomise and questions actually store the correct values and they do.
# include <stdio.h>
# include <stdlib.h>
# include <math.h>
# include <time.h>
int questions() {
int num;
printf("Chose a number between one - ten: ");
scanf("%d",&num);
return num;
}
int randomise() {
int roll;
srand(time(NULL));
roll = rand()%10 + 1;
return roll;
}
int main() {
int chosenNum, enteredNum, i;
chosenNum = randomise();
enteredNum = questions();
for(i = 0; i < 10; i++) {
if(chosenNum != enteredNum) {
questions();
break;
}
else if(chosenNum == enteredNum) {
printf("WELL DONE !!!");
}
}
return 0;
}
Zero errors and Zero warnings. and the outcome should be that you get greeted with well done.
There are two issues:
You are using break which will not allow another attempt to input the number if first number is not matched, and
the return value from questions() in the loop is not assigned to enteredNum (it means it will try old number second time).
enteredNum is never set inside the loop. Thus, it's impossible for the if be false unless it is false the first time.
Fix your loop to something like this instead:
for(i = 0; i < 10; i++){
if(chosenNum != enteredNum){
enteredNum = questions();
break;
}
else if(chosenNum == enteredNum){
printf("WELL DONE !!!");
}
}
This is the most recent update of the code, thanks for all your help guys. The feedback was great ! Heres the code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
int questions(){
int num;
printf("Chose a number between one - ten: ");
scanf("%d",&num);
return num;
}
int randomise() {
int roll;
srand(time(NULL));
roll = rand()%10 + 1;
return roll;
}
int main(){
int chosenNum, enteredNum, i;
int life = 3;
chosenNum = randomise();
enteredNum = questions();
for(i = 0; i < 2; i++){
enteredNum = questions();
if((enteredNum != chosenNum) && life > 0){
life -= 1;
if(life == 1 ){
printf("GAME OVER !!!\n");
break;
}
}
else if((chosenNum == enteredNum) && life > 0){
printf("WELL DONE !!!\n");
break;
}
}
return 0;
}

Need help fixing Segmentation Fault

I had this working a second ago but accidentally broke it. Can anyone help me fix it? I'm getting a Segmentation Fault so I assume I messed up the pointers at some point. It's supposed to generate a bunch of random numbers depending on user input.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
unsigned int mySeed; // creates our variables
unsigned int taps[2];
unsigned int temp[2];
unsigned int myToggle;
FILE *fp;//opens the file
fp = fopen("random.txt", "w");//sets the file to open equal to this file
int TapInputs = 1;
int count = 0;
int tap;
int myNewNumber = 0;
mySeed = atoi(argv[1]);
if(atoi(argv[1]) > 0) //Error checking for negative inputs.
{
printf("Please enter the taps you'd like to use : ");//prompts user to input the taps and then makes sure theyre in range
while(TapInputs)
{
scanf("%d",&tap);
if((tap > 0)&&(tap < 33))
{
*(taps+count)=tap;
}
else if(tap == -1) // when we find -1 we do this
{
TapInputs = 0;
}
else if(tap > 32)
{
exit(0);
}
count++;
}
printf("How many numbers do you want to generate: "); //prompts user to input the number of numbers to use
scanf("%d", &myNewNumber);
while (myNewNumber < 0)// error checking for positive inputs
{
printf("How many numbers do you want to generate: ");
scanf("%d", &myNewNumber);
}
printf("\nRandom Numbers:");
while(myNewNumber)//creates number equal to the user input number in the previous step
{
temp[0] = mySeed; // makes temp1 the seed
temp[1] = mySeed; // makes temp2 the seed
temp[0] = (temp[0] >> taps[0]) & 1; // checks and sets the bit
temp[1] = (temp[1] >> taps[1]) & 1; // checks and sets the bit
myToggle = (temp[0] ^ temp[1]); // here we xor the temp1 and 2
mySeed = (mySeed << 1) ^ myToggle; // use bittoggle to shift the seed and generate a new number
fprintf(fp, "%d\r\n", mySeed); // wrties the generated number into the file
printf("\n%d", mySeed); // prints the number
myNewNumber -= 1;
}
fclose(fp); // closes file, creates a new line and returns 0 to the fucntion
printf("\n");
return 0;
}
else
{ // if the number the user input was 0 we will end our program
exit(0);
}
}
The fault happens immediately upon execution.
This piece of code :
while(TapInputs)
{
scanf("%d",&tap);
if((tap > 0)&&(tap < 33))
{
*(taps+count)=tap;
}
else if(tap == -1) // when we find -1 we do this
{
TapInputs = 0;
}
else if(tap > 32)
{
exit(0);
}
count++;
}
is executed until you find TapInputs to be false, or in other words 0. This will happen only if you give -1 as an input to scanf("%d", &tap). Until then, you will keep reading and also incrementing count.
But some lines above, you have declared
unsigned int taps[2];
and in your while loop you do
*(taps+count)=tap;
So, if you have read tap enough times and keep finding it between 0 and 33, until you find it -1, count will have increased enough to get your array out of bounds.
I think problem is in this line , mySeed = atoi(argv[1]);
you have to do something like this,
you can put the code from that in an if condition ,
if(agrc>1)
{
mySeed = atoi(argv[1]);
---------------------
--------------------
}
I have tested...it is working
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
unsigned int mySeed; // creates our variables
unsigned int taps[2];
unsigned int temp[2];
unsigned int myToggle;
FILE *fp;//opens the file
fp = fopen("random.txt", "w");//sets the file to open equal to this file
int TapInputs = 1;
int count = 0;
int tap;
int myNewNumber = 0;
if(agrc>1)
{
mySeed = atoi(argv[1]);
if(atoi(argv[1]) > 0) //Error checking for negative inputs.
{
printf("Please enter the taps you'd like to use : ");//prompts user to input the taps and then makes sure theyre in range
while(TapInputs)
{
scanf("%d",&tap);
if((tap > 0)&&(tap < 33))
{
*(taps+count)=tap;
}
else if(tap == -1) // when we find -1 we do this
{
TapInputs = 0;
}
else if(tap > 32)
{
exit(0);
}
count++;
}
printf("How many numbers do you want to generate: "); //prompts user to input the number of numbers to use
scanf("%d", &myNewNumber);
while (myNewNumber < 0)// error checking for positive inputs
{
printf("How many numbers do you want to generate: ");
scanf("%d", &myNewNumber);
}
printf("\nRandom Numbers:");
while(myNewNumber)//creates number equal to the user input number in the previous step
{
temp[0] = mySeed; // makes temp1 the seed
temp[1] = mySeed; // makes temp2 the seed
temp[0] = (temp[0] >> taps[0]) & 1; // checks and sets the bit
temp[1] = (temp[1] >> taps[1]) & 1; // checks and sets the bit
myToggle = (temp[0] ^ temp[1]); // here we xor the temp1 and 2
mySeed = (mySeed << 1) ^ myToggle; // use bittoggle to shift the seed and generate a new number
fprintf(fp, "%d\r\n", mySeed); // wrties the generated number into the file
printf("\n%d", mySeed); // prints the number
myNewNumber -= 1;
}
fclose(fp); // closes file, creates a new line and returns 0 to the fucntion
printf("\n");
return 0;
}
else
{ // if the number the user input was 0 we will end our program
exit(0);
}
}
}

C If Loop Fails to Set Struct Data

This has stumped me for a few days now. I have tried searching but have turned up mostly just definitions for struct...
In an if loop I set 2 struct variables, but neither of them come out correct. guests[x].plusone is stuck at 0 and guests[x].plusonefood is simply empty. If I set them (with the same line) outside of the loop, however, there are no issues. My compiler shows no warnings or errors. What am I missing?
This is my first project with C, so please point out anything else you notice, too.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct guestinfo
{
char name[50];
short int plusone;
char food[50];
char plusonefood[50];
};
char temp[5];
char setfood (int f)
{
char foodtemp[f];
int x;
for (x=0; x < f; x++)
{
printf("Food option %d:\n", (x+1));
fgets(&foodtemp[x],100,stdin);
}
return *foodtemp;
}
int main ()
{
int number_of_rsvp=0;
int number_of_food=0;
printf("Number of food choices:\n");
fgets(temp,5,stdin);
number_of_food = atoi (temp);
while (number_of_food <= 0)
{
printf("Please enter a number greater than 0\n");
fgets(temp,5,stdin);
number_of_food = atoi (temp);
}
char food [50] [number_of_food];
**food = setfood(number_of_food);
printf("Number of RSVPs:\n");
fgets(temp,5,stdin);
number_of_rsvp = atoi (temp);
while (number_of_rsvp <= 0)
{
printf("\nPlease enter a number greater than 0\n");
fgets(temp,5,stdin);
number_of_rsvp = atoi (temp);
};
struct guestinfo guests[number_of_rsvp];
int x;
int f;
for (x=0; x < number_of_rsvp; x++)
{
// add input validation to this section
printf("Guest Number %d:\n\nGuest Name:\n", (x+1));
fgets(guests[x].name,50,stdin);
printf("Food Choice #:\n");
fgets(temp,3,stdin);
f = atoi(temp);
f--;
*guests[x].food = food [50] [f];
printf("Plus One? Y/N\n");
fgets(temp,3,stdin);
}
if (strchr (temp,'Y') != NULL || strchr (temp,'y') != NULL) //This loop
{
guests[x].plusone = 1;
printf("Plus one food choice #:\n");
fgets(temp,3,stdin);
f = atoi(temp);
f--;
*guests[x].plusonefood = food [50] [f];
}
else if (strchr (temp,'N') != NULL || strchr (temp,'n') != NULL)
{
guests[x].plusone = 0;
};
FILE *guestlist = fopen ("guestlist.txt","w");
// debugging
printf("%d\n",guests[0].plusone);
printf("%s\n",guests[0].plusonefood);
for (x=0; x < number_of_rsvp; x++)
{
fprintf(guestlist,"Guest Number %d:\n\nName: %s\nFood Choice: %s\n",(x+1),guests[x].name,guests[x].food);
switch (guests[x].plusone)
{
case 1:
{
fprintf(guestlist,"Plus One: Yes\n\tPlus One Food: %s\n\n",guests[x].plusonefood);
break;
}
case 0:
{
fprintf(guestlist,"Plus One: No\n\n");
break;
}
default:
{
break;
}
}
}
fclose (guestlist);
printf("Printing Guest List...\n");
return 0;
}
The problem is that you are accessing an invalid memory place.
The array has number_of_rsvp elements.
Your for() loop interates over the variable x up to the value number_of_rsvp and exits.
After that, the value of x is equal to number_of_rsvp.
Since the index of the array only can go from 0 to number_of_rsvp - 1, the element guests[x], which is guests[number_of_rsvp], is out-of-bound.

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...

How can you get characters without the \n?

In my code below I am trying to ignore new lines. Is there a better way of doing this?
do
{
scanf("%c",&wouldCount);
} while(wouldCount == '\n');
original code
#include <stdio.h>
int main()
{
char Yes = 'Y';
char wouldCount;
int counter;
int start;
int end;
int increment;
const END_DEFAULT = 1;
const INCREMENT_DEFAULT = 1;
printf("Would you like to count?");
scanf("%c",&wouldCount);
while(wouldCount == 'Y' || wouldCount == 'y')
{
printf("Please enter start number.");
scanf("%d",&start);
printf("Please enter a number you would like to stop at?");
scanf("%d",&end);
printf("Please enter a number to increment by.");
scanf("%d",&increment);
if(end < start)
{
end = start + END_DEFAULT;
}
if(increment <= 0)
{
increment = INCREMENT_DEFAULT;
}
for(counter = start; counter < end; counter += increment)
{
printf("%d\n",counter);
}
printf("Would you like to count?");
do
{
scanf("%c",&wouldCount);
} while(wouldCount == '\n');
}
return 0;
}
you could change scanf("%c",&wouldCount); to scanf("\n%c",&wouldCount); as well as forgo the do/while loop. This will tell scanf to ignore an enter with no character entered.
see scanf c++ reference

Resources