Hello! I'm a senior in high school and I've been trying to learn C in order to get into serious coding (I've been using basic stuff like Scratch for the past 6 years). I've made attempts to learn it before but I've never really committed to it until the beginning of this school year now that I'm taking a computer science class. I'm picking up on it pretty quickly, as like I said I have fairly extensive experience with kids' languages like Scratch so I do have a general sense of how programming works.

Anyways, I've been working hard lately on my first actual "project" in a sense- it's nothing special, but I've essentially created an ASCII version of the board game "Battleship" with user inputs and whatnot. It's not perfectly coded- there are some flaws with it as well as a bunch of issues with variables that I had to just work my way around since I couldn't figure out why they were happening, and I'm sure I could have written it more efficiently- but I've gotten it to function using OnlineGDB, an in-browser compiler which I've been mainly writing the code in. I used stdio.h, stdlib.h, and initially conio.h before I switched it out with scurses.h so it'd work on my MacBook.

However, the issues seem to be popping up whenever I try to run the code in any other compilers (by which I mean the 2 others that I attempted lol). I installed Xcode earlier today on my MacBook and tried running the .c file through the terminal, and was greeted with this error message:

error: function definition is not allowed here
{

and the same thing happens whenever I try to run the file in repl.it, another online compiler. Furthermore, in both cases the error happens 10 times, each appearing wherever some function or another is supposed to be defined, and is linked to the following lines in main.c:

  • 85.3
  • 151.3
  • 173.3
  • 198.3
  • 283.3
  • 313.3
  • 364.3
  • 456.3
  • 507.3
  • 540.3


Beyond this there are several assorted notes and warnings relating to undeclared functions and whatnot, but I’ve generally been under the impression that these are all connected to the above 10 errors.

I honestly have no clue what the issue here may be. From what I’ve looked into, it appears that this error shows up when an opening bracket appears without a companion closing one or vice versa, but I used the cmd + f thing to check and I believe there were perfectly equal numbers of opening and closing brackets (I think 134 each, to be exact). Furthermore, even if the brackets were unbalanced, that still wouldn’t explain why it works whenever I run it through onlineGDB, but not through Terminal or repl.it. Does anyone here have any clue what the issue(s) may be?

I've attached the .c file for the project below, and here is the link again to the repl.it upload of the file. Please let me know if you need a tl;dr, or if there are any details that you need clarification on. Thanks!

~Tristan

Battleship (ncurses).c