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.
EXAMPLE:

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"

PROBLEM
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........

sincerely,

stuck

Post a Reply

Replies

- page 1
Oldest  Newest  Rating
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 DUNGEONRIGHTX ...
#define DUNGEONTOPY ...
#define DUNGEONBOTTOMY ...

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
               break;
           ...
       }
       ...
   }

not sure if this helped.  please provide an example or explain further.
0 Jon Z. · July 29, 2015
Probably use a loop.

while(1)
{
//use 'continue' to skip the rest of the code in the loop and jump to the beginning
if(...){
continue;
}

//use 'break' to exit the loop
if(...){
break;
}
}
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: "->"?
/images/forum/upload/2015-08-13/1586a5affad917816aae953fe5768902.png
john
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

sincerely,
thanks for trying to help
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 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 John Tompson · August 16, 2015
c student,
hey bro, what's up man? nice to see you again!;)
/images/forum/upload/2015-08-16/1c3c8405fb923ca9e488233627afb6f4.jpg
no, i haven't seen a game written in c. how do i find one? i didn't know that it was possible to get this, as games are usually protected with some kind of block to prevent you from looking at it. i don't know how to unblock it to look at one. but on other languages, that is how i got good at the language, was by looking at other people's code. however, not as much in the new languages (or c) is available.

how do i "get familiar with the more advanced data structures which used structs"? -- i have done all the tutorials and logged the definition and HOW to make one. so your saying structs are similar to functions, in that they keep all of a set of variables together into a cluster and make it easier to implement this set of variables into the program? i didn't know that you could do that -- if that's what it does. if i am wrong, then in a single sentence, how would you describe what the struct does. my sentence and understanding currently about struct and what it does currently is: 
"a struct is only used to combine floats and ints together so that they can be compatible with eachother when normally you might not be able to use them together." 

what do you mean its "too much work to implement. the labels DUNGEONXX"? how so? (and by the way, i thought that it was DUNGEONX [DUNGEONLEFTX], not DUNGEONXX, as a double array with 2 "X"'s, because by "double array", you meant 1 array with x and array 2 with y, not a double array for x, right -- or was this just a typo?). wouldn't it just be a simple coordinate change such as x=x+1, now x=99,y=5 (map). it sounds to me like this is what you did: you took the entire game map and divided into 4 equal parts (4 squares, as the entire map is a square). so you mean "easier" because you have to worry about a long array size or split it into a bunch of arrays for x (and the same for y).

currently my map is a giant long rectangle because it was easier to program and to look at later (look at the code later, as it (each x row) could be see on a single line and not scattered everywhere, making it difficult to see the map in the code). but i just got the idea, that i can create many more of these giant long rectangles for each room and still make the code simple to program.

so in the actual code, this is exactly how you would use the void main command: "int main (void) {"?. note the use of the ( ) brackets around void, and the possible confusion that i might have as to its use: as i have never seen this before. note: i am not saying that its not possible, just that i have not seen it. i thought void was only to assign functions. so this would mean that main becomes a function? that is confusing: i don't understand what this does (main void) that is different from a normal function or WHY you would use this instead of a "normal funtion". how does this help you doing it differently [i.e. main void instead of void function]?

you said, "use goto to simplify your life." does this mean that you can use the "goto" command in the c language? currently my program is written in another programming language. i am going to convert the program from one language to another . the "goto" command that i suggested, is what you can use in another language, but as far as i know it is not even available in the c language, nor is there anything like it.....

"a function which uses empty parenthesis, e.g. functionOne() takes in no parameters." okay, didn't know that. that's strange why they write this programming language this way then!

so what is the purpose of using a "typedef": is it to assign less bits (space) to a variable to make your program smaller (and therefore take up less space and run faster)?

a pointer pointing at another pointer? dude, i didn't know that you could do that. and you use this to make a double array. using a pointer method with this computer language is a more convulted way of using arrays then other programming languages! [example: "**pointer point to map(x,y)" instead of "map(x,y)". i've noticed that this language takes up an enormous more of an amount of size than the programs have to be with all of this additional unnessary stuff added to simple functions], (i.e. the programs run slower).

"Player->posX": i don't understand how to use this. unless, "->" is an actual c command? and then this statement, "p->posX += x;" (and "y") is saying "i am referring to player 1(p1) in the program, if p1is not equal to the current location, then posX(y) will be the new location"? in the actual code, this is how you write it, "p->posX += x;"? or maybe if player = x5, then the pointer posX = 5, so you are assigning the value of posX to the pointer player(p) then? i've only seen this type thing done before with an ampersand& or *pointer (note the confusion).

so this is what the actual code would look like (or that it CAN be written this way, in this format): "int position posX;"? and this assigns a value to the variable, "position posX"? note: is this 1 value or a single variable with two values, or a single int assignment with two separate variables assigned to 1 int? and your saying that the c language programming has the same format, like in the command of void, where two variables can be  assigned to a single void [function], as in "void updateMapArea (Player p)"? [note: the two variables are, 1 variable (updateMapArea), and 2 variable ((Player p))].

sincerely,
yo bro
0 John Tompson · August 16, 2015
hi jon,
thanks for getting back to me.
how you been?

i thought putting all 500 lines in a single loop was your idea on HOW to get back to the beginning of the program?

and by "call functions", you mean #include header files.h, or voids?

so you were suggesting to use the continue and break functions. have you used them yourself in programming? have you gotten them to work in all of the loops. and if so, do you have a small example program of how to use them in the other loops (i'm not talking about using break in a do while [in other loops such as for or if else], nor am i talking about using the continue with the for loop [but the other loops such as if or do while]).
if you know how to do this [IF ITS POSSIBLE], then this would be really useful to me.

thanks,
friend john

/images/forum/upload/2015-08-16/04fd091c00cd2bb28c527a060edab58d.jpg
+1 Jon Z. · August 21, 2015
Not necessarily header files.  You can have the functions in the same file. Or you can have the functions in different files if you have a lot of functions and want to keep them more organized. 

Look at the following example (it's not proper C code, it's pseudocode).

void fight(){
//fight
life--;
exp++;
...
}

void run(){
//run function
energy--;
...
}

void takeANap(){
//take a nap
energy++;
...
}

int main(){
while(1){
 //ask what to do
 if(toDo == fight){
  fight();
 }
 else if(toDo == run){
  run();
 }
 else if(toDo == takeANap){
  takeANap();
 }
 else if(toDo == exit){
  break;
 }
}
return 0;
}


In the example they are all 1 big if statement, but if it wasn't you could use continue; to go back to the beginning of the loop.

The point is that this way you keep your main function simple and clean. And most of the code is in the functions.
0 c student · August 21, 2015
there seems to be a lot of things you don't understand about the language.  perhaps it would be better if you learned more before approaching a task of this caliber?
  • 1
  • 2

C

107,042 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator