My first semi cool program! I just need help making it cleaner.

I

Imrahil

Guest
Code:
#include <iostream.h> 

int main(void) 

{ 
	int guess;
	int number=50;
	cout << "Guess the number\n";
	cin >> guess;

	if (guess > number)
		cout << "Lower\n";
	else if (guess < number)
		cout << "Higher\n";
	else
		cout << "It's correct!\n";

	while(guess != number ) 
	{ 
		cout << "Please guess again\n";
		cin >> guess;
		if (guess > number)
			cout << "Lower\n";
		else if (guess < number)
			cout << "Higher\n";
		else
			cout << "You're correct!\n";
	}
	return 0;

}

Try this out for yourself. I kind of was fooling around to get this right and somehow if did! Could someone help me clean it up a bit? But just keep it to the basics, no Functions or anything else higher, just loops and anything lower.

I got this idea from the Clock Game on the Price Is Right. The variable number is the number that you're trying to guess. So assigning any value to that will give you the number you must guess.

Thanks!

Imrahil

(edit by onions - added code tags)
 
Um well you could change 'int' to 'short' and use some indentation.
 
It's just the forum that changes the indentation. I will change it to short thanks.

Imrahil
 
Well done :)
A random number would be nicer, but i'm guessing you arent going to release this ;)
 
Well I could put a random number in, if I knew how. Right now I'm learning about Functions. Is there anyway I can make it so I only have a certain amount of guesses?

Imrahil
 
make another while loop that goes over the whole thing. Set an int to 0, say while (int <= 3), and int++ at the bottom (after a guess)
 
I can't seem to make it work. Could you edit my program then repost it?

Thanks,

Imrahil
 
This is really frustrating me! Someone please help! How do I make it only go for a few guesses?

Thanks,

Imrahil
 
Erm...Well I'm just now learning Java, but I did notice something:
You have a major bug. If you guess wrong the first time, and correctly the second time, it will go straight to "return 0;"
 
But that's what it is supposed to do. It's supposed to end once you guess right.

Imrahil
 
Oops, I traced your code wrong. Why not just have it all in one while loop though?

:cheers:
 
Well I dunno why im doing this but here is a cleaner version:
Code:
int main(void) 
{ 
int guess;
int number=50; // random number is in the math.h file I think, not sure though
cout << "Guess the number\n";
cin >> guess;
// you could prolly find a way to incorporate the first guess into the loop too though
// its not a big deal
if (guess > number)
cout << "Lower\n";
else if (guess < number)
cout << "Higher\n"; //  loop already ends if the number is good, dont need the else

while(guess != number ) 
{ 
cout << "Please guess again\n";
cin >> guess;
if (guess > number)
cout << "Lower\n";
else if (guess < number)
cout << "Higher\n"; // same thing, else is useless
}
cout << "You're correct!\n";
return 0;

}

(edit by onions - added code tags)
 
Code:
#include <iostream.h> 

int main(void) 

{ 
	short guess;
	short number=50;
	int cool=0;
	
	cout << "Guess the number\n";
	cin >> guess;

	if (guess > number)
		cout << "Lower\n";
	else if (guess < number)
		cout << "Higher\n";

	while(guess != number ) 
	{ 
		cout << "Please guess again\n";
		cin >> guess;
		if (guess > number)
			cout << "Lower\n";
		else if (guess < number)
			cout << "Higher\n";
		else 
			cout << "You're correct!";
		cool++;
		if (cool>10)
    {
      cout << "Time's Up!\n";
      break;
    }
	}
	
	return 0;

}

Here's my latest code. Now it finally has a time limit. But when time's up, it also says that the number is either higher or lower.

Thanks,

Imrahil

(edit by onions - added code tags)
 
Well here is what I was thinking for the body of the main method:

Code:
int number = 50;
int guess;
cout << "Guess the number.\n"
cin >> guess;
while (guess != number) {
  if (guess > number) {
    cout << "Lower\n";
  } else if (guess < number) {
    cout << "Higher\n";
  }
  cout << "Guess again.\n";
  cin >> guess;
}
cout << "That's correct\n";
return 0;

You'll have to excuse my conventions and/or syntax errors; like I said I've just started Java, but the concept should be the same.

*edit: you could easily add your guess limit to this too, though I would recommend doing it by using a while loop and nesting your other while loop inside it.
 
an easy random number system is srand() and rand(). srand() stands for SeedRandom, and rand means, well, random.

You can use just rand(), but the workings of rand() aren't really random, but if you call srand before calling rand, it seeds it with a different starting value. You can seed this with the current system time.
Code:
#include <time.h>  //has the time() function, which returns a value of type time_t, which is just a long
#include <iostream>
int main()
{
            srand(time());      //seed the generator

            std::cout<<"random number:"<<rand()<<endl;
            std::cout<<"random number between 0 and 10:"<<rand()%10<<endl;
            std::cout<<"thanks for playing.  Please tip your waitress.\n";
return 0;
}

i didn't compile this code, so if it doesn't work, sorry ;) but you get the idea.
 
Code:
#include <iostream.h> 

int main(void) 

{ 
	short guess;
	short number=50;
	int cool=0;
	
	cout << "Guess the number\n";
	cin >> guess;

	if (guess > number)
		cout << "Lower\n";
	else if (guess < number)
		cout << "Higher\n";

	while(guess != number ) 
	{ 
		cout << "Please guess again\n";
		cin >> guess;
		
		if (cool>10)
    {
      cout << "Time's Up!\n";
      break;
    }
		if (guess > number)
			cout << "Lower\n";
		else if (guess < number)
			cout << "Higher\n";
		else 
			cout << "You're correct!\n";
		cool++;
	}
	
	return 0;

}

Here, I finally made it so you don't have unlimited chances. Thanks a lot guys for all the help.

Imrahil

(edit by onions - added code tags)
 
PLEASE use standard headers!!!

#include <iostream> NO .h

this means you will have to write either using namespace std; or using std::cout; using std::endl; etc. or write std:: infront of cin, cout, endl every time you use them. The last is tedious at first but also the best option.

edit: to clarify,

cin, cout etc. are not reserved words, you can create your own variables called cin, cout etc. if you want (don't though).

So the compiler (and someone reading your code) needs to know that you are using the cin, cout etc. that are in the standard namespace (std) and not your own versions.
 
JESUS CHRIST!! I have absolutely no idea what the **** you guys are talking about!!
 
Frank said:
JESUS CHRIST!! I have absolutely no idea what the **** you guys are talking about!!

Do not feed the ego's. :afro:
 
I feel the need to comment :)

First up, the useage of a short is pointless, anything from a 386 upwards fetches 32bits at a time, which on most platforms is the size of an int, so you might as well use an int and be done with it (the only time shorts are usefull is for packing data together

Also, just to reinforce what HybridM said, use the standard headers and std namespace, the .h header you are currently using is for backwards compatibilty only and as you are new you should really forget you ever saw it.

You could change your loop from a while(){} loop to a do{}while() loop (see, the person who thought a do{}while() was pointless, it isnt) and restructure your output a bit to reduce some of the duplicate code you have (in this instance its trivial duplication, however its good to get into the habit of it)

As a side note;
MadMechwarrior's code is whats know as 'over commenting hell', if anyone codes like that they should be flogged in public for the abuse to my eyes. If your mod code looks like that MadMechwarrior go and fix it now :) (see my notes in the 'Clean Code' thread)
 
well, his comments are tips for imrahil, so I doubt he really comments like that. A do while sounds like a good idea, it really isn't pointless, though I was brainwashed into thinking that at one point. its rather useful, especially for stuff like this where you have to input the first guess or whatever for sure, and it would also remove some of that ugly repetition going on(i.e. the same cins and couts appearing twice)

happy coding
 
rules updated to make you use "
Code:
" tags :p
 
nice little example from Dev-C++ files.

Code:
#include <iostream>
#include <stdlib.h>
#include <time.h>

using namespace std;

void Start ();
void GetResults ();

int  i, j, life, maxrand;
char c;


void
Start ()
{
     i = 0;
     j = 0;
     life = 0;
     maxrand = 6;

     cout << "Select difficulty mode:\n"; // the user has to select a difficutly level
     cout << "1 : Easy (0-15)\n";
     cout << "2 : Medium (0-30)\n";
     cout << "3 : Difficult (0-50)\n";
     cout << "or type another key to quit\n";
     c = 30;

     cin >> c;                   // read the user's choice
     cout << "\n";

     switch (c)
     {
        case '1' : maxrand = 15;  // the random number will be between 0 and maxrand
        break;
        case '2' : maxrand = 30;
        break;
        case '3' : maxrand = 50;
        break;
        default : exit(0);
        break;
     }

     life = 5;         // number of lifes of the player
     srand( (unsigned)time( NULL ) ); // init Rand() function
     j = rand() % maxrand;  // j get a random value between 0 and maxrand

     GetResults();

}


void
GetResults ()
{
     if (life <= 0)
        // if player has no more life then he lose
     {
        cout << "You lose !\n\n";
        Start();
     }

     cout << "Type a number: \n";
     cin >> i;          // read user's number

     if ((i>maxrand) || (i<0)) // if the user number isn't correct, restart
     {
        cout << "Error : Number not between 0 and \n" << maxrand;
        GetResults();
     }

     if (i == j)
     {
        cout << "YOU WIN !\n\n"; // the user found the secret number
        Start();
     }

     else if (i>j)
     {
        cout << "Too BIG\n";
        life = life - 1;    // -1 to the user's "life"
        cout << "Number of remaining life: " << life << "\n\n";
        GetResults();
     }

     else if (i<j)
     {
        cout << "Too SMALL\n";
        life = life - 1;
        cout << "Number of remaining life:\n" << life << "\n\n";
        GetResults();
     }
}


int
main ()
{
     cout << "** Jackpot game **\n";
     cout << "The goal of this game is to guess a number. You will be ask to type\n";
     cout << "a number (you have 5 guess)\n";
     cout << "Jackpot will then tell you if this number is too big of too small compared to the secret number to find\n\n";
     Start();
     return 0;
}
 
Hai my fellow HL2 friends. This be my first post, I see you code C for HL2, that is good! I write similar thing below, this my first go at coding, I not try compile this but should work fine. Tell me what you think :bounce: !

Code:
#include <iostream>
#include <cstdlib>
int main(void)
{
  short iGuess;
  // Mummy said not to play with random numbers. In range 0 to 100
  short iNumber = (rand()%100);
  int iGoes=0;
  int iTurns=5;
  // Daddy never told me about do loops until now
  do
  {
    cout << "Please enter a number";
    cin >> iGuess;
    if (iGuess<iNumber)
      cout << "Lower";
    else if (iGuess>iNumber)
      cout >> "Higher";
    else
      cout << "Winner";
    iGoes++;
  // He never told me about boolean expressions either
  } while ((iGuess!=iNumber)&&(iGoes<iTurns));
  if (iGuess!=iNumber) 
    cout << "out of time";
  return 0;
}
 
By the way, the person who thinks do-while is useless happens to be none other than Bjarne Stroustrup. Creator of this fine language of C++.
 
Do-whiles are good if you want the code inside of them to gaurantee that it is executed once, otherwise you should go for the while/for loops. And another way to limit the # of guesses a user can have is to use a for() loop in there to make sure it only iterates so many times, thus making it easier to read and cleaning up the lines of code.
 
Back
Top