my printf loops not working as I wanted in c language

ghz 8months ago ⋅ 108 views
#include<stdio.h>
#include<time.h>
#include<stdlib.h>

void printRoom(int side, char wallChar, char name) 
{
    for(int i=0; i<side; i++) 
    {
        printf(" ");
        for(int j=0; j<side; j++) 
        {
            if(i == side/2 && j == side/2)
                printf("%c", name);
            else if(i == 0 || i == side-1 || j == 0 || j == side-1)
                printf("%c", wallChar);
            else
                printf(" ");
        }
        printf("\n");
    }
}


void direction(int side, char dir, char wallChar, char name) 
{
    int i,j;
    if(dir=='u')
    {
        printRoom(side,wallChar,name);
        printf("\n");
        if(i==side/2)
        {
        printf("^");
        printf("\n");
        printf("|");
        }
    }

    else if(dir=='d')
    {
        if(i==side/2)
        {
        printf("|");
        printf("\n");
        printf("v");
        }
        printRoom(side,wallChar,name);
    }

    else if(dir=='r')
    {
        if(j==side/2)
        {
            printf("->");
        }
        printRoom(side,wallChar,name);
    }

    else
    {
        printRoom(side,wallChar,name);
        if(j==side/2)
        {
            printf("<-");
        }
    }
    
}

int stepGenerator() 
{
     return rand() % 5 + 4; 
}

int main() 
{
    srand(time(NULL));
    char initial, dir;
    int side;
    int step=0;
    int stepsToWin=stepGenerator();
    char wallChar = '#'; 

    printf("Enter the initial of your name: ");
    scanf(" %c", &initial); 

    while(stepsToWin != step) 
    {
        printf("Enter the side length of the room: ");
        scanf("%d", &side);
        while(getchar() != '\n'); 
        printf("Enter the direction you wanna go: ");
        scanf("%c", &dir); 
        direction(side, dir, wallChar, initial);
        step++;
    }
     printf("Enter the side length of the room: ");
    scanf("%d", &side);
    while(getchar() != '\n'); 
    printf("Enter the direction you wanna go: ");
    scanf("%c", &dir);
    wallChar='$';
    direction(side, dir, wallChar, initial);
    printf("\n");
    printf("congrats you have won the game!");

    return 0;
}

Expected output for choosing right direction

   # # # # # 
   #       # 
-->#   y   # 
   #       # 
   # # # # # 

Expected output for choosing left direction

   # # # # # 
   #       # 
   #   y   # <--
   #       # 
   # # # # # 

Expected output for choosing down direction

       |       
       v       
   # # # # # 
   #       # 
   #   y   # 
   #       # 
   # # # # # 

Expected output for choosing up direction

   # # # # # 
   #       # 
   #   y   # 
   #       # 
   # # # # # 
       ^       
       |       

Expected output for winning the game(was chosen right for example)

   $ $ $ $ $ 
   $       $ 
-->$   y   $ 
   $       $ 
   $ $ $ $ $ 
Congratulations! You've found the treasure.

I dont get why the arrow doesn't print out. I have tried asking gpt but it breaks other things while trying to fix the issues I want to fix.

Also as I mentioned I dont know what getchars() is and we haven't seen it in class either.is there anyway my code will work without using getchars()?

Answers

The issue with your code is that the variables i and j inside the direction() function are not initialized, which leads to unpredictable behavior when checking their values.

To fix this issue, you need to initialize i and j properly inside the direction() function. You can pass them as parameters to the function along with other necessary parameters. Additionally, you need to correctly handle the arrow direction printing based on the chosen direction.

Here's the corrected version of your direction() function:

void direction(int side, char dir, char wallChar, char name, int i, int j) 
{
    if(dir == 'u')
    {
        if(j == side/2)
        {
            printf("^\n");
            printf("|\n");
        }
        printRoom(side, wallChar, name);
    }
    else if(dir == 'd')
    {
        printRoom(side, wallChar, name);
        if(j == side/2)
        {
            printf("|\n");
            printf("v\n");
        }
    }
    else if(dir == 'r')
    {
        if(i == side/2)
        {
            printf("->");
        }
        printRoom(side, wallChar, name);
    }
    else
    {
        printRoom(side, wallChar, name);
        if(i == side/2)
        {
            printf("<-");
        }
    }
}

In this corrected version, i and j are passed as parameters to the direction() function, and their values are checked appropriately before printing the arrow direction.

Regarding getchar() function: The getchar() function is used to read a single character from the standard input stream (stdin). It is often used to consume newline characters left in the input buffer after using scanf() to prevent them from being read as input for subsequent input operations. In your code, it's being used to clear the input buffer after reading the integer input for the side length of the room. However, you can achieve the same effect by adding a space before the %c format specifier in scanf() to consume the newline character, like this:

scanf(" %c", &dir); // Notice the space before %c

This way, you don't need to use getchar() to clear the input buffer.