Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help rewriting this Number Guessing program in a for-loop format. Code wo

ID: 3597369 • Letter: I

Question

I need help rewriting this Number Guessing program in a for-loop format. Code would be appreciated. Thank you

#include<iostream>

#include<string>

#include<cstdlib>

#include <cmath>

#include<time.h>

using namespace std;

int main(){

//Variable Delarations (boolean value for correct is false), user inputs a number guess

int number;

int guess;  

bool correct = false;

int numGuesses;

srand(static_cast<int> (time(NULL)));

// Generates a random number between 1 and 100.

number = (rand() % 100) + 1;

//User can inpute endless amount of number guesses, and loops the program

//Only allows 5 tries

numGuesses = 0;

while (numGuesses < 5)

{

cout << "Guess the number the computer randomly picked between 1 - 100: ";

cin >> guess;

numGuesses++;

// Check if the user has guessed the correct number.

// If not, tell him if his guess is too low or too high

if (number > guess)

{

cout << "Sorry, your guess is too low" << endl<<endl;

}

else if (number < guess)

{

cout << "Sorry, your guess is too high" << endl<<endl;

}

else

{

cout << "you guessed right, you win!" << endl<<endl;

correct = true;

}

}

cout << "Sorry you lost. The number is: " << number << endl;

////////////^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ possible answer if user is wrong

system("pause");

return 0;

}

Explanation / Answer

//I have replaced while loop with for loop

//and also made few appropriate changes to your code

#include<iostream>
#include<string>
#include<cstdlib>
#include <cmath>
#include<time.h>
using namespace std;

int main(){
//Variable Delarations (boolean value for correct is false), user inputs a number guess
int number;
int guess;  
bool correct = false;
int numGuesses;

srand(static_cast<int> (time(NULL)));

// Generates a random number between 1 and 100.
number = (rand() % 100) + 1;


//User can inpute endless amount of number guesses, and loops the program
//Only allows 5 tries
numGuesses = 0;
for(int i=0;i<5;i++)
{
cout << "Guess the number the computer randomly picked between 1 - 100: ";
cin >> guess;
numGuesses++;
// Check if the user has guessed the correct number.
// If not, tell him if his guess is too low or too high
if (number > guess)
{
cout << "Sorry, your guess is too low" << endl<<endl;
}
else if (number < guess)
{
cout << "Sorry, your guess is too high" << endl<<endl;
}
else
{
cout << "you guessed right, you win!" << endl<<endl;

correct = true;
break;
}
}
if(!correct)
{
cout << "Sorry you lost. The number is: " << number << endl;
////////////^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ possible answer if user is wrong
}
system("pause");

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote