Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Magic Number !!

Reply
Thread Tools

Magic Number !!

 
 
WELCOME ###
Guest
Posts: n/a
 
      05-10-2007
Hello every one.
The following is code built by C++ \ CLI . It's built well :
--------------------------------------------------
#include "stdafx.h"

#include <cstdlib>

using namespace System;

int main(array<System::String ^> ^args)

{

int magic;

int guess;

Console::Write(L"Enter Your Guess Number : ");

guess=Console::Read();

Random^ r = gcnew Random();

r->Next();

magic=7;

if(guess=7)

{

Console::WriteLine(L"BRAVO");

}

else

{

Console::WriteLine(L" SORRY..");

}

return 0;

--------------------------------------

When you compile this code,it compiles well except if & else.If one operates
the other not!!!

Please, can any person Help??!!

Thanks

===================================


 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      05-10-2007
WELCOME ### wrote:
> Hello every one.
> The following is code built by C++ \ CLI . It's built well :


Please stop posting C++/CLI stuff here, it's *off topic*.

--
Ian Collins.
 
Reply With Quote
 
 
 
 
WELCOME ###
Guest
Posts: n/a
 
      05-10-2007
Oh
It's C++, and my code problem is C++.
What's wrong..Friend?!!
You can"t fix it?!!!!
======================
"Ian Collins" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> WELCOME ### wrote:
>> Hello every one.
>> The following is code built by C++ \ CLI . It's built well :

>
> Please stop posting C++/CLI stuff here, it's *off topic*.
>
> --
> Ian Collins.



 
Reply With Quote
 
davidrubin@warpmail.net
Guest
Posts: n/a
 
      05-10-2007
On May 9, 10:49 pm, "WELCOME ###" <(E-Mail Removed)> wrote:
> Oh
> It's C++, and my code problem is C++.
> What's wrong..Friend?!!
> You can"t fix it?!!!!
> ======================"Ian Collins" <(E-Mail Removed)> wrote in message
>
> news:(E-Mail Removed)...
>
> > WELCOME ### wrote:
> >> Hello every one.
> >> The following is code built by C++ \ CLI . It's built well :

>
> > Please stop posting C++/CLI stuff here, it's *off topic*.

>
> > --
> > Ian Collins.



It seems you've succumb to one of the top 3 programming errors.

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      05-10-2007
WELCOME ### wrote:

Please don't top post.

> "Ian Collins" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> WELCOME ### wrote:
>>> Hello every one.
>>> The following is code built by C++ \ CLI . It's built well :

>> Please stop posting C++/CLI stuff here, it's *off topic*.
>>


> Oh
> It's C++, and my code problem is C++.
> What's wrong..Friend?!!
> You can"t fix it?!!!!
> ======================


The code you posted isn't C++, at least according to my almost but not
quite standard conforming compiler:

CC x.cc
"x.cc", line 1: Error: Could not open include file "stdafx.h".
"x.cc", line 5: Error: System is not defined.
"x.cc", line 7: Warning (Anachronism): Using array as a template without
a declaration.
"x.cc", line 7: Error: System is not defined.
"x.cc", line 7: Error: String is not defined.
"x.cc", line 7: Error: Operand expected instead of ">".
"x.cc", line 7: Error: A class template name was expected instead of array.
"x.cc", line 15: Error: Console is not defined.
"x.cc", line 15: Error: The function "Write" must have a prototype.
"x.cc", line 17: Error: Console is not defined.
"x.cc", line 17: Error: The function "Read" must have a prototype.
"x.cc", line 19: Error: Random is not defined.
"x.cc", line 19: Error: r is not defined.
"x.cc", line 19: Error: gcnew is not defined.
"x.cc", line 19: Error: Badly formed expression.
"x.cc", line 21: Error: r is not defined.
"x.cc", line 29: Error: Console is not defined.
"x.cc", line 29: Error: The function "WriteLine" must have a prototype.
"x.cc", line 37: Error: Console is not defined.
"x.cc", line 37: Error: The function "WriteLine" must have a prototype.
"x.cc", line 41: Error: "}" expected instead of EOF.
20 Error(s) and 1 Warning(s) detected.

--
Ian Collins.
 
Reply With Quote
 
David Harmon
Guest
Posts: n/a
 
      05-10-2007
On Thu, 10 May 2007 12:49:20 +1000 in comp.lang.c++, "WELCOME ###"
<(E-Mail Removed)> wrote,
>Oh
>It's C++, and my code problem is C++.
>What's wrong..Friend?!!
>You can"t fix it?!!!!


1. Don't top post.
2. "gcnew" and everything that goes with it is not C++.
3. Stop putting assignments in "if" statements.

 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      05-10-2007
On May 10, 2:41 pm, "WELCOME ###" <(E-Mail Removed)> wrote:
>
> When you compile this code,it compiles well except if & else.If one operates
> the other not!!!
>
> Please, can any person Help??!!


That is how 'if' and 'else' work. Either the first block gets
executed,
or the second block gets executed. Not both. Not none.

 
Reply With Quote
 
Salt_Peter
Guest
Posts: n/a
 
      05-10-2007
On May 9, 10:41 pm, "WELCOME ###" <(E-Mail Removed)> wrote:
> Hello every one.
> The following is code built by C++ \ CLI . It's built well :


i don't think so.

> --------------------------------------------------
> #include "stdafx.h"
>
> #include <cstdlib>
>
> using namespace System;
>
> int main(array<System::String ^> ^args)
>
> {
>
> int magic;
>
> int guess;
>
> Console::Write(L"Enter Your Guess Number : ");
>
> guess=Console::Read();
>
> Random^ r = gcnew Random();
>
> r->Next();
>
> magic=7;
>
> if(guess=7)


Uh, the above statement says:
if you successfully *assign* the literal value 7 to the variable
guess, then process the accompanying block.
Its a *assignment*, not a comparison

Try:
if ( 7 == guess ) // ah, not an assignment
{
...
}

I placed the constant on the left side for a reason, at least that way
if you mistakenly write (7 = guess) the compiler will generate an
obvious error since the lhv is a constant.
Let that be a lesson to you.

>
> {
>
> Console::WriteLine(L"BRAVO");
>
> }
>
> else
>
> {
>
> Console::WriteLine(L" SORRY..");
>
> }
>
> return 0;
>
> --------------------------------------
>
> When you compile this code,it compiles well except if & else.If one operates
> the other not!!!


thats expected, see above.
Next time, can you please post C++ code?


 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      05-10-2007
"Old Wolf" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> On May 10, 2:41 pm, "WELCOME ###" <(E-Mail Removed)> wrote:
>>
>> When you compile this code,it compiles well except if & else.If one
>> operates
>> the other not!!!
>>
>> Please, can any person Help??!!

>
> That is how 'if' and 'else' work. Either the first block gets
> executed,
> or the second block gets executed. Not both. Not none.


Well, except in the OP's case the if will always be executed since he's
using = instead of ==


 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      05-10-2007
Jim Langston wrote:
> "Old Wolf" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
>> On May 10, 2:41 pm, "WELCOME ###" <(E-Mail Removed)> wrote:
>>> When you compile this code,it compiles well except if & else.If one
>>> operates
>>> the other not!!!
>>>
>>> Please, can any person Help??!!

>> That is how 'if' and 'else' work. Either the first block gets
>> executed,
>> or the second block gets executed. Not both. Not none.

>
> Well, except in the OP's case the if will always be executed since he's
> using = instead of ==
>

I doubt it, not if he tries to compile the code with a C++ compiler....

--
Ian Collins.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
magic / counter-magic? (detect loading file?) Giles Bowkett Ruby 9 12-17-2007 05:42 AM
[Newbie Question] Magic Number's dangerous? seungwoo.yu@gmail.com Java 60 12-07-2005 11:20 PM
OT: Number Nine, Number Nine, Number Nine FrisbeeŽ MCSE 37 09-26-2005 04:06 PM
bad magic number lawrence Java 5 10-10-2003 08:31 PM



Advertisments