trying to return to the beginning of the program....

0 John Tompson · July 29, 2015
i have studied this c language and find no way to get BACK to the beginning of a program.

create character hero
walk around {infinite until life==0}
[don't want the program to end after making 1 step into a dungeon]: need to explore the rest of the map. data 1,1,2,1,1,1,1. read data. get a$. map(x,y). A$="hero's name"

need to return to the walk around section.
500 lines of code and 3 other loops separate needing to return to this section.
don't know how to do this without a "goto 10": ?"this is not possible with the 'continue' command...": A$="this c language is not that good of a language and takes more space (?print){}()"" to write and in program size)".

?"do you know how to solve this problem?";get how$

::i don't see how it is possible to fit 500 lines of code and 3 separate loops within a single "do while" or "if else" statement.
when a sample was run for mixing a for and if statement together for example, it completely ERASED the values of all of my variables within these brackets........



Post a Reply


- page 2
Oldest  Newest  Rating
0 Jon Z. · August 16, 2015
I wouldn't put the entire program in a loop, but I'd put there the main structure, and the then call functions for different things.
0 c student · August 13, 2015
yes, there are many things in the c language.  you should probably get familiar with the more advanced data structures which used structs so that you can manage your program more effectively. 

i split the dungeon area into a square section which was inside the overall map since it might've been easier to just label locations instead of having multiple locations because it might be too much work to implement.  the labels DUNGEONXX referred to coordinates of the map (assuming it's a square).  you can create different map areas if you wish to, however that means you need to keep the player in check like removing a character from a map and loading them into the new map.

using void main is possible however it's a standard to have main return int, hence int main.  other than that, you can use void in any other functions, meaning it doesn't return any values.

to get back to the beginning of the program, you would just need a while loop.  however, in your case, it might seem like you will require an immense amount of nested loops and i might just suggest you to use goto to simplify your life.

a function which uses empty parenthesis, e.g. functionOne() takes in no parameters.

typedef is just setting your own data type the same as another, e.g. typedef short position just means that position is a type short.  short is a data type which is 16 bits or 2 bytes.  an int is 32 bits or 4 bytes.

a variable declared like **var means it's a pointer to a pointer.  something that resembles a 2d array.

the arrow -> just means a pointer to.  e.g. Player->posX means a Player points to posX.

just a quick question, have you seen the source code to any games?  it might be helpful if you knew how a game was written before writing one yourself.
0 John Tompson · August 13, 2015
hey you, c student?
glad to make your aquaintance.

yes, after i posted the question, i started to think about it and realized that splitting it into sections, such as by calling header files.h into the program to help to simplify it.
before that -- yes -- i had been planning to do ALL of the program inside the main.

interesting, you are suggesting breaking up the map into 4 sections?
my original program only has 1 section. though in my studies i have noted that this could be useful for, say, big maps; and is used in making drawing programs.
though i'm not sure why they would break it up into 4 parts. what is the purpose of breaking it up into 4 parts.
but other than that question, i have the map part figured out: x=x+1, y=y+1.

so your using a struct command to store all of the main character stats instead of like, lets say, a save character option? open data. write data file. 
never thought of it being used like that. i thought the ONLY purpose of using a "struct" was so that you could use integers and whole numbers together {especially in unknown instances when they are not compatible
{float and int}.

what a good idea -- i never thought about using a "void" to maintain the current map location of the character. i didn't even know that that was possible -- that the programming language allowed you to do that!!!!

so when you say "#define DUNGEONLEFTX", this function would be x=x-1? this is what you mean, or would there be more involved in this function that you create?

you can void the main? i didn't know that was possible. what does that do? i don't understand. the program wouldn't run.
so your saying that the program doesn't run "until you enter a input from the keyboard" per say. i guess that makes sense. i want turn based, or await input from user.
however, i noticed that you used "int main (void) {",  not void main..... umm, you can only use the void command when it is IN FRONT of the function? you can use it differently?

but if this, what you say IS possible, then i wouldn't need to get back to the beginning of the program, because the entire program {in the main} is simply to fetch a keyboard input?
which means that the solution would be a simple for command to solve it as in. for i = 1 to 1000000. for (turns=1; turns <=1000000; turns++).
otherwise, the problem still remains of getting back to the beginning of the program (if this doesn't work).

i thought the int assigning had to be to a single word such as, "int what". but your saying that i can assign int to, "int say what". {referring to your "int position posX;" code assignment}.
again, the use two words? the c program allows you to do this "player p": "void updateMapArea (Player p)"?

i never did understand what it meant to simply leave a ( ) blank. logically it doesn't make sense. even basic functions have them: int(RND(1)*6).  rand()%6 ) + 1. your example: "initializePlayer();".
i take it that the "()" is filled in later? otherwise, i don't understand how this works. is it like assiging that a variable exists, but give it no value, such as int value (instead of int value=5).

interesting, while (1)? you can do that? so that would be "p" right, as in while player 1? didn't know the function allowed you to do that. thanks bro.

never heard of "typedef". and i thought that i knew ALL of the commands in c.....
i'm assuming its something like assigning a variable a class, something like float, int, or char?
also, never heard of "short" -- this is a c command?

*player means pointer right? never heard of assigning a "struct" to a variable pointer (ampersand).
i thought the only kind of structure that a struct could make was only limited to the float, int, or char classes!

i have never heard of the **variable. i know that *variable, means the variable pointer, but two "*" asterisks?...
not sure what this does.

i never understood why you'd want to ampersand a variable to find its address: both inputs or methods return the same value!
it doesn't make sense how you could use this. the address doesn't do you any good unless you are going into the binary langauge of 0101110.

i know that there is a >= sign, but your saying there is a -> sign? never heard of it. didn't know this was a command. it is a command: "->"?
0 John Tompson · August 13, 2015
hi jon z! hows your day? :)
thanks for replying back to me.

i've tried the continue command and it seems very limited. continue only works if it is within a loop. not only that limitation, but the fact that the continue command cannot be used with all of the loop functions.
the only one so far that continue has been successful, is the loop of "for". when using the continue command, it simply goes to the next line, not the beginning of the program. but the main problem with the "continue" is that ALL 500 lines of my program would have to be within a single loop (to be able to use the continue command to get back to the beginning of the program). not only that, when the continue only goes a line forward, not a line in front of when it was given in the code.

and as far as the break command goes -- now that command is equally as useless. i only thing that i have been able to use the break with is the do while loop. and then when finished, it simply goes to the next line, not the needed several lines before the command was given. the c language really has a lot of difficulties it seems. quite bulky and not all that great.

however, what i can do is to try more examples and methods of how to use continue and break, and maybe i might find something that can work, i don't know.
yet, it does not seem like it will be able to solve the get me back to the beginning of the program delimma.
[the author is furrowing his forehead together as he thinks about putting all 500 lines of a program within a SINGLE loop]...
<it sounds like many errors waiting to happen>

but if you think this is what i should do, as what you suggested, i will think more on this matter

thanks for trying to help
0 Jon Z. · July 29, 2015
Probably use a loop.

//use 'continue' to skip the rest of the code in the loop and jump to the beginning

//use 'break' to exit the loop
0 c student · July 29, 2015
are you doing all 500 lines inside main?  if so, i'd strongly suggest you split your code into functions and into smaller functions again if it's appropriate.  call the required function within main and loop that.  something like this? (assume map origin (0, 0) from bottom left):

#define SUCCESSFUL 1

#define DUNGEONLEFTX ...
#define DUNGEONTOPY ...

typedef short position;
typedef struct player *Player;

struct player {
   char *name;
   short level;
   int **inventory;
   position posX;
   position posY;
   char *mapArea;

Player playerOne;

//update player position on map
void updateMapArea (Player p) {
   if ((p->posX >= DUNGEONLEFTX && p->posX < = DUNGEONRIGHTX)
        && (posY >= DUNGEONBOTTOMY && p->posY < = DUNGEONTOPY)) {
       p->mapArea = "Dungeon";
   } else if ...


// update player position
int walk (Player p, position x, position y) {
   p->posX += x;
   p->posY += y;
   updateMapArea (p);
   return SUCCESSFUL;

int main (void) {
   initializePlayer();    // set values into player
   while (1) {
       switch (keyboard) {    // fetch key pressed from keyboard
           case 'w':
               walk (playerOne, 1, 0);    // move up

not sure if this helped.  please provide an example or explain further.
  • 1
  • 2



One of the most popular languages of all time.

Bucky Roberts Administrator