#include <iostream>
#include <cstdio>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
	int rNumber = 0;
	int uNumber = 0;
	const int maxNum = 10;
	const int minNum = 1;
	const int range = maxNum - minNum;
	int counter = 0;
	
	//declare random formula
	srand((unsigned int)time(NULL));
	rNumber = 1 + rand() % (range + 1);

	cout << "Welcome To The Guessing Game!\n";

	//goes infinite until the right number is selected or they select -1
	do{
		while(uNumber != rNumber || uNumber != -1)
		{
			cout << "Please Select A Number From 1 Through 10 To See If It Matches The Random Number.\n\n";
			cout << "Please Enter -1 To Exit The Program.\n\n"; 
			cout << "Please Enter A Number: \n\n";
			 cin >> uNumber;
			counter++;

			if(rNumber = uNumber){
				cout << "You have guessed wisely!\n\n";
				cout << "Good Job!\n";
			}
			else{
				if(rNumber > uNumber){
					cout << "Too High, Try Again!\n\n";
				}
				else{
					if(rNumber < uNumber){
						cout << "Too Low, Try Again!\n\n";}
				}
			}
		}//End While
		cout << "You Tried " << counter << " Times\n\n";
		cout << "Thank You For Playing The Guessing Game!\n";
	}
return 0;
}

//The Only problem I have with this is that the code runs every time but it tells me that the selection I performed is correct and does not show action that it is creating a random number and i can't fix it, can somebody help me on this?

Recommended Answers

All 3 Replies

Ok First off, there are two while constructions in C++ , do { } while(condition); and while(condition) { } There fore you don't need the do after the welcome statement.
In is current form this program should not have compiled.

You have written if(rNumber = uNumber) This is correct C++ but not what you want. It assigns uNumber to rNumber and then tests to see if rNumber is true, i.e. if rNumber is not zero. Therefore your code will always find this to be true except if the user enters zero.

The reason that you didn't find that error almost instantly, is that you have not enabled warning on your compile. If it is g++ then use the -Wall flag, if it is something else then please find out how to and enable it. Developing code without warning enabled for almost any size project will just take extra time debugging.

The rule is that any errors that you can move from run-time errors to compile errors are to the programmers advantage. This is the reason for much of the const correctness, for the type and cast requirements, as well as function/class definitions. Make use of this.


FINALLY: Please use code tags. [It is in the link at the top of this forum:
Read this before posting]

You must check that with files... not the normal documents

Thanks For the help, I eliminated the "do {}" and got the code working now.

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.