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; Ok, I've been searching two books for answers which I don't seem to find. They are: 1.)"Learning to Program in ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    18

    Arrow while loop not behaving properly.

    Ok, I've been searching two books for answers which I don't seem to find. They are:

    1.)"Learning to Program in C++" By Steve Heller
    2.) "The C++ Programming Language" Third Edition By Bjarne
    Stroustrup

    I'm using bloodshed C++ 4 on w2k.

    my program is something I've actually tried to come up with myself. A little excersise to get the program to detect values above and below a certain predetermined number. my code follows:


    #include <iostream>
    #include <string>

    int main()
    {

    using namespace std;
    int correctguess;
    correctguess = 33;
    int attempt;

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

    cin >> attempt;

    if (attempt == correctguess)
    cout << "Thats right on the money!" << endl;

    //while (attempt != correctguess)
    //{
    //cout << "Sorry thats not correct, please try again." << endl;
    //cin >> attempt;
    //}
    //cout << "Thats correct!" << endl;


    while (attempt > correctguess)
    {
    cout << "Try a little lower." << endl;
    cin >> attempt;
    }


    while (attempt < correctguess)
    {
    cout << "Thats too low." << endl;
    cin >> attempt;
    }

    return 0;
    }

    My problem is that if I run the program and type 33 then everything is cool, however if I type something over 33 the first while of the two that are not commented out goes into action telling me that my selection was too high.

    If I then type a value lower than 33 I get the second while going.

    My problem is after that sometimes the program just exits.
    How do I get the while functions (are they functions?) keep on repeating until the correct value is reached? Am I missing something here?

    The funny thing is that sometimes I can type in 55 three times and get a "Try a little lower." message, then type 1 three times and get a "Thats too low."
    and then I try another number and it just quits, or if I throw it a curve and give it a 123 or a 456 the program exits.

    Can somebody point me to some detailed reading regarding this stuff? I'm not trying to get work done for me, I'm trying to teach myself but have a ton of questions. And documentation is so scattered.

    What am I doing wrong? I don't see any obvious mistakes but then again my knowledge of C++ is severely limited.

    Is this one of those bugs thats hard to nail down?

    Thanks to everyone who is putting up with this.
    Last edited by Dreamerv3; 01-04-2002 at 06:33 PM.

  2. #2
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    I am not quite sure I understand what you're trying to code but it seems that if you change some of your while loops to if statements maybe you could get better results.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  3. #3
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    It's a flaw in your logic. If you guess below the first number then you pass the first test. If you then guess a number above you'll pass the second test.

    If your number to guess is 10, then entering 5 will get you past the first while loop. Then if you then guess 20 you'll get past the second while loop.

    You need to enclose these checks within your commented out code so that for every pass it checks to see if you've got the correct value. If not it'll go back to the start and re-check.

  4. #4
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    Ok try this.....make some boxes (drawn) and start making a sequence of what happens after something in put....for example what happens when the user enters a number over 35? Then make another box saying where you programs after that, and then at that point , if it's over 33 what should be the next step and make that next step another box and so on...maybe some one can explain this better it's kinda like using the UML language for this.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    154

    Re: while loop not behaving properly.

    Originally posted by Dreamerv3

    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
    
      using namespace std;
      int correctguess;
      correctguess = 33;
      int attempt;
    
      cout << "Pick a number between 0 and 100:" << endl;
    
      cin >> attempt;
    
           if (attempt == correctguess)
           cout << "Thats right on the money!" << endl;
    
           //while  (attempt != correctguess)
           //{
           //cout << "Sorry thats not correct, please try again." << endl;
           //cin >> attempt;
           //}
           //cout << "Thats correct!" << endl;
    
    
           while (attempt > correctguess)
           {
           cout << "Try a little lower." << endl;
           cin >> attempt;
           }
    
    
           while (attempt < correctguess)
           {
           cout << "Thats too low." << endl;
           cin >> attempt;
           }
    
       return 0;
    }
    First, the using namespace std; should be under the #includes and before main().

    With loops it helps to step through on paper for different values. The two while loops have the same problem. Look at the first one, where attempt > correctguess. Ok, you entered 55 and entered that while loop. cout prints and you enter again. If you enter 55, or any number greater than 33, the loop repeats.
    So on the third try, you enter 33. attempt now equals correctguess, so you exit the loop. You don't enter the last loop, but then again, there's nothing to print the correct answer message now, so the program just ends. If you entered a number less than 33 on the third try, you'll enter the last loop.

    If the first attempt you entered was less than 33, say 27, you enter the last loop but not the first loop. That error message will print, and you'll enter another number. If it's less than 33, you'll stay in the loop. Enter 33 or higher and the while condition fails and you exit the loop. Since, again, there's nothing to print a correct message, and nothing to check for a high value at this point, the program just ends.

    You may want to do some type of nested loops, if - else if statements in a while loop, or while loops in an if statement, or whatever.

  6. #6
    Registered User
    Join Date
    Jan 2002
    Posts
    18
    Here is my new code using if:


    #include <iostream>
    #include <string>



    int main()
    {

    using namespace std;
    int correctguess;
    correctguess = 33;
    int attempt;

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

    cin >> attempt;

    if (attempt == correctguess)
    cout << "Thats right on the money!" << endl;

    //while (attempt != correctguess)
    //{
    //cout << "Sorry thats not correct, please try again." << endl;
    //cin >> attempt;
    //}
    //cout << "Thats correct!" << endl;


    if (attempt > correctguess)
    {
    cout << "Try a little lower." << endl;
    cin >> attempt;
    }


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

    return 0;
    }


    It still quits as before.

    This is what I want the program to do:

    Please type in a number between 0 and 100:
    55
    Try a little lower.
    11
    Thats too low.
    34
    Try a little lower
    29
    Thats too low.

    on and on until I hit 33. This lets me the user zero in after a few attempts.

    Here is what happens right now:

    Please type in a number between 0 and 100:
    55
    Try a little lower.
    11
    Thats too low.
    34

    And thats the end of everything it suddenly quits.

    It seems as if the program just executes the first loop once and then after the condition has been met for that first loop the program goes on down the lines executing the second loop, now if the codnition is out of the scope of the second loop that is if the value entered is not lower than 33 then the program just exits, if it is lower than 33 the program repeates the second while loop.

    I want the program at this last stage after the second while loop to go back to the first while loop if the vaue entered is great than 33.
    And I don't know how to code in this type of command that directs the program flow back to a given point.

    I don't know what pionters are or how they work just yet. What mechanism can I use to make program execaution jump to a different place in the program given certain conditions are met.

    I know that in basic one can just use the goto command and a line number. But how does it work in C++?

    I hope thats clearer.

    BTW, is there anywhere on the web where this kind of detailed information is available?

  7. #7
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    maybe some one can explain this better it's kinda like using the UML language for this.
    No offence, but you don't need UML to do this.

  8. #8
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    I know you don't but maybe it will be easier for him, because he seems to be a newbie.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  9. #9
    Registered User
    Join Date
    Aug 2001
    Posts
    154
    What you want is either while loops inside an if statement, or if statements inside a while loop.

  10. #10
    Registered User
    Join Date
    Jan 2002
    Posts
    18
    Ok here is my flowchart:

    It is a bit big so please excuse the size however it is a small file, about 10k in size, so even 56kbps users should see it right away.

    http://www.cinematicpictures.com/~dr...cpp/fchart.gif

    See what I mean?

    At the bottom the program just quits, I want to make the program jump back to the very first if.

    Bear in mind what you see in my sample code is pretty much what I know thus far about C++, so I'm not very experienced with nested if and while loops, what does nested mean anyway?
    Does it mean encapsulating the two whilel loops inside another while loop so that the loop holding them prevents the program from exiting and thus sends the program back to the beginning of the first while loop?
    Is this making any sense?

    Can someone show me some sample code explaining this concept? Or point me to an url?

    Does anyone see what I mean by "jump back to the very first IF statement" is there an else -> goto type of command that would do this?

    [edit]

    Here is my source code modified to reflect a nested structure as far as I could figure it out:
    #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 << "Thats right on the money!" << endl;
    else


    if (attempt > correctguess)
    {
    cout << "Try a little lower." << endl;
    cin >> attempt;
    }


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



    return 0;

    //if (attempt == correctguess)
    //cout << "Thats right on the money!" << endl;


    //while (attempt != correctguess)
    //{
    //cout << "Sorry thats not correct, please try again." << endl;
    //cin >> attempt;
    //}
    //cout << "Thats correct!" << endl;
    }

    I just commented out the parts I don't think I'll need right now.

    There are probably a ton of problems in it, that I don't know about since my docs are so hard to follow.

    I'm off to Barnes and Noble to see what other books there are, I'm after my third book already, why don't these things have examples?
    Last edited by Dreamerv3; 01-04-2002 at 08:19 PM.

  11. #11
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    Although I haven't really analyzed your code I think that you might be able to try a go to statement, where you want the program to go to the beginning......but remember these statements are not really recommended..........they are shunned!!!!!!!!!!! You could also use some continue statements, maybe not really sure.
    Last edited by incognito; 01-04-2002 at 10:09 PM.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  12. #12
    Registered User
    Join Date
    Jan 2002
    Posts
    18
    How did all you guys teach yourselves?
    I mean I'm really trying, but it seems that everytime I go sit down and write something there's always some critical tiny thing that I have not been exposed to and which causes my program to trip.

    Continue statement?

    The goto sounds promising, yet why is it shuned?
    Is there some problem with goto?

    All I'm asking for is a good reference to read up on.
    Just point me in the right direction.

  13. #13
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    Go to it's a little bit messy because it can create really nasty bugs, this is why it's shuned.


    // Listing 7.4
    // Demonstrates break and continue

    #include <iostream>

    int main()
    {
    using namespace std;
    unsigned short small;
    unsigned long large;
    unsigned long skip;
    unsigned long target;
    const unsigned short MAXSMALL=65535;

    cout << "Enter a small number: ";
    cin >> small;
    cout << "Enter a large number: ";
    cin >> large;
    cout << "Enter a skip number: ";
    cin >> skip;
    cout << "Enter a target number: ";
    cin >> target;

    cout << "\n";

    // set up 3 stop conditions for the loop
    while (small < large && large > 0 && small < MAXSMALL)

    {

    small++;

    if (small % skip == 0) // skip the decrement?
    {
    cout << "skipping on " << small << endl;
    continue;// it will go back to beginning of the loop
    }

    if (large == target) // exact match for the target?
    {
    cout << "Target reached!";
    break;
    }

    large-=2;
    } // end of while loop

    cout << "\nSmall: " << small << " Large: " << large << endl;
    return 0;
    }

    Remember continue is to be used on a while or for loop only....they are the second most dangerous "things" next to go to statements..create nasty bugs.
    Last edited by incognito; 01-05-2002 at 12:16 AM.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  14. #14
    Señor Member
    Join Date
    Jan 2002
    Posts
    561
    The goto statement is shunned because it creates spagetti code, code that goes all over the place. The ideal way to code is linear, it goes in one direction. The way to get around the goto statement in most situations is the function. This leads to linear code that is easier to follow and debug.

  15. #15
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    Hey you might want to try Sams Teach Yourself C++ in 21 days that's the one I am reading right now on C++, I have fourth edition. If you have any questions with it ask me.......BTW which book are you reading? But I've heard this book is good 2.) "The C++ Programming Language" Third Edition By Bjarne
    Stroustrup , it's by the guy who invented C++
    Last edited by incognito; 01-05-2002 at 12:24 AM.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

Page 1 of 2 12 LastLast
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