| Kovs95 (52) | |||||||
Hi! Sorry im posting about this more! Iv'e worked on this some more and now when I debug it, it keeps repeating the "while" loop in the beginning:
The strange thing is that it doesn't re-show the grid that I added at the end of the loop: void showgrid();. After I type in "down", for example, it doesn't check if the move is valid, or move the player position. It just repeats
It seems like the program is skipping over the rest of the code. It was working earlier! I'd appreciate anyone's comments Thanks
| |||||||
|
|
|||||||
| Chervil (1206) | |||||||
main()If you actually want to call the function, do it like this:
To define the function (outside of main() ) do something like this:
See Declaring functions here for an example: http://www.cplusplus.com/doc/tutorial/functions2/ | |||||||
|
Last edited on
|
|||||||
| Kovs95 (52) | |
| When I define the function outside of the main function, it says the variables are undefined because they are all local variables. What do I do if they are local and the void is outside the main? Make then global variables? | |
|
|
|
| Chervil (1206) | |
| Making variables global can sometimes be done. But usually a better solution is to pass as parameters whichever variables the function requires. | |
|
|
|
| vlad from moscow (3662) | |||||||
Your original code is invalid. You think that below there is a function definition
But you may not define a function inside another function. Why does not the compiler issue an error? Because it is not a function definition. At the end of the first line there is a semicolon: void showgrid(); So the compiler considers the code as it consists from two parts: 1) function declaration void showgrid(); and below the declaration some code block
To correct the code you should 1) remove the semicolon 2) declare a function parameter. So the valid code is
This function definition you should place outside main. In main or before main you should place its declaration void showgrid( char grid[][10] ); Maybe there are other errors but I did not look through all your code because it is already enough that your code is invalid. | |||||||
|
Last edited on
|
|||||||
| Kovs95 (52) | |||
Ok, can you check if I passed the variables as parameters? I put the "void showgrid (parameters)" outside the main function and called on it as showgrid (parameters); inside of my functions. The variables are col, row, and grid[7][10]. Do I need to include the variables within the parentheses everytime I call on the function? Also, if I declared the variables, (int row, int col, char grid[7][10]), outside the main function, do I still have to declare them as local variables?Thanks for all your help Chervil! cheers
| |||
|
|
|||
| vlad from moscow (3662) | |||||
|
The compiler shall issue an error. It is an invalid function call showgrid(int row, int col, char grid[7][10]); Instead of using arguments you declared parameters. Also the function definition is invalid. Instead of
should be
| |||||
|
Last edited on
|
|||||
| Kovs95 (52) | |||
|
Ok thanks for all your imput Vlad! Your imput is very appreciated! Since i am new to c++, I am a little confused by your function definition. First, you said, "Instead of using arguments you declared parameters." What do you mean by this? I thought I want to declare parameters in my def.. Second, why did you take out the 7 in char grid[][10]. Since the array is supposed to be a two demensional array of 7 by 10, I thought it's written as grid[7][10].Lastly, I still receive an error that the "function does not take 0 arguments" I made your corrections.
This part char grid appears red and it says "type name is not allowed". Do you know the problem to this?
| |||
|
|
|||
| vlad from moscow (3662) | |
|
One again it is not a function call showgrid(char grid][][10], int rows); Instead of passing arguments you declared parameters. As for the declaration I showed then arrays passed by value implicitly are converted to a pointer to their first element. So all listed function declarations are equivalent and declare the same function void showgrid(char ( *grid )[10], int rows ); void showgrid(char grid[][10], int rows ); void showgrid(char grid[7][10], int rows ); void showgrid(char grid[50000][10], int rows ) | |
|
Last edited on
|
|