Loops

Started by
4 comments, last by sSimontis 20 years, 11 months ago
I am working on a text based game. I got stuck with a loop though. How can I make it loop through a section until the player wishes to go on? I am working on item shops. But I''m not sure how to loop it so that it lets the player shop again until they are finished. Scott
Scott SimontisMy political blog
Advertisement
Have a do{}while loop or something like that and have the 'player' type in 'q', 'e' or something if they want to Exit the shop. Or show the loop to us so we can see how you have done it.


NeFrUgLe

[edited by - NeFrUgLe on May 3, 2003 9:27:26 AM]
-- NeFrUgLe
im not exactly sure what you''re asking, but here goes anyway:
int choice = -1;while(choice > 0 && choice < 3){  cout << "Pick a number.. 1 through 3: ";  cin >> choice;  if( choice < 0 || choice > 3 )     cout << "sorry, that''s not a valid responce" << endl;} 


hope that helps

-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 ]
You could do something like this...


  int StoreLoop = 1;int Item = 1;char choice;while(StoreLoop){    cout << "What you want to shop?" << endl;    cout << "1. Axe" << endl;    cout << "2. Dagger" << endl;    cin >> Item;    cout << "Continue shopping (y/n)?: ";    cin >> choice;    if(choice==''n'')        StoreLoop = 0;}  


I think this will work...
----------------------------------------------------------------------------------------------------------------------"Ask not what humanity can do for you, ask what you can do for humanity." - By: Richard D. Colbert Jr.
Android_S has the right idea there, but I would put something more like this:

    while(TRUE)     {     std::cout<<"What would you like to buy?/n                 1:Axe/n                 2:Sword/n"     std::cin>>Item;     std::cout<<"Would you like to continue? (y/n)"     std::cin>>choice;     if(choice == 'n' || choice == 'N')         break;     }    

EDIT: Looked at my code and said 'Oops'.

That's what I would do, at least.


Curse you windows and your poor explanation of my errors!!!

[edited by - Chigaimasu on May 3, 2003 10:07:59 AM]
Curse you windows and your poor explanation of my errors!!!
Chigaimasu-you forgot to put semi-colons at the end of the cout statements, btw thats how i would do it too, except i would use 1 instead of TRUE cuz im lazy

edit: just saw some more errors, the newlines at the end of your (Chigaimasu's) cout statements should have a backward slash in them instead of a forward one

doh, nuts. Mmmm... donuts
My website

[edited by - brassfish89 on May 3, 2003 10:59:48 AM]

This topic is closed to new replies.

Advertisement