# A noob in need of help!

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 11-20-2003
wiramu
A noob in need of help!
HI! Im trying to make a C++ console app. where you give the computer a number and it tries to guess it by process of elimination.
You put in a number for it to guess,
then it sees if it is bigger or smaller than 5000 and goes from there....

Code:

```while (Guess != SecretNumber)         {                 Sleep(32);  //slow it down                 if(Guess > SecretNumber)                 {                         cout << "Ok, so it's bigger than "<< Guess <<"\n\n";                         Guess = Guess /2;                         cout << "Um... Is it... " <<Guess<<"??\n";                 }                 else if(Guess < SecretNumber)                 {                         cout << "Ok, so it's smaller than "<<Guess<<"\n\n";                         Guess = Guess - (Guess/2);                         cout << "Um... Is it... " <<Guess<<"??\n";                 }                 else if (SecretNumber == Guess)                 {                         cout << "yay\n";                 }```
Could please tell me where I am going wrong and maybe what I need to do [/plea]
;D
Thanks :p
• 11-20-2003
major_small
Re: A noob in need of help!
I'm tempted to say this is a generally bad algorithm, but here goes...
Code:

`Guess = Guess - (Guess/2);`
should be something like:
Code:

```Guess+=(Guess/2); //or Guess = Guess + (Guess/2);```
you can also run into this problem... Guess comes to 3, and their number is two.

Code:

```guess>number   guess/2 guess<number   guess+=guess/2```
this is how it looks to a computer (kinda)
Code:

```3>2   3/2=1 1<2   1/2=0; 0+1=1;```
then it goes into an infinite loop...
perhaps you should go with the more conventional version of this program: the program picks a random number and the user guesses it.
• 11-20-2003
XSquared
Or
Code:

`Guess *= 1.5`
and
Code:

`Guess *= 0.5`
• 11-20-2003
Prelude
Binary search anyone?
Code:

```#include <iostream> #include <stdlib.h> #include <windows.h> using namespace std; int main() {   int number;   int l, h, m;   cout<<"Enter a number between 0 and 5000: ";   if ( !( cin>> number || number < 0 || number > 5000 ) ) {     cerr<<"I said a number between 0 and 5000...";     return EXIT_FAILURE;   }   l = 0;   h = 5000;   cout<<"Hmm";   while ( true ) {     m = ( l + h ) / 2;     cout<<'.';     if ( number < m )       h = m - 1;     else if ( number > m )       l = m + 1;     else       break;     Sleep ( 500 );   }   cout<<"It's "<< m <<", I knew it!"<<endl; }```
• 11-20-2003
XSquared
Prelude! How could you!?!?!!

#include <stdlib.h>

#include <cstdlib>

Sheesh. I expected more from you.
• 11-20-2003
wiramu
Cool thanx!
I'll get to work!
If I have any more troubles, now I know where to go! :D

P.S. The programme I made prior to this one was one where the computer makes a random number and the user guesses it. [it actually worked!]

P.P.S. Think I might have a crack at making pong after this... these forums are full of pong programmes!!
• 11-20-2003
Dante Shamest
> Prelude! How could you!?!?!!

I bet she was using Dev-C++, which automatically inserts <stdlib.h> instead of <cstdlib>.
• 11-20-2003
major_small
i completely forgot that Dev adds that stuff in... whenever I start a new project, I always erase everything it throws in there... prewritten code just kinda annoys me...
• 11-20-2003
Prelude
>Sheesh. I expected more from you.
Hmm? What do you mean? (I actually know what you mean, but I want to hear your reasoning before I give you mine ;))

>I bet she was using Dev-C++, which automatically inserts <stdlib.h> instead of <cstdlib>.
Nope, I wrote that in MSVC++ 6. And the choice of header was quite deliberate. ;)
• 11-20-2003
XSquared
My reasoning is that since it is a C++ program, it should use C++ headers. Since there is a C++ conversion of <stdlib.h> (<cstdlib>), you should use it. I figured that you would at least conform to the standards.

• 11-20-2003
Prelude
>Since there is a C++ conversion of <stdlib.h> (<cstdlib> ), you should use it.
The C++ headers wrap everything in the std namespace. This makes things like printf calls look funny. Also, my version of MSVC++ doesn't do this, so if I forget the std:: prefix, I'll be programming outside of the standard unless I use a using directive on std, which I prefer not to do unless I'm exceptionally lazy.

>I figured that you would at least conform to the standards.
The C++ standard gives us the c* headers, but it also requires conformance with standard C. This means that the C .h headers must be supported in every C++ implementation. So I've kept to the standards. ;)

1 part laziness, 1 part sadistic intentions, and 2 parts obscure standards interpretation. I do it because I don't like to deal with VC++6's problems while compiling with multiple compilers, and I like to shoot down arguments against a seemingly non-standard style. ;)
• 11-20-2003
JaWiB
>>This makes things like printf calls look funny.

Well printf calls already look funny in my opinion...cout is prettier :D

I guess I can't argue with you though...
• 11-20-2003
Prelude
>Well printf calls already look funny in my opinion...cout is prettier
A matter of opinion to be sure. But I don't usually use C streams in C++ for the sake of good style and because they mesh awkwardly with std::strings.
• 11-21-2003
wiramu
I've almost got my programme (btw is it programme or program??) working (haven't had much time to work on it :@)

P.S:
Quote:

...I bet she was using Dev-C++.....
'She' = me
Me = Wiramu
Wiramu = a 'he'
confused?
Good! 'Cos I'm wiramu, and I'm a dude!
o_0

PPS: Im using MS VS version7
;D
• 11-21-2003
Cat
Quote:

Originally posted by Prelude
The C++ standard gives us the c* headers, but it also requires conformance with standard C. This means that the C .h headers must be supported in every C++ implementation. So I've kept to the standards. ;)
All the C .h headers are officially listed as deprecated, so they apparently aren't going to be with us forever.

Also, C++ has never premitted all standard C code to compile; even before C99, C++ and C had different rules in a number of areas. One that comes to mind is C's automatic conversion of a void * to any other pointer type which is forbidden in C++. So I think the implication that standard C is standard C++ as well is a little misleading.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last