looping

Started by
28 comments, last by craphead 21 years, 4 months ago
I think it would be nice to see you posting here <------ CLICK HERE.
[size="2"]I like the Walrus best.
Advertisement
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) 

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!

-eldee;another space monkey;[ Forced Evolution Studios ]
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;}  
50/50 Robotics and Software
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]
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.

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;  
50/50 Robotics and Software
that is because loop probably automatically has some garbage value in it. ie.. it doesn''t equal true and it never starts the loop.
thx for all the help. i finnaly got it. bout time

shuv-it
50/50 Robotics and Software
>>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.

This topic is closed to new replies.

Advertisement