Question

Find and fix errors #include <iostream> #include <cstdlib> #include <ctime> using namespace std; const int MIN...

Find and fix errors

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

const int MIN = 1;
const int MAX = 10;

int getRandom(int low, int high);

int main()
{
  
int random_num = 0;
int player_num;
int tries;
int seed = static_cast<int>(time(0));
bool guessed = false;
  
srand(seed);
// call the getRandom function below
  
  
tries = 4;
while ( tries > 0 && !guessed )
{
cout << "Enter a number within the range 1 to 10: ";
cin >> player_num;
if( player_num == random_num )
{
cout << "Congratulations! You did it!\n\n";
}
else
{
if ( tries = 1 )
cout << "Sorry, the number was " << random_num << endl;
else
{
if( player_num < random_num )
cout << "Your guess is low!";
else
cout << "Your guess is high!";
cout << " Try again!\n\n";
}
}
}
return 0;
}

/*~*~*~*~*~*~
This function takes two integers low and high,
and generates a random number within this range (inclusive)
*~*/
int getRandom(int low, int high)
{
if (low > high)
{
int hold = low;
low = high;
high = hold;
}
  
return rand() % (high - low + 1) + low;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

const int MIN = 1;
const int MAX = 10;

int getRandom(int low, int high);

int main() {
    int random_num = 0;
    int player_num;
    int tries;
    int seed = static_cast<int>(time(0));
    bool guessed = false;

    srand(seed);

    // call the getRandom function below
    random_num = getRandom(MIN, MAX);

    tries = 4;
    while (tries > 0 && !guessed) {
        cout << "Enter a number within the range 1 to 10: ";
        cin >> player_num;
        if (player_num == random_num) {
            cout << "Congratulations! You did it!\n\n";
            guessed = true;
        } else {
            if (tries == 1)
                cout << "Sorry, the number was " << random_num << endl;
            else {
                if (player_num < random_num)
                    cout << "Your guess is low!";
                else
                    cout << "Your guess is high!";
                cout << " Try again!\n\n";
            }
        }
        tries--;
    }
    return 0;
}

/*~*~*~*~*~*~
This function takes two integers low and high,
and generates a random number within this range (inclusive)
*~*/
int getRandom(int low, int high) {
    if (low > high) {
        int hold = low;
        low = high;
        high = hold;
    }

    return rand() % (high - low + 1) + low;
}

Add a comment
Know the answer?
Add Answer to:
Find and fix errors #include <iostream> #include <cstdlib> #include <ctime> using namespace std; const int MIN...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • #include <iostream> #include <cstdlib> #include <time.h> #include <string> using namespace std; int main() { srand(time (0));...

    #include <iostream> #include <cstdlib> #include <time.h> #include <string> using namespace std; int main() { srand(time (0)); int number, guess, response, reply; int score = 0 number = rand() % 100 + 1; do { do { cout << "Enter your guess "; cin >> guess; score++; if (guess < number) cout << guess << " is too low! Enter a higher number. "; else if (guess > number) cout << guess << " is too high! Enter a lower number....

  • This is for C++ #include <random> #include <iostream> #include <ctime> using namespace std; /* In the...

    This is for C++ #include <random> #include <iostream> #include <ctime> using namespace std; /* In the game of craps, a shooter rolls 2 dice and adds the dots on the upper most faces of the dice. 7 or 11 on the first roll wins, 2, 3, or 12 on the first roll loses, andthing else is call the point and the player rolls again The following program fragment uses 1-way if statements simulate the 1st roll of the dice. Replace...

  • #include<iostream> #include<cstdlib> using namespace std; int main() {     // create array of size 20    ...

    #include<iostream> #include<cstdlib> using namespace std; int main() {     // create array of size 20     int arr[20];         int i;         cout<<"scores : ";     for( i = 0 ; i < 20 ; i++ )     {         // generate a random number i range 70 - 100 an add it to arr         arr[i] = rand() % 31 + 70;                 cout<<arr[i]<<" ";     }         // store the total score     int total = 0;...

  • Can someone help me make the output of this code more spaced out? #include<iostream> #include<cstdlib> using...

    Can someone help me make the output of this code more spaced out? #include<iostream> #include<cstdlib> using namespace std; int main() {    int amount=1000,guess,answer,bet,count=0,win=0;    float per;    char ch;    cout<<"Welcome to the high-low betting game.\nYou have $1000 to begin the game.\nValid guesses are numbers between 1 and 100.\n";    do    {        cout<<"Please enter a bet:\n";        cin>>bet;        answer=rand()%100;        for (int i=0;i<6;i++)        {            cout<<"Guess "<<i+1<<":";   ...

  • Getting this error. [Error] expected ';' after class definition #include <iostream> #include <cstdlib> #include <ctime> #include...

    Getting this error. [Error] expected ';' after class definition #include <iostream> #include <cstdlib> #include <ctime> #include <conio.h> using namespace std; int sum=0; class Coin { int sideUp; public: Coin(int value) { toss(value); }; void toss(int value); } void Coin::toss(int value) { if(rand() %100<=50) { sum+=value; cout<<"\n You got a head. Value "<<value<<" added!!"; } else { cout<<"\n You got a tail. No value added"; } } int main() { int temp; cout<<"\n Quarter = 25 cents\n Dime = 10 cents...

  • Consider the following C++ program: #include <iostream> #include <cstdlib> using namespace std; int main int n...

    Consider the following C++ program: #include <iostream> #include <cstdlib> using namespace std; int main int n =0; int i = 0; cout << "Please enter a strictly positive number:"; cin >> n if (n <= 0) exit(EXIT_FAILURE) while (n > 1) n-n/2; i << endl; cout"Output:" return 0; Answer the following questions: What is the output of the program for each of the following values of n: -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9? What does...

  • #include <fstream> #include <iostream> #include <cstdlib> using namespace std; // Place charcnt prototype (declaration) here int...

    #include <fstream> #include <iostream> #include <cstdlib> using namespace std; // Place charcnt prototype (declaration) here int charcnt(string filename, char ch); int main() { string filename; char ch; int chant = 0; cout << "Enter the name of the input file: "; cin >> filename; cout << endl; cout << "Enter a character: "; cin.ignore(); // ignores newline left in stream after previous input statement cin.get(ch); cout << endl; chcnt = charcnt(filename, ch); cout << "# of " «< ch« "'S:...

  • #include <iostream> #include <cmath> #include <iomanip> #include <cstdlib> using namespace std; bool isInt (double value) {...

    #include <iostream> #include <cmath> #include <iomanip> #include <cstdlib> using namespace std; bool isInt (double value) {     double dummy;     return bool(modf(value, &dummy) == 0); } double sqr(double value) { return value * value; } double calcFrictionFactor(double R, double D, double epsilon) {     const double BlasiusCoefficient = 0.3164;     double f_old, f_new;     f_new = BlasiusCoefficient * pow(R, -0.25); // loop until our two values are within 0.000001 of each other     do {   f_old = f_new; // previous guess is now the old one...

  • #include "stdafx.h" #include <iostream> using namespace std; class dateType {    private:        int dmonth;...

    #include "stdafx.h" #include <iostream> using namespace std; class dateType {    private:        int dmonth;        int dday;        int dyear;       public:       void setdate (int month, int day, int year);    int getday()const;    int getmonth()const;    int getyear()const;    int printdate()const;    bool isleapyear(int year);    dateType (int month=0, int day=0, int year=0); }; void dateType::setdate(int month, int day, int year) {    int numofdays;    if (year<=2008)    {    dyear=year;...

  • Merge Sort: Time Complexity: O(n log(n)) #include "stdafx.h" #include <iostream> #include <time.h> #include <stdlib.h> using namespace...

    Merge Sort: Time Complexity: O(n log(n)) #include "stdafx.h" #include <iostream> #include <time.h> #include <stdlib.h> using namespace std; void combine(int *a, int low, int high, int mid) {        int i, j, k, c[100000];        i = low;        k = low;        j = mid + 1;        while (i <= mid && j <= high)        {               if (a[i] < a[j])               {                      c[k] = a[i];                      k++;                      i++;               }               else               {                     ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT