0

I would like to convert the following program to run on a linux system for my school assignment. I am struggling very hard because the linux computer labs are shutdown due to covid. So, I coded my homework on my windows computer. I had access today and it did not run due to multiple errors. What can I do to convert my code to make it linux compatible?

#include <stdio.h>
#include <string.h>






char ch;

#define NUMROWS 6
#define NUMCOLS 6
int i, j;

char str1[42] = {'S', '#', '#', '#', '#', '#', '\n', '.', '.', '.', '.', '.', '#', '\n', '#', '.', '#', '#', '#', '#', '\n', '#', '.', '#', '#', '#', '#', '\n', '.', '.', '.', '#', '.', 'G', '\n', '#', '#', '.','.','.','#','\n'};


main()
{


    
    
    char response[10];
    char response2[10];
    char yes;
    char no;
    char ready;
    char notready;
    
    printf("Do you want to play the maze game? yes|no : \n");
    scanf("%s", &response);
    
    if (!strcmp(response, "yes"))
    {
    printf("\n");
    printf("Game will begin!");
    printf("\n");
    printf("\n");
    
    printf("Maze Board Example: \n");
    printf("S##### \n");
    printf(".....# \n");
    printf("#.#### \n");
    printf("#.#### \n");
    printf("...#.G \n");
    printf("##...# \n");
    printf("\n");
        
    printf("Intstructions:");
    printf("Are you ready to play? ready|no \n");
    scanf("%s", &response);
    
    if (!strcmp(response, "ready"))
    {
        printf("playing...");
        

        system("cls");



        while(1)
        {
            mazeGo();
        }
    }
    

    }
    
    
    
    
    
    if (!strcmp(response, "no"))
    {
        printf("\n");
        printf("Game will exit");

    }
    
    return 0;
    
    
    
    
}

    
    
void mazeGo()
{


    str1[0]= '+';
    printf("%s", str1);
    sleep(1);
    system("cls");
    str1[7] = '+';
    printf("%s", str1);
    sleep(1);
    system("cls");
    str1[8] = '+';
    printf("%s", str1);
    sleep(1);
    system("cls");
    str1[9] = '+';
    printf("%s", str1);
    sleep(1);
    system("cls");
    str1[10] = '+';
    printf("%s", str1);
    sleep(1);
    system("cls");
    str1[11] = '+';
    printf("%s", str1);
    sleep(1);
    system("cls");
    str1[15] = '+';
    printf("%s", str1);
    sleep(1);
    system("cls");
    str1[22] = '+';
    printf("%s", str1);
    sleep(1);
    system("cls");
    str1[28] = '+';
    printf("%s", str1);
    sleep(1);
    system("cls");
    str1[29] = '+';
    printf("%s", str1);
    sleep(1);
    system("cls");
    str1[30] = '+';
    printf("%s", str1);
    sleep(1);
    system("cls");
    str1[37] = '+';
    printf("%s", str1);
    sleep(1);
    system("cls");
    str1[38] = '+';
    printf("%s", str1);
    sleep(1);
    system("cls");
    str1[39] = '+';
    printf("%s", str1);
    sleep(1);
    system("cls");
    str1[32] = '+';
    printf("%s", str1);
    sleep(1);
    system("cls");
    str1[33] = '+';
    printf("%s", str1);
    sleep(1);
    system("cls");
    
    printf("\n You win!");
    
    exit(0);

}





Blastfurnace
  • 17,441
  • 40
  • 50
  • 66
Brendan
  • 39
  • 3
  • It would be helpful, if you could add the error (messages) you had to your question. On first sight, the `system("cls")` will generally not work on Linux. It is a built-in command of Windows' `CMD.EXE`, which is what runs the "strings" you pass to `system()` on Windows. On Linux it would (for the sake of argument) be a "bourne-shell", so try `system("clear")` instead. YMMV. – Christian.K Mar 11 '21 at 07:44
  • 1
    why are there so many unnecessary blank lines? – phuclv Mar 11 '21 at 08:14
  • 1
    Indentation is inconsistent too. Makes it hard to read. – Shawn Mar 11 '21 at 08:28
  • Unrelated: use a Linux [virtual machine](https://en.wikipedia.org/wiki/Virtual_machine) running inside your windows host – pmg Mar 11 '21 at 08:39

2 Answers2

1

I cannot imagine you got no warnings on Windows...

...
printf("Do you want to play the maze game? yes|no : \n");
scanf("%s", &response);
...

=> format '%s' expects argument of type 'char ', but argument 2 has type 'char ()[10]

It should be scanf("%s", response); or even better: scanf("%9s", response);

That is enough to invoke Undefined Behaviour and have the program run smoothly, crash at any moment or provide non consistent output... And the error is repeated in the program...

system("cls");

=> warning: implicit declaration of function 'system'

Not an error of course, but best practices recommend to declare functions before using them. You should use #include <stdlib>

void mazeGo()

=> warning: conflicting types for 'mazeGo'

The identifier was first encountered at mazeGo(); and was implitely declared as int mazeGo(). You should have a void mazeGo(); declaration before main

And a number or unused variables. Here again it is not an error but leaving unused variables in a code could hide typos and make future maintenance harder.

Apart from that, system("cls") will clear the screen only on Windows. On Linux the shell started by system will complain for an unknown command but it should not abort the program.

TL/DR:

  1. Warnings are not to be ignored
  2. If the program had been correct on Windows it would have run on Linux (except for the screen clearing)
Serge Ballesta
  • 121,548
  • 10
  • 94
  • 199
0

Firstly you should add the return type of void to your main function.

void main()...

then you're trying to execute the "cls" command, which in linux will not work, because it's a windows specific command.

  • 4
    The [correct return type for `main`](https://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c/204483#204483) is `int`, independent of the system. (And OP does `return 0` at the end, so the function isn't void.) – M Oehm Mar 11 '21 at 07:44
  • `main() {...` without the return type would at least be interpreted as `int main() {...`. Said differently this suggestion makes the code worse than it already is :-( – Serge Ballesta Mar 11 '21 at 08:11