Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /////////////////MY NUMBER GUESSING CODE FOLLOWED BY CS BOOK CODE////////
- // number guessing
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- int main()
- {
- int guess, noOfGuess = 0, number;
- number = (rand() + time(0)) % 100;
- while (guess != number && noOfGuess != 5)
- {
- cout << "Guess the number: ";
- cin >> guess;
- cout << endl;
- if (guess < number)
- {
- cout << "Guess too low!" << endl;
- noOfGuess++;
- }
- else if (guess > number)
- {
- cout << "Guess too high!" << endl;
- noOfGuess++;
- }
- else
- {
- cout << "You guessed it! The number is: " << number << endl;
- }
- }
- if (noOfGuess == 5)
- cout << "You lose, too many guesses! The number is: " << number << endl;
- return 0;
- }
- ////??????????????? BOOKS NUM GUESS????????????????????????////
- // Flag-controlled while loop.
- // number guessing game
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- int main()
- {
- int num;
- int guess;
- int noOfGuesses;
- bool done;
- num = (rand() + time(0)) % 100;
- noOfGuesses = 0;
- done = false;
- while ((noOfGuesses < 5) && (!done))
- {
- cout << "Enter an integer greater than or equal to 0 and less than 100: ";
- cin >> guess;
- cout << endl;
- noOfGuesses++;
- if (guess == num)
- {
- cout << "Winner! You guessed the correct number." << endl;
- done = true;
- }
- else if (guess < num)
- cout << "Your guess is lower than the number.\nGuess again!" << endl;
- else
- cout << "Your guess is higher than the number.\nGuess again!" << endl;
- }
- if (!done)
- cout << "You lose! The correct number is " << num << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment