function isn't called

This is the beginning of snake game. Why function "atsiranda()" isn't called?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <cmath>
#include <windows.h>
#include <ctime>
using namespace std;
void goup(), goleft(), goright(), godown(), foxe(), atsiranda();
int x=9, y=9, paintx, painty, obuolys1, obuolys2;
char cord[20][20];
int main()
{ srand(time(NULL));
  int obuolys1=rand()%19;
  int obuolys2=rand()%19;
  cord[obuolys1][obuolys2]='o';
  cout << obuolys1 << " " << obuolys2; Sleep(1000);
    cin.get();
godown();
return 0;
}

void goup() {while(!kbhit())
 {

     system("CLS");
     if ((obuolys1==x) and (obuolys2==y-1)) {atsiranda();}
     cord[x][y-1]='x';
     cord[x][y]='-';
     y--;
     foxe();
     cout << x << " " << y;
     Sleep(100);
     if (kbhit())
        {
            char ch=getch();
            if (ch=='a') {goleft();}
            if (ch=='s') {godown();}
            if (ch=='d') {goright();}
        }
 }
         }
void godown() {while(!kbhit())
 {

     system("CLS");
     if ((obuolys1==x) and (obuolys2==y+1)) {atsiranda();}
     cord[x][y+1]='x';
     cord[x][y]='-';
     y++;
     foxe();
     cout << x << " " << y;
     Sleep(100);
     if (kbhit())
        {
            char ch=getch();
            if (ch=='w') {goup();}
            if (ch=='a') {goleft();}
            if (ch=='d') {goright();}
        }
 }
           }
void goleft() {while(!kbhit())
 {

     system("CLS");
     if ((obuolys1==x-1) and (obuolys2==y)) {atsiranda();}
     cord[x-1][y]='x';
     cord[x][y]='-';
     x--;
     foxe();
     cout << x << " " << y;
     Sleep(100);
     if (kbhit())
        {
            char ch=getch();
            if (ch=='w') {goup();}
            if (ch=='s') {godown();}
            if (ch=='d') {goright();}
        }
 }
           }
void goright() {while(!kbhit())
 {

     system("CLS");
     if ((obuolys1==x+1) and (obuolys2==y)) {atsiranda();}
     cord[x+1][y]='x';
     cord[x][y]='-';
     x++;
     foxe();
     cout << x << " " << y;
     Sleep(100);
     if (kbhit())
        {
            char ch=getch();
            if (ch=='w') {goup();}
            if (ch=='s') {godown();}
            if (ch=='a') {goleft();}
        }
 }
            }

void foxe()
 {
     for (painty=0; painty<=19; painty++)
     {
            for (paintx=0; paintx<=19; paintx++) {
                if ((cord[paintx][painty] !='x') and (cord[paintx][painty]!='o')) {cord[paintx][painty]='-';} cout << cord[paintx][painty];} cout << endl;
     }
 }

void atsiranda()
{ cout << "labas!"; Sleep(1000);
    obuolys1=rand()%19;
    obuolys2=rand()%19;
    cord[obuolys1][obuolys2]='o';
}


Apparently, obuolys1 and obuolys2 loses value when they are taken out of int main... what the heck is going on
closed account (3qX21hU5)
I would highly suggest you reread whatever material you have that deal with functions. It seems you don't understand how programs have different scopes. By that I mean if you want to use a variable that is declared in main() in a function you need to pass that variable to the function through a parameter.

Here is a example.

1
2
3
4
5
6
7
8
9
10
11
12
13
int addNumbers(int numOne, int numTwo, int numThree)
{
    return numOne + numTwo + numThree;
}

int main()
{
    int one = 5;
    int two = 10;
    int three = 55;

    cout << addNumbers(one, two, three);
}


What the function is doing is saying whatever variables I pass into numOne, numTwo and numThree I want to add all their values together and return that sum.

So again reread up on functions because I don't think you totally get the idea behind them yet.

Also you cant do this
void goup(), goleft(), goright(), godown(), foxe(), atsiranda();
Topic archived. No new replies allowed.