Thread: Looping.

  1. #1
    Unregistered
    Guest

    Looping.

    Okay, first of all, I'm sorry if there was a post about this I did not see.

    I am starting with the C++ language. I've been reading C++ for Dummies 4th edition for about 2 days now. I started making my own program using numbers, and everything works, except the looping. I cant make the program loop for some reason. It always terminates. Does anyone have a solution???

  2. #2
    Still A Registered User DISGUISED's Avatar
    Join Date
    Aug 2001
    Posts
    499
    Post some code so we can see where the problem is and we'll be glad to help.

  3. #3
    Unregistered
    Guest
    Ok, here goes:

    //
    //Number Game
    //
    #include <stdio.h>
    #include <iostream.h>

    int main()
    {
    //input loop count
    int loopCount;
    cout<<"Enter number of guesses you wish to make:";
    cin>> loopCount;

    //enter the guess
    int guess;
    cout<<"Enter your guess:";
    cin>> guess;

    if(guess<=42||>=44)

    {
    //Path 1
    loopCount=loopCount--;
    cout<<"That's incorrect. Try again.";
    }
    if(guess==44)
    {
    loopCount=loopCount--;
    cout<<"That's correct. Press any key to exit.";
    }

    return 0;

    }

    That's it. I know, it's pretty stupid 'cause you can only play once. But, hey, it's a start.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >But, hey, it's a start.
    We have to start somewhere, and getting one iteration to work before making the program loop many times is an excellent design strategy.

    Try this out and see if you can work out exactly what it does and why.
    Code:
    #include <iostream.h>
    
    int main()
    {
      //input loop count
      int loopCount;
      cout<<"Enter number of guesses you wish to make:";
      cin>> loopCount;
      cin.ignore();
      
      //enter the guess
      while ( loopCount-- > 0 ) {
        int guess;
        cout<<"Enter your guess:";
        cin>> guess;
        cin.ignore();
        
        if(guess==44) {
          cout<<"That's correct.\n";
          break;
        }
        else
          cout<<"That's incorrect.\n";
      }
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  5. #5
    Unregistered
    Guest
    I know what everything else does, except the command cin.ignore(). I have no idea what it does. One of the problems with my code was I didn't include while('condition'), right?

  6. #6
    . Driveway's Avatar
    Join Date
    May 2002
    Posts
    469
    Yea, you didn't have a loop in your code. That's why it wasn't working.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I know what everything else does, except the command cin.ignore().
    Using cin can cause data to be left in the input stream. This causes problems with further calls to cin so there is a method called ignore() which reads and discards remaining data in the stream.

    -Prelude
    My best code is written with the delete key.

  8. #8
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Try to remember that the program starts at the top of main and works its way down to the end of main. If you want any loops at all you have to use for, while or do..while structures.
    Couldn't think of anything interesting, cool or funny - sorry.

  9. #9
    Registered User
    Join Date
    Aug 2002
    Posts
    14
    Ok, so let me get this straight, the program uses the loopCount variable to loop everything after while(loopCount>0) statement?

    Oh, yeah, I tried doing the while command after the code I just showed you all, but I put the condition as while(guess>=45||<=43). I guess that was the problem with the second try. Thanks a bunch.

  10. #10
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    it has to be like this:

    if( guess >= 45 || guess <= 43 )
    {
    //do stuff
    }

    Everyone makes that mistake, I know I did - many, many times.
    Couldn't think of anything interesting, cool or funny - sorry.

  11. #11
    Registered User
    Join Date
    Aug 2002
    Posts
    14
    I feel so stupid. I have no idea why I didn't think of that.

  12. #12
    Sir Mister Insane Sako Klinerr1's Avatar
    Join Date
    May 2002
    Posts
    608
    lol.
    Email: [email protected] || AIM: MisterSako || MSN: [email protected]

    -the shroom has spoken

  13. #13
    Registered User
    Join Date
    Aug 2002
    Posts
    14
    One more problem. I got the program to loop fine and everything, but when it displays the message: That's correct, it just exits. Is there any way to make the program terminate by pushing a key?

  14. #14
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    > Is there any way to make the program terminate by pushing a key?
    It depends.

    Include conio.h if you have it, to get access to getch(), then just add getch() where you want your program to pause.
    scanf could be done, but that would require hitting enter only.

    > Klinerr1: lol.
    ...
    The world is waiting. I must leave you now.

  15. #15
    Registered User
    Join Date
    Aug 2002
    Posts
    14
    So:

    #include <iostream.h>
    #include <conio.h>

    int main()
    {
    //code
    }

    //wher I want it to pause
    cout<<"That is correct. Press any key to exit.";
    getch()
    break;

    }

    return 0;

    }

    Or something like that. The getch() thing is right, right?

    EDIT: I finally got it! Thanks everybody! Now, to move onto the next chapter.-_-
    Last edited by DarkSFK; 08-02-2002 at 09:09 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. wierd looping effect after exporting 3ds to .x annimation
    By Anddos in forum Game Programming
    Replies: 3
    Last Post: 01-06-2009, 01:43 PM
  2. problems with prototype function looping
    By dezz101 in forum C Programming
    Replies: 5
    Last Post: 04-29-2008, 06:03 AM
  3. looping went berserk
    By miryellis in forum C Programming
    Replies: 7
    Last Post: 09-21-2004, 01:59 PM
  4. Looping questions
    By Peyote in forum C++ Programming
    Replies: 3
    Last Post: 09-15-2003, 11:01 PM
  5. looping and input
    By Kinasz in forum C Programming
    Replies: 2
    Last Post: 03-17-2003, 07:12 AM