Sign in to follow this  
supercrazy7474

need help..

Recommended Posts

Hello all,I am new to gamedev. Could someone read this over and come out with an answer:
[Source]#include <iostream>//for cout
using namespace std;
class first
{
    public:
void getinput(int x = 1, int y = 1, int mapx = 2, int mapy = 2)/*use keypresses to see where the user wants to go*/
{
   if(void KeyPress(short* 119))/*if user enters a w(west)...*/
   {
       x = x - 1;/* then use ints to go west...*/
}
   if(void KeyPress(short* 101))/*if its an e(east)...*/
   {
       x = x+1;/*then use ints to go east...*/
}
   if(void KeyPress(short* 110))/*if its an n(north)...*/
   {
       y = y+1;/* then use ints to go north...*/
}
   if(void KeyPress(short* 115))/*if its an s(south)...*/
   {
      y = y-1;
  }   if(void KeyPress(short* 120))/*if user wants to exit...*/
  {
      return 0;/*exit*/
}
}      

int main()
{
    class second : public first
    {
   void other()
   {  switch(x)/* make sure user is inside map */
     {
     case x>mapx:
         cout<<"cannot go that way.go another way.";
         x = x-1;
         void getinput(int x = 1, int y = 1, int mapx = 2, int mapy = 2);
         break;
         
     case x<mapx:
         cout<<"cannot go that way.go another way.";
         x = x+1;
         void getinput(int x = 1, int y = 1, int mapx = 2, int mapy = 2);
         break;
         
         default:
             cout<<"moved sucessfully.you are now at:("<<x<<","<<y<<")";         
             break;   
     }
     switch(y)
     {
         case y>mapy:
             cout<<"cannot go that way.go another way.";
             y = y - 1;
             void getinput(int x = 1, int y = 1, int mapx = 2, int mapy = 2);    
             break;
             
             case y<mapy:
                 cout<<"cannot go that way.go another way.";
                 y = y + 1;
                 void getinput(int x = 1, int y = 1, int mapx = 2, int mapy = 2);
                 break;
                 
                 default:
                 cout<<"moved sucessfully.you are now at:("<<x<<","<<y<<")";
                 break;
             }
       }
     }
};[/Source]
For some reason,I get this stupid error:Line:73 Syntax error at end of input.[bawling]

Share this post


Link to post
Share on other sites
Argh.Still Doesnt work.What is wrong now?
#include <iostream>//for cout
using namespace std;
class first
{
public:
void getinput(int x = 1, int y = 1, int mapx = 2, int mapy = 2)/*use keypresses to see where the user wants to go*/
{
if(void KeyPress(short* 97))/*if user enters an a(west)...*/
{
x = x - 1;/* then use ints to go west...*/
}
if(void KeyPress(short* 100))/*if its an d(east)...*/
{
x = x+1;/*then use ints to go east...*/
}
if(void KeyPress(short* 119))/*if its an n(north)...*/
{
y = y+1;/* then use ints to go north...*/
}
if(void KeyPress(short* 115))/*if its an s(south)...*/
{
y = y-1;
} if(void KeyPress(short* 120))/*if user wants to exit...*/
{
return 0;/*exit*/
}
}

int main()
{
class second : public first
{
void other()
{ for(;;)/* make sure user is in map*/
{
void getinput(int x = 1, int y = 1, int mapx = 2, int mapy = 2);
if(x>mapx)
{
x = x-1;
cout<<"cannot go that way.go another way.";
continue;
} else if(x<mapx)
{
x = x+1;
cout<<"cannot go that way.go another way.";
continue;
} else if(y<mapy)
{
y = y+1;
cout<<"cannot go that way.go another way.";
continue;
} else if(y>mapy)
{ y = y-1;
cout<<"cannot go that way.go another way.";
continue;
}
cout<<"moved sucessfully.you are now at:("<<x<<","<<y<<")";
}
}
}
}[/Source]
I would also like it if someone made a modification of the end that worked.

Share this post


Link to post
Share on other sites
Dude, there is no such thing as void KeyPress( short* 97 ).

This is how a function is declared:
void KeyPress( short ps )

And this is how you call it:
KeyPress( some_val )

If you want to call it inside an if-statement:
if ( KeyPress(some_val) )

But if you want to do that, the function must return a bool or int, instead of void. I think you need to read more books or tutorials about functions.

Share this post


Link to post
Share on other sites
To the OP: I think you're getting ahead of yourself. I recommend starting small and taking baby steps. Write small programs demonstrating the uses of various language features and then move on to other features... and continue;

Ryan

Share this post


Link to post
Share on other sites
if(void KeyPress(short* 119))


I assume you have been given (via a library) a function declared so:

void KeyPress(short* something)


Let's break this down.

The first word, "void", is the return type of that function. In this case, there is no return value. However, you will get output from it in a different way...

"KeyPress" is the name of the function.

Inside the brackets, we have a list of arguments to the function. In this case, there is one input, a pointer to a "short" (2-byte integer - that's not really accurate, but good enough for explanatory purposes) value. Here "something" is the name used for that input within the function. The compiler couldn't care less if you have a variable named "something" in your own code; that is a completely different variable.

When you want to use a function you "call" it. Syntax for calling this function would be:

KeyPress(foo)


where "foo" is a pointer to a short value. Notice you do not put the return type or the type of the input value.

So. Now we are faced with some problems:
1) We need to have a pointer to a short value, and it has to point to a valid short value.
2) We need to find out, given what KeyPress does, whether the key pressed is 119. Except we are not going to write the number 119 in our code; we are going to write 'w', which is translated into the number 119 (stored in a "char" value, which is not the same as a short, but will be implicitly converted so we're OK).

The easiest way around the first problem is to have a local variable which is a short value, and take its address, which is a number that indicates where the thing is located in the computer's memory. That number is a pointer-to-short; that's how pointers work. So that fragement looks like this:

short key;
KeyPress(&key);


Notice the '&'. That's the address-of operator that you need here.

Ok, so now we have the second problem, of using the value. Like I said, "KeyPress" doesn't return anything. But via the pointer, it can change the value of our local "key" variable, because it knows where that is located in memory. (In C++, it is preferred to do things like this in a different way, using "references" rather than pointers. However, in C++ you often have to work with old APIs that were originally intended for C...) And this is what's going to happen (I don't know what API you're using, but let's just say for now that I'm psychic [wink]): After you make that KeyPress call, the key variable will store the numeric code for the key. So now we can compare it:

if(key == 'w') {
// do stuff!
}


Now, things get a little more involved. We don't really want to call the KeyPress function four times to check each direction, because it's just going to tell us the same thing each time (probably). And having several "if" statements that all look the same suggests that we might want a "switch" construct instead. Oh, and as for this business with the "class first" and "class second : public first", I think you are very confused. I'm going to cut that bit out for now, because it's not helping at all. And finally, remember that the calling code needs to see the changes to our variables; otherwise those results just get thrown away.

So now we have:


void getinput(int& x = 1, int& y = 1, int& mapx = 2, int& mapy = 2) {
// Notice the "&" symbols on the input types; this means we
// are "passing by reference", which is the C++ idea I
// mentioned above. The calling code will get its variables
// changed by what happens here.

// First, read a key.
short key;
KeyPress(&key);
// Now, figure out what to do.
switch(key) {
// By writing single letters in single quotes, it becomes
// a lot more obvious what the numbers are supposed to mean.
case 'w': // west
x = x-1;
break; // this is important when you use switch statements
// This construction is designed to let you do some weird
// things that you usually won't want to do. For now,
// just follow the rule to always put the "break" at the
// end of a "case" in a "switch".
case 'e': // east
x = x+1;
break;
// I think you had these two mixed up. Usually people write
// things so that y increases when you go *down*, as shown
// here:
case 'n': // north
y = y-1;
break;
case 's': // south
y = y+1;
break;
} // otherwise, do nothing.
// You cannot "return 0" from a void function. There is nothing
// to return.
}



YOUR ASSIGNMENT:

1) Fix the main() code. Remember what I said about how to call functions, and get rid of the "class second" and "void other()" bit, it's complete nonsense in that context. Oh, and make sure that there are some variables inside main() so that you can pass them to getinput(). The 'x', 'y' etc. declared in getinput() are completely different from any variables mentioned in main(). You can use completely different names in main() if you like, and the function call will basically "translate" between the variables.

2) Make it so that getinput() will do the checking. Have it return a "bool" value (either false or true) according to whether or not the user could move in the requested direction, and only change the x/y if the move is valid. In the main(), check the return value and output the appropriate message. Notice that now you only have the "failure" message written once. This is a very good thing.

Share this post


Link to post
Share on other sites
ok... all i want you guys to do is help me figure out why there is a syntax error at end of input. Thats the only error.
#include <iostream>//for cout
using namespace std;
class first
{
public:
void getinput(bool redo = false; bool check = false; int x = 1, int y = 1, int mapx = 2, int mapy = 2, char input)/*use keypresses to see where the user wants to go*/
{
cin>>input;
switch(input)
{
case 'w': // west
x = x-1;
break;
case 'e': // east
x = x+1;
break;

case 'n': // north
y = y+1;
break;
case 's': // south
y = y-1;
break;
case 'e':
check = true;
break;

default:
redo = true;
break;


}
}

int main()
{
class second : public first
{
void other()
{ for(;;)/* make sure user is in map*/
{
void getinput(int x = 1, int y = 1, int mapx = 2, int mapy = 2);
if(redo==true)
{
continue;
}
if(x>mapx)
{
x = x-1;
cout<<"cannot go that way.go another way.";
continue;
} else if(x<mapx)
{
x = x+1;
cout<<"cannot go that way.go another way.";
continue;
} else if(y<mapy)
{
y = y+1;
cout<<"cannot go that way.go another way.";
continue;
} else if(y>mapy)
{ y = y-1;
cout<<"cannot go that way.go another way.";
continue;
}
cout<<"moved sucessfully.you are now at:("<<x<<","<<y<<")";
if(check = true;)
{
return 0;
}
}
}
}
}

Share this post


Link to post
Share on other sites
Quote:
Original post by supercrazy7474
zahlman:I know about the pointers and things like that.


No, you clearly don't, and you clearly haven't been listening, based on your most recent source dump:


#include <iostream>
using namespace std;
// Code has been reformatted to my preferred indentation style. It is mostly
// a matter of preference, but you do need to pick *some* style and stick to it.
class first {
public:
void getinput(bool redo = false; bool check = false; int x = 1, int y = 1, int mapx = 2, int mapy = 2, char input) {
// Those semicolons need to be commas.
// Also, variables don't work anything like what you expect them to. When you
// put them all between the ()s, you are saying that the caller is going to
// provide all those variables.
// Also, because you pass by value, none of the changes to any of those
// variables can be seen anywhere else in the program.
// Also, "mapx" and "mapy" are unused here.
cin>>input;
switch(input) {
case 'w': // west
x = x-1;
break;
case 'e': // east
x = x+1;
break;
case 'n': // north
y = y+1;
break;
case 's': // south
y = y-1;
break;
case 'e':
// You have two 'e' cases; that's no good.
// It seems like you want to use this case to quit the game;
// so you'll want to pick a different letter for that command.
// Perhaps 'q'?
check = true;
break;
default:
redo = true;
break;
}
}

int main() {
class second : public first {
// Declaring a class within a function makes no sense at all.
void other() {
// Functions can't nest like this in C++.
for(;;) {
void getinput(int x = 1, int y = 1, int mapx = 2, int mapy = 2);
// This is not a function call, it is a function declaration.
// Also, it declares a function different from the one above.
// You put your previous "getinput" inside a class, so it is a method
// of "first" objects rather than a free function.
// That means that in order to use it, you have to create a "first" object
// first, and then call its "getinput" method.
if(redo==true) {
// The 'redo' variable isn't available here, the way you've set things up.
// Neither are any of the others.
continue;
}
if(x>mapx) {
x = x-1;
cout<<"cannot go that way.go another way.";
continue;
} else if(x<mapx) {
x = x+1;
cout<<"cannot go that way.go another way.";
continue;
} else if(y<mapy) {
y = y+1;
cout<<"cannot go that way.go another way.";
continue;
} else if(y>mapy) {
y = y-1;
cout<<"cannot go that way.go another way.";
continue;
}
cout<<"moved sucessfully.you are now at:("<<x<<","<<y<<")";
// You should add an <<endl at the end of all of your cout lists.
if(check = true;) {
// You want == rather than =, and you don't want that semicolon there.
return 0;
}
}
}
}
}
// You have an extra close bracket, which is the cause of the "parse error
// at end of input". You have lots of other bizarre problems, and frankly I
// don't know why your compiler isn't reporting anything. Maybe it's too
// confused, or just wants you to make the brackets balance first.
// Choosing a style of indentation and applying it consistently will make it
// much easier to detect this sort of problem.


Share this post


Link to post
Share on other sites
Zahlman: // Declaring a class within a function makes no sense at all.
Just a bit nitpicking, sorry, but there cases in which declaring a class inside a function makes perfect sense (e.g. emulating local functions, using templated algorithm with custom predicates, ...) [smile].

Cheers,
Pat.

Share this post


Link to post
Share on other sites
Hello all,thanks for your help. I think that im going to quit on this project for a little while, I am going to work on something else right now. I might come back to this with another format of code, because as you can see mine is not the best you have seen.[lol] I am a very new person to c++, this is my first time with an actual use of classes.

Share this post


Link to post
Share on other sites
Quote:
Original post by darookie
Zahlman: // Declaring a class within a function makes no sense at all.
Just a bit nitpicking, sorry, but there cases in which declaring a class inside a function makes perfect sense (e.g. emulating local functions, using templated algorithm with custom predicates, ...) [smile].

Cheers,
Pat.


No problem, I expected to be corrected/enlightened somewhere or other. Didn't know you could do that in C++, although I can imagine the uses - I suppose it could a be decent scoping trick in the right hands. I've certainly never seen it. :\ Anyway, better to keep things simple for the n00b eh? Classes aren't yet even really relevant to the design...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this