need help..

Started by
12 comments, last by Zahlman 19 years, 4 months ago
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]
I pity the fool that uses numerical operators as boolean evaluators!
Advertisement
You can't use boolens in switch statments.

Use nested if statements instead.
If(x>mapx){  //code one}else if(x<mapx){  //code two}else{  //default code}
ok thanks,I have also noticed some bugs in it that I will fix.
I pity the fool that uses numerical operators as boolean evaluators!
Argh.Still Doesnt work.What is wrong now?
#include <iostream>//for coutusing 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.
I pity the fool that uses numerical operators as boolean evaluators!
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.
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
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.
zahlman:I know about the pointers and things like that.Thanks for the code and the tips on the bottom of your post though. I will use that.
I pity the fool that uses numerical operators as boolean evaluators!
argh!your idea isnt working either.I think that if I go with my idea i can fix it.I am also using a cin instead of a keypress.
I pity the fool that uses numerical operators as boolean evaluators!
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 coutusing 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;     }                            }            }     } } 
I pity the fool that uses numerical operators as boolean evaluators!

This topic is closed to new replies.

Advertisement