#include <iostream.h>
#include <time.h>
#include <stdlib.h>
#define MAX_RANGE 1000

main ()
{
long value, guess;
int tries = 0;

srand ( time (NULL) );         // Initialize random generator
value = rand()%MAX_RANGE+1;    // Get random between 1 and MAX_RANGE

     cout << "A random number has been selected between 1 and 1000." << endl;
     cout << "Try to guess the value.";
     cin >> guess;
     tries ++;
     while (value != guess)
     {
           if (guess > value)
           {
                     cout << "The value is lower than " << guess << " Try again." << endl;
           }
           else if (guess < value)
           {
                     cout << "The value is higher than " << guess << " Try again." << endl;
           }
           else
           {
                     cout << "You guessed the value!!" << endl;
                     cout << "It took you " << tries << " tries to guess the correct value." <<              endl;
           }
           cout << "Guess another value." << endl;
           cin >> guess;
           tries ++;
     }
     return 0;
}

So I've written this code here to guess higher and lower numbers. It compiles and runs properly, but I have a question regarding compiling it. Dev C++ comes with a GUI compile button, so the command prompt is unnecessary. Unfortunately, when I use this button, after the program is finished, the command prompt automatically closes. So I was curious how to compile using the command prompt to avoid this problem. Additionally, I know that I need to specify the directory where my code is located. How can I changed the directory the computer is currently looking at?

By the way, how come code tags aren't working properly... Shoulden't this be numbered now...

Ancient Dragon commented: Thans for using code tags correctly +21

First, thanks for taking the time to read the rules about code tags :)

>>By the way, how come code tags aren't working properly... Shoulden't this be numbered now
you have to add the language like this:

[code=c++] // your code here

[/code]

>>Unfortunately, when I use this button, after the program is finished, the command prompt automatically clos
If you want the program's window to remain open after the program finishes then you have to add another line just before main() returns to prompt for keyboard input so that the program will pause at that point.

If you want the program's window to remain open after the program finishes then you have to add another line just before main() returns to prompt for keyboard input so that the program will pause at that point.

I read in another thread that the cin.get() function (I believe it was called) could be used, but I also remember reading elsewhere that compiling using the command prompt achieves the same effect. Any idea how to do this as well?

Apparently I'm not allowed to do more than 1 edit per post, otherwise I'd fix that code tag. Sorry about that.

When you compile using the command prompt you first have to open a command window. That window doesn't disappear after compiling or running your program.

So I click start, go to run, type CMD. Now I'm in the command prompt. What's the command line that runs the compiler?

I don't know -- I always use the IDE. But I do know its a lot more complicated than simply putting cin.get() at the end of main().

#include <iostream.h>
#include <time.h>
#include <stdlib.h>
#define MAX_RANGE 100

main ()
{
long value, guess; 
int tries = 1;

srand ( time (NULL) );         // Initialize random generator
value = rand()%MAX_RANGE+1;    // Get random between 1 and MAX_RANGE

     cout << "A random number has been selected between 1 and 1000." << endl;
     cout << "Try to guess the value.";
     cin >> guess;
     

     while (value != guess)
     {
           if (guess > value)
           {
                     cout << "The value is lower than " << guess << " Try again." << endl;
           }
           else if (guess < value)
           {
                     cout << "The value is higher than " << guess << " Try again." << endl;
           }
           else
           {
                     cout << "You guessed the value!!" << endl;
                     cout << "It took you " << tries << " tries to guess the correct value." << endl;
                     int dummy;
                     cin.get() >> dummy;
           }
           cout << "Guess another value." << endl;
           cin >> guess;
           tries ++;
     }
     cin.get();
     return 0;
}

So I tried the strategy without the command prompt. I feel really stupid. I cannot figure how to make this work. Wherever put that cin.get(), the window always closes.

That problem has nothing to do with the compiler you are using. On line 37: when you enter a numeric value cin will not remove the <Enter> key from the keyboard buffer. You need to strip that out. One way is to call cin.ignore() after line 37.

That problem has nothing to do with the compiler you are using. On line 37: when you enter a numeric value cin will not remove the <Enter> key from the keyboard buffer. You need to strip that out. One way is to call cin.ignore() after line 37.

alright. So, on the bright side, I was able to get the program to stop when I placed the cin.ignore() function after line 37. Unfortunately, the text telling the player that he has would still not display.

again on the bright side, I decided that I would try to add a new feature to my program. I made it so that the computer simultaneously calculates the random value by guessing numbers in between known high and low values. But now the program doesn't stop at all and the cin.ignore() function seems to be having no effect at all. Any ideas whats causing this?

Here is the code:

#include <iostream.h>
#include <time.h>
#include <stdlib.h>
#define MAX_RANGE 1000

long value, guess; 
float computer_guess;
int tries = 1, computer_tries = 1, max_computer_guess, min_computer_guess;

int Computer ()  //The function computer determines the most efficient method of calculating the random value.
{
	if (computer_tries == 1)
	{
		computer_guess = MAX_RANGE / 2;
	}
	else 
	{
		computer_tries ++;
        while (computer_guess != value)
        {
			if (computer_tries == 2)
                        {
				max_computer_guess = MAX_RANGE;
				min_computer_guess = 0;
                         }
			 if (computer_tries > 2)
			{
				if (computer_guess > max_computer_guess)
				{
					max_computer_guess = computer_guess;
				}
				if (computer_guess < min_computer_guess)
				{
					min_computer_guess = computer_guess;
				}
			}
            if (computer_guess > value)
            {
				computer_guess = (max_computer_guess + computer_guess) / 2;
            }
            else if (computer_guess < value)
			{
				computer_guess = (min_computer_guess + computer_guess) / 2;
            }
		}
	}
	return (computer_tries);
}

main ()
{
srand ( time (NULL) );         // Initialize random generator
value = rand()%MAX_RANGE+1;    // Get random between 1 and MAX_RANGE

     cout << "A random number has been selected between 1 and 1000." << endl;
     cout << "Try to guess the value.";
     cin >> guess;
 
     while (value != guess)
     {
           if (guess > value)
           {
                     cout << "The value is lower than " << guess << " Try again." << endl;
           }
           else if (guess < value)
           {
                     cout << "The value is higher than " << guess << " Try again." << endl;
           }
           else if (guess == value)
           {
                     cout << "You guessed the value!!" << endl;
                     cout << "It took you " << tries << " tries to guess the correct value." << endl;
                     cout << "The computer took" << Computer () << "tries";
                     if (tries < computer_tries)
                     {
						 cout << "Congratulations, you beat the computer.";
					 }
					 else
					 {
						 cout << "The computer beat you.  Better luck next time.";
					 }
					 cin.ignore();
           }
           cout << "Guess another value." << endl;
           cin >> guess;
           cin.ignore();
           tries ++;
		   
     }
     return 0;
}

EDIT:
eww... that looks awful. Let me attach a copy of the code so that it's more legible.

Hi,
Maybe this woud be helpfull.
Run cmd and enter in cmd filename path.
Like this.
C:\dev-cpp\untitled1.exe

Or you can use any Text editor.
Use for example:
Open notepad
Write a code like this

#include <stdio.h>

int main()
{
      int x=1;

      printf( "%d", x);

return 0;
}

Then save it ;example C:\dev-cpp\untitled1.cpp
run CMD
type cd C:\dev-cpp\bin
Use this one for example compiler
type mingw32-g++ C:\dev-cpp\untitled1.cpp -c
This mind that he create ( .OBJ or .O) in bin directory.
type mingw32-g++ C:\dev-cpp\untitled1.o -o C:\elite.exe
That mind that he create exe file of course.
Now if you wana to include resource ,lib files ,and all other stuff you must learn how to compile.
I never compile like this because i have dev-cpp who BUTTON Sortcut F9 and all is done.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.