Thread: Old code

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    18

    Old code

    Strangely enough this doesn't work. Any ideas why?
    [code]
    #include <iostream>

    using namespace std;

    int main()

    {
    char rob;
    cout<<"yes or no?\n";
    cin>>rob;
    char y;
    char n;
    y==1;
    n==0;
    {
    if (rob=y)

    cout<<"thats correct";
    }
    return 0;
    }
    /[code]

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    
    {
    char rob;
    cout<<"yes or no?\n";
    cin>>rob;
    char y;
    char n;
    y==1;
    n==0;
    {
    if (rob=y)
    
    cout<<"thats correct";
    }
    return 0;
    }
    Terrible i tell you!
    try this:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    
    {
    char rob;
    cout<<"yes or no?\n";
    cin>>rob;
    char y;
    char n;
    y=1;
    n=0;
    if (rob==y)
        cout<<"thats correct";
    return 0;
    }

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Not very strange at all, really...

    >y==1;
    >n==0;
    >if (rob=y)

    Why not read this
    http://www.cprogramming.com/cboard/s...threadid=31176
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Someone needs to learn the difference between assignment operators and comparison operators.

    1) To compare two items you use ==
    2) To assign a value to an item use =
    Code:
    x = 10;  // assign 10 to x
    
    if( x == 10 ) // compare x with the 10
    Naturally I'll skip overloading of operators and other compairson functions, because you don't even understand this first item.

    On a side note, the second code example, while it will compile, isn't much better. It's ugly code also. One of the main reasons I don't like C++ as much as C is because it promotes such ugly code. (Randomly scattering variable declarations throughout code. Ug.)

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    Of course that all depends on the programmer, even with
    basic i put the variables on the top, because it's so much
    eayer managing, can't imaging creating variables in the
    middle of a program, good tutorials dont promote this kind
    of programming

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    160
    But when you call a function in the middle of main and in that function create a int or something that is actually the same as creating an int in the middle of main
    Well english isn't my first language, (it's instead a useless language called danish which only 5 milion people speak!!) so if you think my grammar SUCKS (it does by the way) than you're more then welcome to correct me.
    Hell I might even learn something

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Somewhere in Mr. Stroustrup's book it's suggested that declaring of variables should be done as near as practicable to where the variable is used in the code.

    Whether it lends itself to readability, or not, (I personally feel it does not) the rationale, as I recall, is that there is wasted overhead involved in declaring/defining variables which may not be used during program execution. Not too hard to envision an application, such as a game, where execution may never involve one, or more, sections of code.

    Now all we need is a rock-solid "Rule of Thumb" to guide us in determining when the trade-off in potentially wasted overhead is acceptable where readability and maintainability are concerned.

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  8. #8
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Originally posted by quzah
    Someone needs to learn the difference between assignment operators and comparison operators.

    1) To compare two items you use ==
    2) To assign a value to an item use =
    Code:
    x = 10;  // assign 10 to x
    
    if( x == 10 ) // compare x with the 10
    Naturally I'll skip overloading of operators and other compairson functions, because you don't even understand this first item.

    On a side note, the second code example, while it will compile, isn't much better. It's ugly code also. One of the main reasons I don't like C++ as much as C is because it promotes such ugly code. (Randomly scattering variable declarations throughout code. Ug.)

    Quzah.
    In addition to what Quazh said, the == operator returns true or false only, that's why they are used in if() statements.
    none...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM