srand(time(0)); // the computer generates a long list of numbers if you put in srand(34) it will move to area 34 on that list and select numbers from there but if you put in time because time is always changing that will make it even more random. So if you run srand(34) over again you will get back out the same numbers.
randomnum = rand() % 100 + 1; //the rand() is the random number the % is the modulus it divides hat ever number by 100 and puts the remainder. eg 324 remainder 24 or 1492 remainder 92 therefore it can go from 0 to 99 that is why they add the 1 to reach the 100.
cout << "Welcome to the guessing game " << endl;
cout << "Please guess a number" << endl;
cin >> g;
if
(g != randomnum);
{
while (g != randomnum);
{
cout << "That is incorect, please try again";
cin >> g;
}
}
if (g = randomnum)
{
cout << "well done";
}
#include <cstdlib>
#include <ctime>
#include <iostream>
usingnamespace std;
int main() {
int g;
int randomnum;
srand(time(0));
// the computer generates a long list of numbers if you put in
// srand(34) it will move to area 34 on that list and select numbers
// from there but if you put in time because time is always changing
// that will make it even more random. So if you run srand(34) over
// again you will get back out the same numbers.
randomnum = rand() % 100 + 1;
// the rand() is the random number the % is the modulus it
// divides hat ever number by 100 and puts the remainder. eg 324
// remainder 24 or 1492 remainder 92 therefore it can go from 0
// to 99 that is why they add the 1 to reach the 100.
cout << "Welcome to the guessing game " << endl;
cout << "Please guess a number" << endl;
cin >> g;
if (g != randomnum)
;
{
while (g != randomnum)
;
{
cout << "That is incorect, please try again";
cin >> g;
}
}
if (g = randomnum) {
cout << "well done";
}
return 0;
}
First off, where'd you hear that explanation about what srand does?
Second, putting semicolons after the conditions of your ifs and whiles is almost always not what you want to do.
Third, g = randomnum uses = instead of ==, which is also probably not what you want to do.
Also (unrelated to your program working or not), a quick few notes about code quality: both of your if statements are currently redundant. As in, you could completely get rid of them without changing your program's overall function. Lemme explain.
The if statement on line 24 of the formatted code (in my post) has the same condition as the while loop on line 27, which is the first statement in the body of your if statement. Therefore, your if statement checks for something that's going to be checked anyway by your loop's condition. It'd be different if that loop was a do-while, where the condition is evaluated at the end of the loop.
Also, the if on line 34 checks for something that will be true once you get out of your loop. Think about it: for your loop to stop running (as it's currently written), g would need to equal randomnum.
-Albatross
P.S. - What do you suppose happens if someone types "thirty" into your program?
#include <iostream>
#include <random>
int main()
{
std::cout << "Welcome to the number guessing game!!!\n";
std::cout << "I have picked a number between 1 and 100.\n\n";
// create a random device
std::random_device rd;
// set a distribution range (1 - 100)
std::uniform_int_distribution<int> dist(1, 100);
// pick and store the random number
unsigned numPicked = dist(rd);
unsigned guess = 0; // stores the number the user guessed
unsigned guessNum = 0; // stores the number of guesses
for (guessNum = 0; guess != numPicked; guessNum++)
{
std::cout << "What would you like to guess? ";
std::cin >> guess;
if (guess < numPicked)
{
std::cout << "\nYou guessed too low!!!\n\n";
}
elseif (guess > numPicked)
{
std::cout << "\nYou guessed too high!!!\n\n";
}
}
std::cout << "\nYou guessed it!!!\n"
<< "It took you " << guessNum << " guesses.\n";
}
Welcome to the number guessing game!!!
I have picked a number between 1 and 100.
What would you like to guess? 50
You guessed too low!!!
What would you like to guess? 75
You guessed too high!!!
What would you like to guess? 65
You guessed too low!!!
What would you like to guess? 70
You guessed too low!!!
What would you like to guess? 72
You guessed it!!!
It took you 5 guesses.
Another game run:
Welcome to the number guessing game!!!
I have picked a number between 1 and 100.
What would you like to guess? 50
You guessed too high!!!
What would you like to guess? 25
You guessed too low!!!
What would you like to guess? 35
You guessed too low!!!
What would you like to guess? 40
You guessed too low!!!
What would you like to guess? 45
You guessed too low!!!
What would you like to guess? 48
You guessed too high!!!
What would you like to guess? 17
You guessed too low!!!
What would you like to guess? 46
You guessed too low!!!
What would you like to guess? 47
You guessed it!!!
It took you 9 guesses.