|
-
Feb 1st, 2003, 02:10 AM
#1
Thread Starter
Addicted Member
Want this code to loop forever (kinda) but having problems...
Hey I got this loop that I want to loop forever unless the user types Exit. This is my current code; when C++ builds the program, it starts up the command prompt, and it's totally blank. Nothing but a cursor.
VB Code:
int i = 0;
int a = 0;
while (i == a);
{
cout <<"The armory trainer behind the counter speaks:\n";
cout <<"Armory Trainer: Welcome to the armory. Enter the number\n of the item you would"
<<" like to buy:\n#1 Helmet = 200g\n#2 Shield = 500g\n#3 Armor = 900g\n"
<<"#4 Dagger = 10g\n#5 Scimetar = 400g\n#6 ShortSword = 1000g\n"
<<"#7 LongSword = 2000g\n#8 Exit\n\nArmory Trainer: So, what will it be?";
cin >> item;
if (item == Helmet)
{
Shelmet -= 1;
cout <<"You got a helmet.";
plyrGold -= 200;
}
else if (item == Shield)
{
Sshield -= 1;
cout <<"You receive a shield.";
plyrGold -= 500;
}
else if (item == Armor)
{
Sarmor -= 1;
cout <<"Wow! You got some heavy duty Armor";
plyrGold -= 900;
}
else if (item == Dagger)
{
Sdagger -= 1;
cout <<"You got a dagger.";
plyrGold -= 10;
}
else if (item == Scimetar)
{
Sscimetar -= 1;
cout <<"You have received a Scimetar";
plyrGold -= 400;
}
else if (item == Shortsword)
{
Sshortsword -= 1;
cout <<"Wow! You got a Shortsword.";
plyrGold -= 1000;
}
else if (item == Longsword)
{
Slongsword -= 1;
cout <<"Amazing! You got an awsome Longsword!";
plyrGold -= 2000;
}
else if (item == Exit)
{
return;
}
else
{
cout <<"The item you have asked for is one we \n"
<<"don't carry.";
}
}
Thanks!
Ríçk
 Eat long and prosper! 
If someone helps you, find someone you can help.
If you still have time, click on the darn banner up there and help the forum! 
-
Feb 1st, 2003, 05:29 AM
#2
Fanatic Member
( i am guessing you put this in the int main(void) or something and left it out for convenience)
i'm not sure if it helps, but is it possible to put:
while(1==1)
instead of while(i==a)
oh, and i think i may have read somewhere that while() loops need ; at the end, but perhaps that is do while() loops.
sql_lall 
-
Feb 1st, 2003, 05:45 AM
#3
Monday Morning Lunatic
You need to put a couple of cout << flush statements in. It's not displaying the contents of the buffer because it doesn't think it needs to just yet.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Feb 1st, 2003, 09:47 AM
#4
Addicted Member
the only problem is that you have a semi-colon after the while() statement.. it's not incorrect syntaxwise, but logically all it's doing is looping -nothing- forever
To understand recursion, one must first understand the concept of recursion.
-
Feb 2nd, 2003, 03:25 AM
#5
Fanatic Member
try...
try the semicolon after the last close-squiggly-bracket "}" of the while loop
while(i==a)
{
//***blablabla***
};
Oh, and instead of having two integer variables at the start, just have one bool variable:
bool a=true;
while(a)
etc...
sql_lall 
-
Feb 3rd, 2003, 02:43 AM
#6
Fanatic Member
Why would you need to put a semicolon after the last } ?
I never do that and it works perfect
Never argue with fools, they will only drag you down to their level, and beat you with experience.
Q: How do you tell an experienced hacker from a novice?
A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer
-
Feb 3rd, 2003, 04:01 AM
#7
Fanatic Member
OK
Yeah, sorry about that. I'm only relatively new to C++, i think i'm getting it mixed up with something.
sql_lall 
-
Feb 3rd, 2003, 03:29 PM
#8
Fanatic Member
Ok, cool
I just wanted to make sure I did everything right, I'm pretty new to c++ myself...
Never argue with fools, they will only drag you down to their level, and beat you with experience.
Q: How do you tell an experienced hacker from a novice?
A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|