Archived

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

sSimontis

Loops

Recommended Posts

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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites