0

This is my code, which chooses a random number from 0 to 10 for the user to guess.

//guess the number game
//my code
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
  unsigned int secretNumber; 
  int guess;
  int maxNumber = 10;
  int maxTries = 4;
  int numTries = 1;


  srand(static_cast<unsigned int>(time(0)));
  secretNumber = (rand() % 10)+ 1;
  cout << "GUESS A NUMBER FROM 0 TO 10!!\n";
    do {
      cout << "\nGuess: \n";
      cin >> guess;
  
      if (guess < secretNumber)
      {
          cout << "too low:(:(!!      ";
          numTries++;
          cout << "Guesses Left: " << maxTries - numTries;
      } ***//Would it be better to add a bool in the condition?***
  
      else if (guess > secretNumber && guess <= maxNumber)
      {
          cout << "Too high:D:D!!      ";
          numTries++;
          cout << "Guesses Left: " << maxTries - numTries;
      }
  
      else if (guess > maxNumber)
      {
          cout << "Do you know how to count to 10?\n";
          cout << "Only from 0 TO 10!!  ";
          numTries++;
          cout << "Guesses Left: " << maxTries - numTries;
      }
  
      else {
          cout << "WOW! you GUESSED IT?! AMAZING!!!!";
          cout << "You're right! the number is " << guess; 
          cout << "\nYou got it right in " << numTries << " guesses!!!";
      }
  
      if (numTries == maxTries)
      {
          cout << "\n\nYou LOOSE :( LOL!";
      }
  
  
  } while (guess != secretNumber && maxTries != numTries);

  return 0;
} 

This is the teacher's code, which is simpler and includes a bool variable. Should my previous code be simpler, just as this one?

// Example program
// Teacher's code
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
  int secretNumber = 7; 
  int guess;
  int numTries = 0;
  int maxTries = 3;
  bool outOfGuesses = false;

  while (secretNumber != guess && !outOfGuesses)
  {
      if (numTries != maxTries)
      {cout << "Guess a Number: ";
      cin >> guess;
      numTries++;}
  
      else 
      {
          outOfGuesses = true;
      }
  }

  if (outOfGuesses)
  {
      cout << "You loose!";
  }
  else 
  {
      cout << "You win!";
  }
  return 0;
}

//Is my code as efficient and simple as the teacher's code? //Is there a simpler way to do what I intended to do in my code?

user4581301
  • 33,082
  • 7
  • 33
  • 54
  • 1
    this is not a place for code reviews, try [here](https://codereview.stackexchange.com/) – Alberto Sinigaglia Aug 11 '20 at 00:31
  • 3
    There isn't an issue of "simplification". The only difference is the amount of information displayed to the user after they enter a number. CodeReview is the place to have working code reviewed. You can ask your teacher [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/q/1452721/364696) – David C. Rankin Aug 11 '20 at 00:35
  • The question is important. No one could tell which is better unless context is provided. – Louis Go Aug 11 '20 at 00:35
  • @DavidC.Rankin So you think the first code is simple as well? I will post it there thanks. Why Is using namespace std bad practice? – Nella Crystal Aug 11 '20 at 00:45
  • @NellaCrystal Click on the link in David's comment to get the answer to the question. – Barmar Aug 11 '20 at 00:49
  • @Barmar oh right, checked it. it didn't show as a link before! – Nella Crystal Aug 11 '20 at 00:55
  • I like the first one because knowing whether the guess is higher or lower than the secret is more fun. You even check if the number they enter is more than 10 (which is a fun easter egg) - but you don't check for zero or negative numbers (you should do that). – Jerry Jeremiah Aug 11 '20 at 01:55
  • The two programs do different things. What are they supposed to do? Without hat information there’s no basis for comparing them. – Pete Becker Aug 11 '20 at 02:01

0 Answers0