Magic Number !!

W

WELCOME ###

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

===================================
 
W

WELCOME ###

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

Ian Collins

WELCOME ### wrote:

Please don't top post.
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.
 
D

David Harmon

On Thu, 10 May 2007 12:49:20 +1000 in comp.lang.c++, "WELCOME ###"
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.
 
O

Old Wolf

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.
 
S

Salt_Peter

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;

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

Jim Langston

Old Wolf said:
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

Ian Collins

Jim said:
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....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top