-4

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

Dttank
  • 3
  • 2
    The first compiler warning (among others) is for `printf("Welcome, %s\n");` which does not provide an argument. The next is for `scanf("%s");` ditto. Please look at the full warnings output. – Weather Vane Nov 30 '22 at 21:49
  • Have you tried running your code line-by-line in a debugger while monitoring the control flow and the values of all variables, in order to determine in which line your program stops behaving as intended? If you did not try this, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Andreas Wenzel Nov 30 '22 at 22:01
  • 1
    Please note that [it is generally expected that you make a debugging attempt yourself before asking for help on Stack Overflow](https://idownvotedbecau.se/nodebugging/). Questions which do not demonstrate any debugging attempt and do not specify what you have learnt in the debugging attempt, are usually not well received. – Andreas Wenzel Nov 30 '22 at 22:01
  • I also recommend adding more functions. You've got a lot going on in `main`. `CreateAnAccount` and `SignIn` should probably be functions at least. A function should do one thing and do it well. – yano Nov 30 '22 at 22:06

1 Answers1

1

It's totally normal, after while(choice != 1 && choice != 2) you have return 0;

What you need to do is change the structure of your code, to something like that:

main() {
    while(;;){
        scanf()
        if (choice == 1) { ... } // Choice #1
        if (choice == 2) { ... } // Choice #2
        if (choice == 0) { break }  // Exit the app
        else { printf("Ops! Tray again."); } // Wrong choice
    }
}
Albert Shown
  • 230
  • 7
  • So basically I put the whole program in a big while loop and the first if is the create an account the second the sign in and the third is exiting the app / else { printf("Ops! Tray again."); } ?? but doesnt the exiting the app need to be both in choice 1 and 2 ? or it doesnt matter? – Dttank Nov 30 '22 at 22:01
  • The idea behind `while(;;)` is to never exit the app until the user press 0, if you want to add Exit inside the second loop probably you will need to add a flag, or just simply use `exit(0);`. – Albert Shown Nov 30 '22 at 22:09
  • BTW, You get downvoted because you didn't add a section to explain how you tried to solve this issue. Stackoverflow tries to avoid `Hi guys, can you do my homework for me?` =) – Albert Shown Nov 30 '22 at 22:12