Working with strucures

guys first sorry for sparing your time with me.I need to learn from you how to use structure cause i allways try and nothing.Errors and errors.I will post my try.5 errors.Ty

#include<iostream.h>
#include<conio.h>
class Game
{
private:
int x,y;
char ch;
public:
void initializare()
{
x=0;
y=0;
}
void miscare()
{
while(ch!='\r')
{
ch = getche();
switch (ch)
{
case 'w':x++;
break;
case 's':x--;
break;
case 'a':y--;
break;
case 'd':y++;
break;
default:cout<<"wrong choice(w/s/a/d)";
}
cout<<"You are at "<<x<<" And "<<y<<endl;
}
}
}
int main()
{
Game f1;
f1.initializare();
f1.miscare();

}
and now the error log:
prog.cpp:36: error: new types may not be defined in a return type
prog.cpp:36: note: (perhaps a semicolon is missing after the definition of `Game')
prog.cpp:36: error: extraneous `int' ignored
prog.cpp:36: error: `main' must return `int'
prog.cpp:36: error: return type for `main' changed to `int'
prog.cpp:41:2: warning: no newline at end of file

ty for your time guys.
class Game
{
...
};

You forget semicolon...
Last edited on
Here is the code cleaned up with line numbers so we can talk about it:

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
#include<iostream.h>
#include<conio.h>

class Game
{
private:
	int x,y;
	char ch;
public:
	void initializare()
	{
		x=0;
		y=0;
	}
	void miscare()
	{
		while(ch!='\r')
		{
			ch = getche();
			switch (ch)
			{
			case 'w':
				x++;
				break;
			case 's':
				x--;
				break;
			case 'a':
				y--;
				break;
			case 'd':
				y++;
				break;
			default:cout<<"wrong choice(w/s/a/d)";
			}
			cout<<"You are at "<<x<<" And "<<y<<endl;
		}
	}
}
int main()
{
Game f1;
f1.initializare();
f1.miscare();

}


Line 1: instead of "#include <iostream.h>" use the C++ convention of "#include <iostream>"

Line 39: End the class definition with a semicolon (replace "}" with "};").

Line 45: main must return an int. So add "return 0;"

Then reply back with your new error log.
JMJAtlanta:
Line 45: main must return an int. So add "return 0;"

The standard:
If control reaches the end of main without encountering a return statement, the effect is that of executing return 0;

Last edited on
add "using namespace std" since you didn't specify the namespace of "cout"
You may also consider using "_getche()" to get rid of a warning in VC++.
Last edited on
Topic archived. No new replies allowed.