Results 1 to 8 of 8

Thread: Want this code to loop forever (kinda) but having problems...

  1. #1

    Thread Starter
    Addicted Member Fat_N_Furry's Avatar
    Join Date
    Oct 2002
    Posts
    171

    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:
    1. int i = 0;
    2. int a = 0;
    3.     while (i == a);
    4.     {
    5.         cout <<"The armory trainer behind the counter speaks:\n";
    6.         cout <<"Armory Trainer: Welcome to the armory. Enter the number\n of the item you would"
    7.              <<" like to buy:\n#1 Helmet = 200g\n#2 Shield = 500g\n#3 Armor = 900g\n"
    8.              <<"#4 Dagger = 10g\n#5 Scimetar = 400g\n#6 ShortSword = 1000g\n"
    9.              <<"#7 LongSword = 2000g\n#8 Exit\n\nArmory Trainer: So, what will it be?";
    10.         cin >> item;
    11.         if (item == Helmet)
    12.         {
    13.             Shelmet -= 1;
    14.             cout <<"You got a helmet.";
    15.             plyrGold -= 200;
    16.         }
    17.         else if (item == Shield)
    18.         {
    19.             Sshield -= 1;
    20.             cout <<"You receive a shield.";
    21.             plyrGold -= 500;
    22.         }
    23.         else if (item == Armor)
    24.         {  
    25.             Sarmor -= 1;
    26.             cout <<"Wow! You got some heavy duty Armor";
    27.             plyrGold -= 900;
    28.         }
    29.         else if (item == Dagger)
    30.         {
    31.             Sdagger -= 1;
    32.             cout <<"You got a dagger.";
    33.             plyrGold -= 10;
    34.         }
    35.         else if (item == Scimetar)
    36.         {
    37.             Sscimetar -= 1;
    38.             cout <<"You have received a Scimetar";
    39.             plyrGold -= 400;
    40.         }
    41.         else if (item == Shortsword)
    42.         {
    43.             Sshortsword -= 1;
    44.             cout <<"Wow! You got a Shortsword.";
    45.             plyrGold -= 1000;
    46.         }
    47.         else if (item == Longsword)
    48.         {
    49.             Slongsword -= 1;
    50.             cout <<"Amazing! You got an awsome Longsword!";
    51.             plyrGold -= 2000;
    52.         }
    53.         else if (item == Exit)
    54.         {
    55.             return;
    56.         }
    57.         else
    58.         {
    59.             cout <<"The item you have asked for is one we \n"
    60.                  <<"don't carry.";
    61.         }
    62.     }


    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!

  2. #2
    Fanatic Member sql_lall's Avatar
    Join Date
    Jul 2002
    Location
    Up Above (i.e. AUS)
    Posts
    571
    ( 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

  3. #3
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  4. #4
    Addicted Member DarkMoose's Avatar
    Join Date
    Jul 2000
    Location
    in a box
    Posts
    185
    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.

  5. #5
    Fanatic Member sql_lall's Avatar
    Join Date
    Jul 2002
    Location
    Up Above (i.e. AUS)
    Posts
    571

    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

  6. #6
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    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

  7. #7
    Fanatic Member sql_lall's Avatar
    Join Date
    Jul 2002
    Location
    Up Above (i.e. AUS)
    Posts
    571

    OK

    Yeah, sorry about that. I'm only relatively new to C++, i think i'm getting it mixed up with something.
    sql_lall

  8. #8
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    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
  •  



Click Here to Expand Forum to Full Width