• Advertisement

Archived

This topic is now archived and is closed to further replies.

looping

This topic is 5528 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
Urhm.... how about some specifics? If don''t mind burning in hell after you die, you can use a ''goto'' statement, otherwise, use a loop, like so:

while(loop = true)
{
// code here





} // skips back up to while

I''m guessing you already knew this, and what you''re looking for IS actually a goto. However, I think you''ll find that with good code design, you will never need to do that. Post again with what you are trying to accomplish, and maybe some code snippets. That''ll make it alot easier to help you.

By the way I THINK* the way to use a goto is like this:

line_label:
// code here
goto line_label;

...seeing how I don''t use them though, I''m not sure about that. =)

Share this post


Link to post
Share on other sites
goto ... but using it is in most cases avoidable and should be avoided coz'' it can be very confusng and causing errors if not used right. its better to break the code into several functions and classes with functions

what language are u using?

another way would be to put while(){
} around your code... or some other loop     

Share this post


Link to post
Share on other sites
because theres no such thing as ''loop''
i suggest you read some primer tutorials on the net or get a book.

-eldee
;another space monkey;
[ Forced Evolution Studios ]


::evolve::

Do NOT let Dr. Mario touch your genitals. He is not a real doctor!

Share this post


Link to post
Share on other sites
The, probably better, solution, as others have insinuated at is to wrap the code you want to execute more than once into its own function, then call that function as many times you want.

As foor "loop undefined", I guess you tried Beowulf_ as it was. loop is a bool type variable (which of course must be defined) which is tested to see if the block shall be executed or not. If you don't want the block to be skipped, you need to set it to true before the loop. You would also need a way to set it to false to exit the loop.

EDIT: Btw, I think someone needs to take a trip to the book store (hint, hint)




[edited by - CWizard on November 30, 2002 7:23:49 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Beowulf_
while(loop = true)
{
// code here
} // skips back up to while


I sincerely hope that you mean
while(loop == true) 

or, better yet,
while(true == loop) 

Share this post


Link to post
Share on other sites
quote:
Original post by Miserable I sincerely hope that you mean

while(loop == true)


or, better yet,

while(true == loop)



well, beowulf''s method works too..
it wouldnt be useful mind you.. but it makes the loop loop
(it''ll just never end)

note that you can also do this:

while(true)
{
// do stuff
if(youwannaexit)
break;
if(youwannastartover)
continue;
}

though i hear it''s bad practice to break from loops, i dunno why

-eldee
;another space monkey;
[ Forced Evolution Studios ]


::evolve::

Do NOT let Dr. Mario touch your genitals. He is not a real doctor!

Share this post


Link to post
Share on other sites
heres my code it still wont work


  
#include <iostream.h>
#include <cstdlib>
#include <windows.h>
int main()

{
int crap;
int str; //your str

int def; //your def

int lvl; //your lvl

int exptnl; //your exp tnl

int hp; //your hp

int gold; //your gold


int ohp; //ogre hp

int odef; //ogre def

int oexp; //ogre exp

int ostr; //ogre str


int dhp; //dragon hp

int ddef; //dragon def

int dexp; //dragon exp

int dstr; //dragon str


int bohp; //baby ogre hp

int bodef; //baby ogre def

int boexp; //baby ogre exp

int bostr; //baby ogre str

int username;
int password;

int attack;

int loop;
int exit;
int restart;


//you

str=10;
def=10;
lvl=0;
exptnl=100;
hp=30;
gold=500;
//ogre

ohp=50;
odef=50;
oexp=60;
ostr=30;
//dragon

dhp=60;
ddef=60;
dexp=70;
dstr=40;
//baby ogre

bohp=30;
bodef=12;
boexp=30;
bostr=15;

cout<<"Welcome to Fighter Quest!" << endl;



cout<<"You are a young warrior looking for adventure." <<endl;

cout<<"" <<endl;

cout<<"
Your str: "<<str << endl;

cout<<"
" <<endl;

cout<<"
Your def: "<<def << endl;

cout<<"
" <<endl;

cout<<"
Your lvl: "<<lvl << endl;

cout<<"
" <<endl;

cout<<"
Your exp till next level <TNL>: "<<exptnl << endl;

cout<<"
" <<endl;

cout<<"
Your hp: "<<hp << endl;

cout<<"
" <<endl;

cout<<"
Your gold: "<<gold << endl;

cout<<"
Do yo wish to..." <<endl;

cout<<"
1. Go hunt." <<endl;

cout<<"
2. Go to the armory." <<endl;

cout<<"
3. Quit." <<endl;

cin>>crap;

if (crap==1)

{

cout<<"
You chose to hunt." << endl;
cout<<"
You are attacked by an ogre." << endl;
cout<<"
It hits you for 20." << endl;
cout<<"
You faint and then die." << endl;
system("
PAUSE");
return 0;

}
if (crap==2)

{

cout<<"
You chose to go to the armory." << endl;


}
if (crap==3)

{

cout<<"
You chose to quit." << endl;
system("
PAUSE");
return 0;

}

cout<<"
You are at the armory." << endl;
cout<<"
You go inside." << endl;
cout<<"
The clerk greets you." << endl;
cout<<"
You have 500 gold." << endl;
cout<<"
All you can buy is a scimitar." << endl;
cout<<"
It has 8 attack." << endl;
cout<<"
Do you wish to buy it..." << endl;
cout<<"
1(yes) 2(no)..." << endl;

cin>>crap;
if (crap==1)

{

cout<<"
You you buy the scimitar." << endl;
gold=gold-500;
str=str+8;
cout<<"
Your gold: "<<gold << endl;
cout<<"
Your str: "<<str << endl;



}



if (crap==2)

{

cout<<"
You leave." << endl;
cout<<"
You are attacked by an ogre." << endl;
cout<<"
It hits you for 20." << endl;
cout<<"
You faint and then die." << endl;
system("
PAUSE");
return 0;

}

cout<<"
You leave town in hope of adventure." <<endl;
cout<<"
You encounter a baby ogre!" <<endl;
bohp=30;
while(loop==true)
{
attack=str-bodef;
cout<<"
You hit the baby ogre for " <<attack;
cout<<"
!"<< endl;
bohp=bohp-attack;
cout<<"
Its hp: " <<bohp <<endl;
if(bohp==0)
{
cout<<"
You killed the baby ogre!" << endl;
}
if(bohp<0)
{
cout<<"
You killed the baby ogre!" << endl;
}
if(bohp>0)
{
attack=bostr-def;
cout<<"
The baby ogre hit you for " <<attack;
cout<<"
!"<< endl;
hp=hp-attack;
cout<<"
Your hp: " <<hp <<endl;

if(hp=0)
{
cout<<"
You got killed the baby ogre!" << endl;
system("
PAUSE");
return 0;
}
if(hp<0)
{
cout<<"
You got killed the baby ogre!" << endl;
system("
PAUSE");
return 0;
}
if(hp>0)
{
if(exit)
break;
if(restart)
continue;}
}
}

system("
PAUSE");
return 0;
}

Share this post


Link to post
Share on other sites
1. Name your variables properly, or better yet learn how to use classes.

2. You've still got a if(hp=0) in there. You might want to switch your style to typing if(0==hp). This is identical, but if you forget the second equals the compiler will bitch at you for it

3. Google for switch/case structures to correct that sequence of 'crap' tests. Something like:

    
switch(crap)
{
case(0)
{
// stuff

break;
case(1):
{
// other stuff

break;
}
default:
{
// panic?

break;
}
}

(syntax might be a little off, don't trust me on it..)

Edit: I just noticed that your 'loop' is an int, yet you try and compare it to true. Either compare with TRUE and keep it as an int (the nasty MFC way of doing things), or switch it to a proper bool and keep the comparison with true.

[edited by - OrangyTang on November 30, 2002 8:10:50 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Structure of the code aside. loop is a variable that needs a value. Typically for a compiler 0 false and non-zero is true. Anyway you need to set loop with an initial value and when you want it to exit the loop you must change that value to 0 (ie. it is no longer true). Also the end of the program where it restarts seems to be looking for a goto type statement.. what you have written won''t work as you want it to.. the variables again need values initialised and to be set to determine how the if statement will be run.

Share this post


Link to post
Share on other sites

im focusing on why this doesnt loop corectly:


  

cout<<"You leave town in hope of adventure." <<endl;
cout<<"You encounter a baby ogre!" <<endl;
bohp=30;
while(loop==true)
{
attack=str-bodef;
cout<<"You hit the baby ogre for " <<attack;
cout<<"!"<< endl;
bohp=bohp-attack;
cout<<"Its hp: " <<bohp <<endl;
if(bohp==0)
{
cout<<"You killed the baby ogre!" << endl;
}
if(bohp<0)
{
cout<<"You killed the baby ogre!" << endl;
}
if(bohp>0)
{
attack=bostr-def;
cout<<"The baby ogre hit you for " <<attack;
cout<<"!"<< endl;
hp=hp-attack;
cout<<"Your hp: " <<hp <<endl;

if(hp==0)
{
cout<<"You got killed the baby ogre!" << endl;
system("PAUSE");
return 0;
}
if(hp<0)
{
cout<<"You got killed the baby ogre!" << endl;
system("PAUSE");
return 0;
}
if(hp>0)
{
}


it stops at:

  cout<<"You encounter a baby ogre!" <<endl;  

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
that is because loop probably automatically has some garbage value in it. ie.. it doesn''t equal true and it never starts the loop.

Share this post


Link to post
Share on other sites
>>I sincerely hope that you mean
>>while(loop == true)
>>or, better yet,
>>while(true == loop)

Doh! Yeah, you know what I meant... the code wasn''t meant to be taken as-is. It was just a quick example. (why true == loop? I''d be very interested to hear the reasoning behind that one)

To the original poster:

For the love of god GET A C++ book! It''s like a child trying to plot the trajectory of a nuclear missile using crayons. Your code, no offense meant, is a tangled rat''s nest. If you don''t know what a loop is, or how it works, you shouldn''t be attempting any sort of major undertaking. I realize you want to get programming as fast as possible, but you can''t out-judo Jackie Chan until you know karate.

I''m including a link that has a zip file containing my first console-fighter type game. Maybe that will give you an idea what you''re up against. I still fudged alot, and only got as far as the first battle, but hey, it was my first attempt, give me a break.

http://www.geocities.com/mb_jacobson2002/disk_drive.html

... then go to Console Fighter Source. It''s about 3MB. Comments are welcome.

Share this post


Link to post
Share on other sites
To the original poster, you really need to clean up your code. For one thing, your variable declarations take up way too much space. For example, instead of saying:

int x;
int y;
int z;
...

You could just say:

int x, y, z;

OR better yet, you could initialize them all on one line like this instead:

int x = 0, y = 3, z = 2;

That would considerably shorten down the size of your program(code line wise) and make it easier to read.

And you can also read up on those case statements, it makes work alot easier both to work with and to read/understand.

[edited by - Apocalypse_Demon on November 30, 2002 8:49:04 PM]

Share this post


Link to post
Share on other sites
not to mention, several times he uses cout ineffectively
cout << "this is my text" << endl;
cout << "" << endl;

you dont need to put the quotes in the second line there,
and moreover- you can better format your stuff like so:

cout << "this is what i will put on line 1" << endl <<
"this is what i will put on line 2" << endl <<
"this is what i will put on line 3" << endl <<
"and so on. it's much easier to read this" << endl <<
"way, no?" << endl << endl; // if you need to make an extra line break, just stick one in there.


[edit: code tags for formating]

-eldee
;another space monkey;
[ Forced Evolution Studios ]


::evolve::

Do NOT let Dr. Mario touch your genitals. He is not a real doctor!

[edited by - eldee on November 30, 2002 9:04:24 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by eldee
though i hear it''s bad practice to break from loops, i dunno why

It''s bad form. Usually it''s much more "structured" to have some other means of breaking out of loops.

But frankly, I don''t see what''s SO bad about it

And to the original poster of this thread, you should learn about functions, reading from files and arrays. It would considerable untangle your code ^^;

Share this post


Link to post
Share on other sites
quote:
Original post by Beowulf_
>>I sincerely hope that you mean
>>while(loop == true)
>>or, better yet,
>>while(true == loop)

Doh! Yeah, you know what I meant... the code wasn''t meant to be taken as-is. It was just a quick example. (why true == loop? I''d be very interested to hear the reasoning behind that one)


Someone already explained it, but it''s such a neat trick that it deserves reiterating. Actually, it''s a guard against human error. If you test (loop == true) and accidentally omit the second =, you''ll get (loop = true) which will assign true to loop and return true. If you switch the order and accidentally type (true = loop), you''ll get a compile time error (you can''t assign a value to a constant) instead of a potentially hard-to-track runtime bug.

(Obviously, this only works when comparing a variable to a constant, and most of the time you won''t make this mistake, but it''s a good habit that will save you from subtle errors every once in a while, and it doesn''t cost you anything, so why not?)

Share this post


Link to post
Share on other sites
quote:
Original post by Miserable
Actually, it's a guard against human error. If you test (loop == true) and accidentally omit the second =, you'll get (loop = true) which will assign true to loop and return true. If you switch the order and accidentally type (true = loop), you'll get a compile time error (you can't assign a value to a constant) instead of a potentially hard-to-track runtime bug.
I agree that it can be a guard, but I would never use it myself. Usually the expressions have a reason of being either on the left or right hand, which is based on how you view the comparison. When you compare, you say "is <left_hand_value> equal to <right_hand_value>?". In this case you ask if true equals something, which doesn't make sense, as we all know what true equals. It is syntaticly correct, but it isn't how most people read it.

EDIT: Btw, I don't think I ever made the mistake of using "=" instead of "==" when comparing.




[edited by - CWizard on November 30, 2002 10:24:56 PM]

Share this post


Link to post
Share on other sites

  • Advertisement