Hey my names Aj. I finally decided to take on the ultimate task of learning to program. Its been a while that i have had an interest but did not start until about 1 week ago to start watching tutorials. Some things really confuse me, but i will eventually look further into them. Well the whole point of this post is to get your advice and criticism on my first advanced program. It took me a while, yes i know pathetic, but please remember i just started. Check it out.
*/
main.cpp
My First Game
Created by AJ Bellman on 9/24/13.
Copyright (c) 2013 AJ Bellman. All rights reserved.
*/
#include <iostream>
#include <string>
usingnamespace std;
int main ()
{
string response;
int guess;
int answer = 178;
string fun;
do {
cout << "Do you want to play a game? yes or no" << endl;
cin >> response;
if (response == ("yes"))
cout << "Okay. Im thinking of a number." << endl;
elseif (response == ("no"))
cout << "Okay. Maybe some other time" << endl;
else
cout << "Please enter 'yes' or 'no'" << endl;
} while (response != ("yes"));
do {
cout << "Can you guess it? Give it try!" << endl;
cin >> guess;
if (guess > answer)
cout << "Your too high, guess lower" << endl;
elseif (guess < answer)
cout << "You guessed too low. Try a higher number" << endl;
elseif (answer)
cout << "Great Job, You got the number" << endl;
else
cout << "Plese Enter your Guess" << endl;
} while (guess != answer);
cout << "Did you have fun?" << endl;
cin >> fun;
if (fun == ("yes"))
cout << "You did? Awesome!" << endl;
else
cout << "Maybe next time you will have fun :(" << endl;
return 0;
htirwin thanks. Also, how would i make it so that it generates a random number? Also, when the question comes up to guess the number, if i put anything other than a number , for example "hi", i get "Your too high" or "Your too low" over and over and over again. How would i fix that?
For your first question, there are two ways to generate a random number.
The easiest way is this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <string>
usingnamespace std;
int main() {
srand(time(0)) // Seed the random number generator with current time
// Generate a random number between 1 and 1000
int answer = rand() % 1000 + 1;
// etc...
}
However, the other way to do it (which gives "more random" numbers) is to do it like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
#include <string>
#include <chrono>
#include <random>
usingnamespace std;
int main() {
// Instantiate a Mersenne Twister Generator, seeded to the
// system time since epoch.
mt19937 generator (chrono::system_clock::now().time_since_epoch().count());
// Create a distribution, forcing value returned by generator
// to be within (inclusive) 1 and 1000
uniform_int_distribution<int> distribution (1, 1000);
int answer = distribution(generator());
// etc...
}
However, in your case, you don't really need high-efficiency, totally-random numbers, so you should probably just go with the first one (its easier to understand!). However, it is still useful to know the second one, in case you find something that you really want to have "properly" random numbers.
Line 31 should convert stream content into an integer. "hi" starts with characters that definitely are not numbers, in the op>> fails. Stream cin is now in failed state and the "guess" was not updated. Any further from cin will fail too. Furthermore, guess was not initialized, so its value is undefined.
You have to test the read succeeds. If it fails, you have to clear the cin stream so that you can input again.
keskiverto is completely correct. However, just incase you don't know what on earth he's on about, here is a quick example (yes, I know I like examples):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
#include <limits>
usingnamespace std;
int main() {
int num;
while (!(cin >> num) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid Input... " << end;
}
return 0;
}