I'm really new to programming. A matter of fact, I started today. My friend made a game like this in a batch, and I wanted to replicate it with programming. You know, get some experience. I will admit, I've learned a lot on this project alone, but at this point I'm stumped.
#include <iostream>
usingnamespace std;
#define NEWLINE = \n
int main ()
{
int svalue;
int yvalue;
int tvalue;
tvalue = 0;
svalue = rand();
cout << "Guess the Number!";
loop:
'\n';
cin >> yvalue;
if
(svalue == yvalue)
cout << "You guessed it!";
return 0;
if
(svalue > yvalue)
cout << "Guess Higher!";
goto loop;
if
(svalue < yvalue)
cout << "Guess Lower!";
goto loop;
}
My problem isn't making all the different parts run in general, but organizing it so it will run together. Keep in mind, this is one of many drafts. There may be some crap in there, and it's lacking a lot. All I want is for it to randomly select a number, then you guess it. If it's to low, it says guess higher, and vice-versa. Not that hard, but when you have terminate it afterward, it goes beyond me. I think messed up on the definition and the tvalue is in there if needed.
Make sure to call srand() to seed rand() (usually call it with time(NULL)). I think you meant to cout << the random '\n'; on line 16. For ifs, if they are multiple lines, you must put { } around the code, i.e.:
Since you've already solved your problem, I figured I might throw in a few pointers from what I've learned. Hopefully these will help you. I find I always learn better when I see my own code modified to work better or use better techniques, because it hits home in my brain. ^^
// Includes. Note: I don't know if time.h is
// usable outside of Visual Studio.
#include <iostream>
#include <time.h>
usingnamespace std;
// In C++ I would reccommend using a global const variable
// instead of a #define. It can't be changed and is visible
// everywhere, but there are a couple advantages such as being
// able to see it in a debugger, and the compiler knowing what
// type it is suppose to be.
constint MAX_RANDOM_VALUE = 100;
int main()
{
// Like firedraco said, seed the random number generator
srand(time(NULL));
// I tried to use names that were more self explanatory.
// I'm going to assume that you know how to use the modulus
// operator (%), but if not there are tutorials on it. This
// line will generate a random number from 1-MAX_RANDOM_VALUE
int goal = (rand() % MAX_RANDOM_VALUE) + 1;
int input;
// Get the initial input after a prompt...
cout << "Guess the number!" << endl;
cin >> input;
// This is an infite loop. I can use break; to exit the
// loop whenever I want, which is only if the user guessed
// the random number. Otherwise this will loop indefinitely.
do
{
if (input == goal)
{
cout << "You guessed it!" << endl;
break;
}
elseif (input < goal)
{
cout << "Guess Higher!";
}
else
{
cout << "Guess Lower!" << endl;
}
cin >> input; // EDIT: Forgot this the first time.
} while (true);
// Notice I did not need to use goto. I've heard from numerous
// programmers now that using goto in C++ is unnecessary and
// generally a bad idea.
return 0;
}
However in this case (IMHO), the while loop looks cleaner, so I would go for the while loop.
EDIT: I noticed a major problem...you need to move the cin >> input; into the loop, otherwise you will be stuck forever if you don't guess the correct number.
Ack! I meant to get an initial input, then start looping with input re-entered at the end of the loop each time but forgot to type it. Thanks for the catch! I've edited it to correct it. Thanks again!