after just a quick look:Quote:
Originally Posted by cgod
- if it's between 1 and 10, why are you using mod 5?
- you already have larger, smaller, and exactly covered, what else are you trying to do here?
- don't use system("pause"). ever.
Printable View
after just a quick look:Quote:
Originally Posted by cgod
- if it's between 1 and 10, why are you using mod 5?
- you already have larger, smaller, and exactly covered, what else are you trying to do here?
- don't use system("pause"). ever.
Yeah ok but i just changed cerin's code so it would work he can do all the minor adjustments.I found this code from awhile ago maybe it will be usefull to cerin.
Code:#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand((unsigned)time(0)); // random number seed
int numberToGuess = rand()%5; // generate random number between 1-5
bool win = false;
int guessNumber = 0;
for (int numberGuesses = 0; numberGuesses < 5; numberGuesses++)
{
cout << "The computer has generate a random number try and guess it " <<endl;
cout << "Guess : ";
cin >> guessNumber;
if (guessNumber == numberToGuess)
{
cout << "Correct "<<endl;
win = true;
break;
}
else if (guessNumber > numberToGuess)
{
cout << "To high try a lower number"<<endl;
win = false;
}
else if (guessNumber < numberToGuess)
{
cout << "To low try a higher number "<<endl;
win = false;
}
}
if (!win)
{
cout << "You have lost the game try again "<<endl;
}
cin.get();
return 0;
}
try again...Quote:
Originally Posted by cgod
Quote:
Originally Posted by major_small
:DCode:int numberToGuess = 2; // generate random number between 1-5
Trust Prelude to come up with something like that.. :rolleyes:
Hm, I'm going to try doing Impossible Thing to Do #1.
Trust me it does matter. Its easier to read, and when your using the [ PHP ]. It color codes it so its easier to tell whats, a comment, whats a function, ect. :DQuote:
Originally Posted by chrismax2
It still doesn't change everytime I run itCode:srand ( (unsigned int) time (0));
int x = rand() / (RAND_MAX / 10 + 1 );
(int)( (double) rand() / (RAND_MAX +1) * 10);
int guess;
Might I make a recommendation????
Go to a C/C++ textbook. Open it to page 1 and begin reading from there. The moment you try to gloss or skim over anything...STOP and re-read it again. You've obviously not grasped a single programming concept. Not even the most basic of principles or logic. And now you're turning a very very very simple statement into the most convoluted mess I've seen in a while. How on earth did you migrate from x = rand() % 10, to that indecipherable muck srand ( (unsigned int) time (0)); int x = rand() / (RAND_MAX / 10 + 1 ); (int)( (double) rand() / (RAND_MAX +1) * 10); int guess;?
Good god man!
Really, honestly I'm not tryin to be rude, but you've got to start from the beginning otherwise your confusion will just get worse. I've seen you struggling through other pieces of code in other threads, and I really mean it when I say I would rather see you progress and succeed instead of struggling and losing interest.
UMMMMMM...... really the first things I did were ACTUALLY READ some basic tutorials from http://www.geocities.com/tutorialperson/ (I'm on my mac laptop it won't let me make it a link) and then I READ the tutorials from this site.
I'm working on getting a book. The code I had was posted and in the FAQ.
I really hope this helps...
This code is extremely straightforward, and if you still have questions after reading the FAQ, this post, and all the previous posts concerning the generation of random numbers and seeding the random generator, maybe programming isn't your bag, dude.Code:#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
//First, we seed the random number generator based on time with srand
//so that we don't get the same random numbers every time
srand( time(NULL) );
//Now we assign a variable to a random number
int foo = rand();
//Now we assign a variable to a random number between 1-10
int bar = (rand() % 10) + 1;
//Now we print 10 different random numbers between 1- 100
for( int x = 0; x < 10; x++ )
{
bar = (rand() % 100) + 1;
cout <<bar <<endl;
}
cout <<'\n' <<endl;
//Now, we have them guess a number between 1 - 10
int number = (rand() % 10) + 1;
int guess = 0;
while( guess != number )
{
cout <<"Guess my number(between 1 - 10): ";
cin >>guess;
//Is guess between 1-10?
if( guess < 1 || guess > 10 )
cout <<"I said between 1-10, try again dude." <<endl;
//Is it greater than our number?
else if( guess > number )
cout <<"Too high, guess lower." <<endl;
//Is it lower than our number?
else if( guess < number )
cout <<"Too low, guess higher." <<endl;
}
//Since we've left the loop, we know they guessed correctly
//...or there was a freak of nature, but we will ignore that case
cout <<"You got it! Thanks for playing." <<endl;
}
Alright I got it now thanks for all the help. One of my teachers is going to look if she can find a mentor for me at the college.