I am a noob that is currently trying to learn C++ for a programming class in highschool, and I am having difficulty with a particular project. Basically, I have to create a number guessing game that lets the user play as many time as he or she wants, while telling him or her if the guess is too high or too low.

However, my if statements aren't working correctly in the loop, they both show up in the command box when the game is run. Furthermore, if I try to use an if else statement for the second condition, the first guess will always turn up as (greater than the number) and every subsequent one afterwards will be lower.

Again, thankyou for assistance if possible.

/*     February 25th, 2008
	Assignment #1 */

#include <iostream.h>
#include <stdlib.h> // For Randomization
#include <time.h> // For Randomization
#include "msoftcon.cpp" // External Application 

void main()

{
	// Setting Colours
	init_graphics();
	set_color(cGREEN,cBLACK);

	srand (time(NULL));	// Starts the randomization process

	char myname[31];
	int guess;
	int tries= 0;
	int number= rand()% 10;
	int choice;
	
	cout<< "			---------------------------\n";
	cout<< "			|THE AMAZING GUESSING  GAME|";
	cout<< "\n			---------------------------";

	cout<< "\n\nPlease enter your first name... ";
	cin.get (myname, 20, '\n'); //	Ends getting the name as enter is hit

	do
	{
		do 
		{
			cout<< "\n\nPlease enter a number (1-10): ";
			cin>> guess;	// Enter in a number

			if (guess < 0 || guess>10)
			{
				cout<< "\nInvalid Value\n";
				return;
			}

			if (guess< number, tries++)
			{
				cout<< "\nYour guess is a bit low...\n";
			}

			if (guess> number, tries++)
			{
				cout<< "\nYour guess is a bit high...\n";
			}

			cout<< "\nYou are on guess "<< tries;
		} while (guess!= number);	// Continue if [guess] is wrong

		cout<< "\n\nYou have guessed the magic number "<< number<< ", in "<< tries<< " tries...";
		cout<< "\n\n\nCongratulations "<< myname<< "!\n\n\n\n\n";

		cout<< "\n\nPress 0 to quit, press any other number to replay: ";
		cin>> choice;

	} while (choice!= 0);	// Replay if anything other than 0 or character is entered.

	cout<< "Goodbye... ";

	return;
}

#include <iostream.h>
#include <stdlib.h> // For Randomization
#include <time.h> // For Randomization
#include "msoftcon.cpp" // External Application

void main()

<iostream.h>, <stdlib.h> and <time.h> should not be used. Use instead <iostream>, <cstdlib>, and <ctime> In terms of <iostream.h> ... it's old. <iostream> is new and good. The other two are used in C++ rather than what you supplied which are C headers.

Void main is bad. Main is defined to return int. use int main()

>> if (guess< number, tries++)
Nope. comas don't go well in if statements. What do you want to test it for? I think you should have tries++ after the if statements before the end of the do-while loop.

>> if (guess> number, tries++)
Same as above, but I reckon that should be an 'else if' rather than only an if.

Our teacher hasn't taught us anything else about the different header files and such, so I am only using what I know. Furthermore, he told us we can use void main and return; instead of return 0;.

If I take out the comas, will the if statements check the entered value correctly? I don't have a compiler or the program at home to test it.

Our teacher hasn't taught us anything else about the different header files and such, so I am only using what I know. Furthermore, he told us we can use void main and return; instead of return 0;.

If I take out the comas, will the if statements check the entered value correctly? I don't have a compiler or the program at home to test it.

I believe that what twomers is saying is that you want to change "if" statements from this:

if (guess< number, tries++)

to this:

if (guess< number)
{
     tries++;
}

In your case you already have some other commands inside the brackets. Keep them in the brackets and just put the part after the comma inside the brackets too, like this:

if (guess< number)
{
     tries++;
     cout<< "\nYour guess is a bit low...\n";
}

Change:
if (guess< number, tries++)

to

tries++;
if (guess< number)
// ...
else if (guess>number )
//...

You should only need to increment tries once.

Our teacher hasn't taught us anything else about the different header files and such, so I am only using what I know. Furthermore, he told us we can use void main and return; instead of return 0;.

I think Salem had a good rant about this kind of situation.... http://www.daniweb.com/forums/post513685-4.html Not all completely relevant to your situation, but some might be.

You can avoid returning things by declaring main as void. He's right. But you can also avoid paying taxes by not doing so. main is defined by the standard to return int. Some compilers flag this as a warning, some errors. Some disregard it completely. But it should return int.
Take a read of this -- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1044841143&id=1043284376

Ok, I have read over the rants and all that and I now realize that some things that are taught are out of date. However, that may change in the future of the course since my teacher wanted us to start small and learn the basics.

Whether or not people agree doesn't matter, since I can't exactly ask him to teach programming more relevant to the modern days. But my question about the if statements were solved, thanks a lot for the help everyone.

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.