Simple guessing game, or a mistake?

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>

using namespace 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.:

1
2
3
4
if(a == b) {
   cout << "you won!";
   return 0;
}
Thanks a lot. That fixed the problem.
No problem
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. ^^

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Includes. Note: I don't know if time.h is
// usable outside of Visual Studio.
#include <iostream>
#include <time.h>

using namespace 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.
const int 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;
      }
      else if (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;
}
Last edited on
Nod, goto is evil. But read this: http://www.parashift.com/c++-faq-lite/big-picture.html#faq-6.15

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.
Last edited on
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!
Remove this:
 
#define NEWLINE = \n 

It is neither necessary nor is the syntax right.

Maybe try endl (from namespace std), if you really do not like putting \n inside of a string.
Topic archived. No new replies allowed.