Thread: Number between 1 and 100 (console app) - Jumping into C++

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    18

    Number between 1 and 100 (console app) - Jumping into C++

    Hi again,
    Looking for some feedback on my code.

    Please can someone tell me how to stop infinite loop errors.

    If the user types in a string or a char instead of an int.
    If the user types in an int instead of a string or char.

    How can I stop an infinite loop from occurring in these situations?

    I want the program to output a message like:
    "Please enter a whole number"
    or
    "Please enter a Y or N"

    Thanks so much!

    Code:
    #include "stdafx.h"
    #include <cstdlib>
    #include <ctime>
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    int randRange(int low, int high)
    {
     return rand() % (high - low + 1) + low;
     // rand() gives us an initial random number
     // we want the total number of values in our range (between 1 and our max value) so we add '1'
     // we add 'low' (lowest possible value) to bring our range up from 0
    }
    
    
    int playgame()
    {
     cout << "Pick a number between 1 and 100: ";
     int chosen = randRange(1, 100);
     int number = 0;
     for (int attempts = 10; ; attempts--)
     {
      cin >> number;
      if (number < 1 || number > 100)
      {
       cout << "Please make sure to pick your number from between the values 1 and 100\n\n";
      }
      else if (number < chosen)
      {
       cout << "The number is higher, guess again!\n\n";
      }
      else if (number > chosen)
      {
       cout << "The number is lower, guess again!\n\n";
      }
      else if (number == chosen)
      {
       cout << "You guessed the correct number!\n\nThe number was: " << chosen << "!!\n\n";
       cout << "Do you wish to play again? Y/N: ";
       string input;
       cin >> input;
       if (input == "y" || input == "Y")
       {
        cout << "\n\n";
        playgame();
       }
       else
       {
        return 0;
       }
      }
     }
    }
    
    
    
    int main()
    {
     srand(time(NULL));
     playgame();
     return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    You need to check the result of input.
    Code:
    if ( cin >> number ) {
      // successfully parsed the input
    } else {
      // some garbage was input, use cin.flush()
    }
    Your for loop needs to actually exit when number of attempts reaches 0.

    Also, lines 40 to 50 should be in a separate function.
    Code:
    void playgame() {
     cout << "Pick a number between 1 and 100: ";
     int chosen = randRange(1, 100);
     ...
    }
    
    void playSeveralGames() {
      do {
        playgame();
        // prompt for more...
      } while ( ans == "y" );
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 03-26-2016, 07:09 AM
  2. Coin Flip (console app) - Jumping into C++
    By Jasper Dunn in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2016, 06:07 AM
  3. Menu (console app) - Jumping into C++
    By Jasper Dunn in forum C++ Programming
    Replies: 15
    Last Post: 03-16-2016, 07:46 AM
  4. Replies: 2
    Last Post: 08-28-2014, 09:06 PM
  5. How to know the number of lines in a text console
    By edugarcia in forum Linux Programming
    Replies: 4
    Last Post: 09-29-2004, 04:14 PM