Thread: Do/While loop not looping

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    3

    Do/While loop not looping

    Forgive me if this has been asked and answered. I have searched the forums and could not find an answer.
    I am a newbie to programming and have been going through the tutorials and am having a issue with my play again loop.
    Here is my code:


    Code:
    #include <iostream>
    #include <cstring>
    #include <stdlib.h>
    #include <ctime>
    #include <conio.h>
    
    using namespace std;
    
    int main()
    {
    char name[15], answer[4], playAgain[4];
    srand(time(0));
    int number=rand()%100;
    int guess=-1;
    int trycount=0;
    
    
    
     cout<<"please enter your first name: ";
     cin.getline(name, 15);
     cout<<"hello " << name <<"\n";
     cout<<"did you have a good day? yes/no " ;
     cin.getline ( answer, 4 );
    
      if
       ( strcmp ( answer, "yes" ) == 0)
       cout<< "I am so glad you had a good day" <<endl;
      else
       cout<< "I am sorry you had a bad day" <<endl;
    
     cout<<"Would you like to play a game? yes/no ";
     cin.getline (answer, 4 );
    
      if (strcmp (answer, "yes") ==0)
      {
         cout<< "Great, this game is find the number " <<endl;
         cout<< "The number will be between 0 and 100" <<endl;
         cout<< "You have 8 tries " <<endl;
          do
          {
           while(guess!=number && trycount<8)
           {
    
             cout<<"Please enter a guess: ";
             cin>>guess;
    
             if(guess<number) cout<<"Too low"<<endl;
             if(guess>number) cout<<"Too high"<<endl;
    
             trycount++;
           }
          if(guess==number)
           cout<<"You guessed the number" <<endl;
          else {
                cout<<"Sorry, the number was: "<<number <<endl;
               }
             cout<<"would you like to play again? yes/no" <<endl;
             cin.getline (playAgain, 4);
           }
          while (strcmp (playAgain, "yes") == 0);
            // cin.getline (playAgain, 4);      
         cout<<"Thanks for playing ";
    
         return 0;
       }
       else {
             cout<< "OK see you next time" ;
            }
    
    
    
    return 0;
    }
    The program compiles and runs -- the first time. When it gets to the hilighted part of the code it asks
    "would you like to play again? yes/no
    Thanks for playing"
    It never does go back to the game part of the code. As you can see, I have tried putting cin.getline(playAgain) both above and below the while statement and get the same result.
    I know it is something easy but I can not figure out what I am doing wrong.
    Thanks in advance for any insight you might be able to provide

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    When you input an integer using the extraction operator >>, the user enters an integer followed by a newline character which indicates the end of the input. The extraction takes the integer but leaves the newline still in the input stream. Then, when you call cin.getline(), getline takes that leftover newline as its input and doesn't wait for anything new.

    Solution is to clear the leftover newline out of the stream right after the integer input. So use cin.ignore to consume and discard that character. Adjust your code as follows:
    Code:
    cin>>guess;
    cin.ignore();
    (Then you will be able to see what you have to fix next.)

  3. #3
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Also, it's better to use strcmpi() instead of strcmp().
    I don't care if someone doesn't like me, i was not put on earth to entertain everyone.

    No King, no Queen, I am the ACE of battle.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    3
    Thanks so much for the quick answer.
    After I adjusted my code as you suggested. I get part of the loop but not all. My output is as follows:

    You guessed the number
    would you like to play again? yes/no yes
    You guessed the number
    would you like to play again? yes/no yes
    You guessed the number
    would you like to play again? yes/no yes
    You guessed the number
    would you like to play again? yes/no no
    Thanks for playing

    So it is not reading the entire loop. It does not make sense to me why it would only loop back to the bottom of the loop and not where the" do" statement starts.

    On a second note. Can you tell me the difference between strcmp() and the strcmpi() and why strcmpi is better? I assume the i means compare integer but I am confused as to why that would be better

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Mr.777 View Post
    Also, it's better to use strcmpi() instead of strcmp().
    I'd say it's better to use std::string instead of C-style strings and use the normal comparison operator (==).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Can you tell me the difference between strcmp() and the strcmpi() and why strcmpi is better? I assume the i means compare integer but I am confused as to why that would be better
    strcmpi() means case insenstive comparisons between strings. e.g. "Maths","maths" will be treated same.
    I don't care if someone doesn't like me, i was not put on earth to entertain everyone.

    No King, no Queen, I am the ACE of battle.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    3
    Thanks again for the input. I did put me on the right track.
    I needed to reset my variables trycount and guess. Program ran like it was suppose to except it kept the same random number. Reset that and everything is happy (including me)
    Appreciate the insight and help from R.Stiltskin, Mr. 777 and Elysia

    You will probably see more of me as I get further into this monster called C++
    Thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Histogram function
    By davidjg in forum C Programming
    Replies: 14
    Last Post: 11-24-2010, 12:21 AM
  2. Replies: 8
    Last Post: 12-01-2008, 10:09 AM
  3. Mutiliplcation Program that uses a (do, while, and for loop)?
    By Debbie Bremer in forum C++ Programming
    Replies: 4
    Last Post: 10-11-2008, 06:04 PM
  4. loop needed also how to make input use letters
    By LoRdHSV1991 in forum C Programming
    Replies: 3
    Last Post: 01-13-2006, 05:39 AM
  5. Can't exit a do/while loop - please help
    By Vireyda in forum C++ Programming
    Replies: 11
    Last Post: 08-08-2005, 06:46 AM

Tags for this Thread