So I'm trying to make a knock off version of Instagram, just for fun purposes, and the whole program is fine, when I select one it opens to create an account or if I select two, it opens to sign up. But if I select random numbers, it brings up the error message to select a number between one and two but when I select 1 or 2 after that message it just ends my program. What did I do wrong?
#define CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BOARD_SIZE 15 // Setting the board size
#define CHAIN 5
void init_board(char board[BOARD_SIZE][BOARD_SIZE], int size); /* Creating the functions */
void print_board(char board[BOARD_SIZE][BOARD_SIZE], int size);
struct CREATEacc
{
char username[16]; // Struct for creating an account
char password[20];
}ca;
struct signIn
{
char userACC[16]; // Struct for signing in
char kodikos[20];
}si;
int main(int argc, char* argv)
{
struct CREATEacc userchoice; // Users actions stored here
struct signIn details; // Users actions stored here
int choice, uchoice, uschoice, size, col = 17, row = 19; // Some integers that are needed for below
char board[BOARD_SIZE][BOARD_SIZE];
size = BOARD_SIZE;
printf("Welcome to Diogram\n");
printf("1. Create an account.\n"); // Diogram menu
printf("2. Already have an account? Sign in.\n");
scanf("%d", &choice);
// If user selects 1 then this is the code for creating an account
if (choice == 1)
{
printf("Username: \n");
scanf("%15s", &userchoice.username);
fgets(ca.username, sizeof(ca.username), stdin);
printf("Password: \n");
scanf("%19s", &userchoice.password);
fgets(ca.password, sizeof(ca.password), stdin);
printf("Username: %s\nPassword: %s\n", userchoice.username, userchoice.password);
printf("Account succesfully created!\n\n\n");
printf("Welcome, %s\n");
printf("1.HOME 2.SEARCH 3.PROFILE");
init_board(board, size);
print_board(board, size);
do
{
printf("\n\nHOME SEARCH PROFILE\n"); // Printing parts of the app
scanf("%d", &uchoice); // Scans what the user selects
if (uchoice == 1)
{
printf("\n\n1.HOME 2.SEARCH 3.PROFILE"); // If selected, User's Diogram Home opens
init_board(board, size); // Calling function to initiate the board
print_board(board, size); // Printing the board
}
else if (uchoice == 2) // If selected, User's Diogram Search opens
{
printf("\n\n1.HOME 2.SEARCH 3.PROFILE");
printf("\nSearch an account here: ");
scanf("%s");
printf("Oops! Looks like you're the only one using Diogram :(\nGo back?\n");
}
else if (uchoice == 3) // If selected, User's Diogram Profile opens
{
printf("\n\n1.HOME 2.SEARCH 3.PROFILE 4.EXIT\n");
printf(" ____\n| |\n|____|\n%s\n\nPOSTS\n", userchoice.username);
init_board(board, size); // Calling function to initiate the board
print_board(board, size); // Printing the board
}
while (uchoice == 4) // If selected, user has the option to exit, and therefore end the program
{
char ch; // Character for checking Y/N
printf("\n\nAre you sure you want to log out(y/n)?\n");
scanf (" %c", &ch); // Scanning users anwser
// If nor y or n pressed, it does nothing until user finally picks y or n
while (ch !='y' && ch !='n')
{
scanf("%c", &ch);
}
if ( ch == 'y')
{
printf("Logged out succesfully!\n ");
exit(EXIT_SUCCESS); // Use exit() function to terminate the execution of the program
}
if (ch == 'n') // If user picks 'n' then it reverts back to his Diogram Profile
{
printf("\n\n1.HOME 2.SEARCH 3.PROFILE 4.EXIT\n");
printf(" ____\n| |\n|____|\n%s\n\nPOSTS\n", userchoice.username);
init_board(board, size);
print_board(board, size);
printf("\n\nHOME SEARCH PROFILE\n EXIT\n");
scanf("%d", &uchoice);
}
}
} while (uchoice !=4); // All of that on a do while loop so it never ends unless user decides to exit
}
// If user selects 2 then this is the code for signing in
else if (choice == 2)
{
printf("Username: \n");
scanf("%15s", &details.userACC);
fgets(si.userACC, sizeof(si.userACC), stdin);
printf("Password: \n");
scanf("%s", &details.kodikos); // User details
printf("Logged in succesfully!\n");
printf("Welcome back %s!\n", details.userACC);
printf("1.HOME 2.SEARCH 3.PROFILE");
init_board(board, size);
print_board(board, size);
do
{
printf("\n\nHOME SEARCH PROFILE\n"); // Printing parts of the app
scanf("%d", &uschoice); // Scans what the user selects
if (uschoice == 1) // If selected, User's Diogram Home opens
{
printf("\n\n1.HOME 2.SEARCH 3.PROFILE"); // Printing parts of the app
init_board(board, size); // Calling function to initiate the board
print_board(board, size); // Printing the board
}
else if (uschoice == 2) // If selected, User's Diogram Search opens
{
printf("\n\n1.HOME 2.SEARCH 3.PROFILE");
printf("\nSearch an account here: ");
scanf("%s");
printf("Oops! Looks like you're the only one using Diogram :(\nGo back?\n");
}
else if (uschoice == 3) // If selected, User's Diogram Profile opens
{
printf("\n\n1.HOME 2.SEARCH 3.PROFILE 4.EXIT\n"); // Printing parts of the app
printf(" ____\n| |\n|____|\n%s\n\nPOSTS\n", details.userACC);
init_board(board, size); // Calling function to initiate the board
print_board(board, size); // Printing the board
}
while (uschoice == 4) // If selected, user has the option to exit, and therefore end the program
{
char ch; // Character for checking Y/N
printf("\n\nAre you sure you want to log out(y/n)?\n");
scanf (" %c", &ch); // Scanning users anwser
// If nor y or n pressed, it does nothing until user finally picks y or n
while (ch !='y' && ch !='n')
{
scanf("%c", &ch);
}
if ( ch == 'y')
{
printf("Logged out succesfully!\n ");
exit(EXIT_SUCCESS); // Use exit() function to terminate the execution of a program
}
// If user picks 'n' then it reverts back to his Diogram Profile
if (ch == 'n')
{
printf("\n\n1.HOME 2.SEARCH 3.PROFILE 4.EXIT\n");
printf(" ____\n| |\n|____|\n%s\n\nPOSTS\n", details.userACC);
init_board(board, size);
print_board(board, size);
printf("\n\nHOME SEARCH PROFILE\n EXIT\n");
scanf("%d", &uschoice);
}
}
} while (uschoice !=4); // All of that on a do while loop so it never ends unless user decides to exit
}
while (choice != 1 && choice != 2)
{
printf("Oops! Looks like you did something wrong!\n");
printf("Try picking between 1 - 2.\n");
scanf("%d", &choice);
}
return 0; // End of the main program
}
void init_board(char board[BOARD_SIZE][BOARD_SIZE], int size)
{
for (int row = 0; row < BOARD_SIZE; ++row)
{
for (int col = 0; col < BOARD_SIZE; ++col) // Function for initiating the board
{
board[row][col] = '+';
}
}
}
// Function for printing the board
void print_board(char board[BOARD_SIZE][BOARD_SIZE], int size)
{
printf("\n ");
int row, col;
for (int i = 0; i < BOARD_SIZE + 6; ++i)
{
printf("---");
}
printf("\n");
for (int row = 0; row < BOARD_SIZE + 2; ++row)
{
printf("| ");
}
printf("\n");
for (int row = 0; row < BOARD_SIZE; ++row)
{
printf("|--");
for (int col = 0; col < BOARD_SIZE; ++col)
{
printf("-");
printf("%c", board[row][col]);
printf("--");
}
printf("-|");
printf("\n");
printf("|");
if (row != BOARD_SIZE - 1)
{
printf(" | | | | | | | | | | | | | | | |\n");
}
if (row == BOARD_SIZE - 1)
{
for (int i = 0; i < BOARD_SIZE + 1; ++i)
{
printf(" |");
}
printf("\n ");
for (int i = 0; i < BOARD_SIZE + 6; ++i)
{
printf("---");
}
}
}
}
I was expecting that the while loop actually works, because if you see the while loop, it has no flaws, the problem is somewhere else, but I can't seem to find where... Any help? :D