Magic Number !!

Discussion in 'C++' started by WELCOME ###, May 10, 2007.

  1. WELCOME  ###

    WELCOME ### Guest

    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

    ===================================
     
    WELCOME ###, May 10, 2007
    #1
    1. Advertising

  2. WELCOME  ###

    Ian Collins Guest

    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.
     
    Ian Collins, May 10, 2007
    #2
    1. Advertising

  3. WELCOME  ###

    WELCOME ### Guest

    Oh
    It's C++, and my code problem is C++.
    What's wrong..Friend?!!
    You can"t fix it?!!!!
    ======================
    "Ian Collins" <> wrote in message
    news:...
    > 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.
     
    WELCOME ###, May 10, 2007
    #3
  4. WELCOME  ###

    Guest

    On May 9, 10:49 pm, "WELCOME ###" <> wrote:
    > Oh
    > It's C++, and my code problem is C++.
    > What's wrong..Friend?!!
    > You can"t fix it?!!!!
    > ======================"Ian Collins" <> wrote in message
    >
    > news:...
    >
    > > 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.
     
    , May 10, 2007
    #4
  5. WELCOME  ###

    Ian Collins Guest

    WELCOME ### wrote:

    Please don't top post.

    > "Ian Collins" <> wrote in message
    > news:...
    >> 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.
     
    Ian Collins, May 10, 2007
    #5
  6. WELCOME  ###

    David Harmon Guest

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

    Old Wolf Guest

    On May 10, 2:41 pm, "WELCOME ###" <> 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.
     
    Old Wolf, May 10, 2007
    #7
  8. WELCOME  ###

    Salt_Peter Guest

    On May 9, 10:41 pm, "WELCOME ###" <> 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?
     
    Salt_Peter, May 10, 2007
    #8
  9. WELCOME  ###

    Jim Langston Guest

    "Old Wolf" <> wrote in message
    news:...
    > On May 10, 2:41 pm, "WELCOME ###" <> 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 ==
     
    Jim Langston, May 10, 2007
    #9
  10. WELCOME  ###

    Ian Collins Guest

    Jim Langston wrote:
    > "Old Wolf" <> wrote in message
    > news:...
    >> On May 10, 2:41 pm, "WELCOME ###" <> 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.
     
    Ian Collins, May 10, 2007
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lawrence

    bad magic number

    lawrence, Sep 20, 2003, in forum: Java
    Replies:
    5
    Views:
    849
    Roedy Green
    Oct 10, 2003
  2. Replies:
    60
    Views:
    1,710
    Chris Uppal
    Dec 7, 2005
  3. Hendrik Maryns

    Magic number in Boolean

    Hendrik Maryns, Mar 10, 2006, in forum: Java
    Replies:
    15
    Views:
    1,130
    Hendrik Maryns
    Mar 13, 2006
  4. artie-effim

    MSVC .pch file magic number?

    artie-effim, Dec 1, 2004, in forum: C++
    Replies:
    1
    Views:
    468
    Victor Bazarov
    Dec 1, 2004
  5. Giles Bowkett
    Replies:
    9
    Views:
    428
    Giles Bowkett
    Dec 17, 2007
Loading...

Share This Page