Should I have more knowledge at this point?

I've just finished chapter 2 in the book "Beginning C++ Through game programming Third Edition" Chapter 2 involved learning if statements, switch statements, while and do loops, and generating random numbers. At the end of the chapter it said to try and change the Guess my Number program so that the player picks a number and the computer has to guess it.
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
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
    srand(static_cast<unsigned int>(time(0))); //seed random number generator

    int secretNumber = rand() % 100 + 1; //random number between 1 and 100;
    int tries = 0;
    int guess;

    cout << "\tWelcome to Guess My Number\n\n";

    do
    {
        cout << "Enter a Guess:  ";
        cin >> guess;
        ++tries;

        if(guess > secretNumber)
        {
            cout << "Too High!\n\n";

        }
        else if (guess < secretNumber)
        {
            cout << "Too Low!\n\n";
        }
        else
        {
            cout << "\nThat's it! You got it in " << tries << " guesses!\n";
        }
    }while(guess !=secretNumber);
}


I get all of this but I can't seem think on how to re-write it so I pick a number and the computer has to guess it, so should I have more knowledge at this point? I feel like I have it in my head but I can't get it out
try something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int min, maks, x, y;
	char comp;
	cout<<"Enter the minimum and maximum values, and the value I have to guess";
	cin>>min>>maks>>x;
	while (true)
	{
		y=(min+maks)/2;
		cout<<y<<endl;
		cin>>comp;
		if (comp=='=')
		{
			cout<<"I did it!";
			break;
			}
		if (comp=='<') maks=y;
		else min=y;
		}
You need more practice, that's all. Even if you know how everything works you also need learn to put things together. You probably learn more by trying really hard to solve this yourself. It can easily get out of hand if you try to solve everything at the same time. Do everything in small steps instead. Focus on the first thing you need to do first. That is to let the user enter a number. When you have done that you move on to the next thing.
Last edited on
It isn't that hard.

Just think, right now the program generates a random number between 1 and 100 and asks the user to enter a guess.

Then compares and tells the user if it is too high or too low.

What your "new" program should is;
Ask the user to enter a number (lets' say 42)
Generate a random number between 1-100.
If generated number is higher than 42 (let's say 82), then generate a new number between 1-81 - there's no point in generating a number higher than 82; the computer already knows that 82 and above is too high.
The new random number is 24; which is lower than 42 - so this time we generate a new number between 25 and 81.

Continue like that until the computer has found the number, and this is also called for binary search and worse case is about 7-8 guesses ( I just can't remember that right now).

So no, you don't need more knowledge. This can all be done with if-statements, rand() and a loop.

Hopefully I didn't say too much now... In that case someone is allowed to give me a slap.
I feel like I have it in my head but I can't get it out


That's probably the hardest part of programming. Taking an idea that works in your head, and translating it to something that works in a computer. Just takes practice.

Just break up the problem into individual steps and it should be a little easier.
Take a look at this thread and try to solve as much problems as you can. It is great. I watched a whole tutorial series on youtube. I thought i knew everything when i was ready. But i knew nothing until i had tried to do it myself.

http://cplusplus.com/forum/beginner/75558/

There are a few good threads you can find with problems if you google around a bit
The difference between your original program and that you have to write is that in the last you should select a random number from an appropriate interval instead of entering it from the console.
OK, doing programming exercises is boring and just plain sucks.

What I've always done is pick a project that seems challenging yet still doable by what you perceive your skill level to be and do it. You will fail and million times and be disheartened at times but every time you go to fix up your code and try and debug it you'll learn something. This could take you months depending upon how big the project is. And you may fail altogether and have to give it up. But you'll learn quite a lot in the process, and it will sink in at a deeper level than just doing exercises. Plus you'll be more motivated. I honestly never heard of someone getting good at programming from doing exercises in a book. Most just set out to do something and learn a little bit about their chosen language along the way.
^this

Go do something you want to do. Surely you started programming for a reason, go learn how to do whatever is you want. Just remember, no matter how hard something may sound it is still programming using the exact language you have been learning.
Thanks, guys. I really appreciate all this advise, and I now feel a more confident to make more programs so I can expand my knowledge :D
Especially by saying "Take it one step at time" that seems to do make it easier.
Thanks again :D
Last edited on
Topic archived. No new replies allowed.