#include <iostream> using namespace std; char M [20][10]; int x=20/2; int y=10/2; int fruitX = rand() % 20; int fruitY = rand() % 10; int k=0; bool gameOver = false; char direction = 0; M[x][y] = '0'; M[fruitX][fruitY]= 'F'; 

    2 answers 2

    Because all your "code" is out of functions. And there variables can be declared, but it is already impossible to calculate expressions just like that. that is, write M [x] [y] = '0'; can not. This can only be written inside the function. Most likely someone just forgot to write int main() { before char M Well, close the brace at the end.

      If you are writing in C ++, which follows from the only tag to the question, you should have already learned that each program in this language must have an entry point , in other words, the main() function

      Try this (the code is updated according to the data obtained from the author’s comment):

       #include <stdio.h> #include <stdlib.h> /* Объявление всех необходимых глобальных переменных * Эти переменные будут доступны из всех частей программы * по своему имени */ char M[20][10]; int x = 20 / 2; // Это константное выражение, компилятор может посчитать int y = 10 / 2; // его на этапе сборки, поэтому им можно инициализировать переменные int fruitX; // В области объявлений нельзя вычислить rand() % 20, int fruitY; // поэтому инициализировать НЕконстантным выражением переменную нельзя int k = 0; bool gameOver = false; char direction = 0; /* Точка входа, эта функция выполняется после старта программы */ int main(int argc, char* argv[]) { /* Обратите внимание, что мы используем глобальную переменную * Если написать что-то вроде * int fruitX = rand() % 20; * то в этой области видимости мы закроем глобальную переменную * локальной переменной с тем же именем. Нам это не нужно */ fruitX = rand() % 20; fruitY = rand() % 10; M[x][y] = '0'; M[fruitX][fruitY] = 'F'; } 

      Your program does not do anything and does not output anything, but now at least it is going to (checked on gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-16))

      Learning C ++ is best conducted through books, in stages, thoughtfully. The option "copied an example and something earned" is fraught with problems and gaps in understanding. Serious language, treat him with respect) Good luck

      UPD: Added code, added comments

      • The fact is that all these variables should be used in functions, and I don’t really want to use pointers - Maxim Sorokin
      • Did we have to guess this through telepathy? Simply declare (declaration) variables outside the function, they will become global, and use on health. Your initial problem was that you tried to make the assignment (the implementation operation, the implementation) outside the function. And outside the function you can only declare, declare, perform something - you can not. - wirtwelt