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;...

  • 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...

  • #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:...

  • 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 "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               {                     ...

  • #include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool...

    #include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool openFile(ifstream &); void readData(ifstream &, int [], int &); void printData(const int [], int); void sum(const int[], int); void removeItem(int[], int &, int); int main() { ifstream inFile; int list[CAP], size = 0; if (!openFile(inFile)) { cout << "Program terminating!! File not found!" << endl; return -1; } //read the data from the file readData(inFile, list, size); inFile.close(); cout << "Data in file:" <<...

  • #include <iostream> #include <sstream> #include <string> using namespace std; int main() {    const int index...

    #include <iostream> #include <sstream> #include <string> using namespace std; int main() {    const int index = 5;    int head = 0;    string s[index];    int flag = 1;    int choice;    while (flag)    {        cout << "\n1. Add an Item in the Chores List.";        cout << "\n2. How many Chores are in the list.";        cout << "\n3. Show the list of Chores.";        cout << "\n4. Delete an...

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
ADVERTISEMENT