while loop not behaving properly.

This is a discussion on while loop not behaving properly. within the C++ Programming forums, part of the General Programming Boards category; elchulo2002: Yeah I'm using the Stroustrup book for reference but its not easy to get explicit definition with coherent examples, ...

  1. #16
    Registered User
    Join Date
    Jan 2002
    Posts
    18
    elchulo2002:

    Yeah I'm using the Stroustrup book for reference but its not easy to get explicit definition with coherent examples, everything is very loosely presented, so its up to you to figure the beast which is C++ out.

    Tim545: Silly me for thinking there were straighforward ways of doing things.

    I guess learning C++ is like Darwinian survivial, only the fittest or in this case, smartest survive.

    No problem, though I just need to try a bit harder.
    Last edited by Dreamerv3; 01-05-2002 at 12:48 AM.

  2. #17
    Registered User
    Join Date
    Dec 2001
    Posts
    194

    Re: while loop not behaving properly.

    What you want to do is use a while loop to keep looping until the guess is correct.
    That is what you had commented out.
    Then inside that while loop, use 2 if statements, to check if it is to high or to low and print out the apporiate hint.

    here is some pseduocode

    Code:
    get guess from user
    while( guess is not right answer)
    {
      if guess is to high 
          tell user guess is to high
      else //there guess was not right, and not to high, it must be low
        tell user guess is to low
      get guess from user
    }
    tell user they got it :)

  3. #18
    Registered User
    Join Date
    Jan 2002
    Posts
    18
    WOW! that looks so simple, I'll try it right away!

  4. #19
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    907
    This works.


    //////////////////////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////////////



    #include <iostream>
    #include <string>
    using namespace std;

    int main()
    {

    int correctguess;
    correctguess = 33;
    int attempt;

    cout << "Pick a number between 0 and 100:" << endl;

    cin >> attempt;

    while (attempt != correctguess)
    {
    cout << "Please try again." << endl;
    cin >> attempt;
    if (attempt > correctguess)
    {
    cout << "Try a little lower." << endl;
    }

    if (attempt < correctguess)
    {
    cout << "Thats too low." << endl;
    }

    }
    cout << "Thats correct!" << endl;

    return 0;
    }
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  5. #20
    Registered User
    Join Date
    Aug 2001
    Posts
    154
    If you're so new that nested loops throw you for a loss, I'd hold off on Stroustroup's book for a while. Deitel & Deitel is a popular textbook.

  6. #21
    Unregistered
    Guest
    here is what i did and it works just how you wanted it to do,
    it also has responses for invalid inputs such as a negative number or a number higher then 100.

    #include <iostream>
    #include <string>
    #include <stdlib.h>
    using namespace std;

    int main()
    {

    int correctguess;
    correctguess = 33;
    int attempt;

    cout << "Pick a number between 0 and 100:" << endl;

    cin >> attempt;


    while (attempt != correctguess)
    {
    if(attempt < correctguess)
    {
    if(attempt<0)
    {
    cout<<"Invalid input, to low. Must be between 0 and 100."<<endl;
    cin>>attempt;
    continue;
    }
    cout<<"That's to low, try again"<<endl;
    cin>>attempt;
    }
    else if (attempt>correctguess)
    {
    if(attempt>100)
    {
    cout<<"Invalid input, to high. Must be between 0 and 100."<<endl;
    cin>>attempt;
    continue;
    }
    cout<<"That's to high, try again"<<endl;
    cin>>attempt;
    }
    }
    if(attempt==correctguess)
    cout<<"Good job you got it right"<<endl;
    system("pause");
    return 0;
    }

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. For loop not running properly
    By Todd88 in forum C++ Programming
    Replies: 21
    Last Post: 04-04-2008, 06:27 PM
  2. syntax question
    By cyph1e in forum C Programming
    Replies: 19
    Last Post: 03-31-2006, 12:59 AM
  3. is my loop badly set up?
    By mabufo in forum C++ Programming
    Replies: 3
    Last Post: 02-19-2006, 03:40 PM
  4. return to start coding?
    By talnoy in forum C++ Programming
    Replies: 1
    Last Post: 01-26-2006, 03:48 AM
  5. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 09:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21