Thread: Why does this keep happening?

  1. #1
    Registered User darknite135's Avatar
    Join Date
    Dec 2007
    Posts
    16

    Question Why does this keep happening?

    OK so I made a program where the user can pick what to do. I created a simple 'if' statement to handle what he types and what he gets.

    Here it is:
    Code:
    if ((answer = 'R') || (answer = 'r'))
    	{
    	path1();
    	}
    	
    	if ((answer = 'S') || (answer = 's'))
    	{
    	path2();
    	}
    But for some reason, it always goes to Path1..... I think its because it's said first. I don't know how to fix this. Even if i type in s, it goes to path1. Help!

  2. #2
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    A misstake you will continue to make over time, and it will frustrate you just as much every time because later you wont see it even though you know about it.

    a = 1 makes the value of a, 1
    a == 1 , checks to see if a is equal to 1

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Bump up the warning level of your compiler, to tell you when you might be making a mess of it.
    http://cboard.cprogramming.com/showp...80&postcount=3

    If you're using dev-c++ or code::blocks as your IDE (you didn't mention), then you can set the same flags as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    And learn how to indent. Stupid indenting will cause you to miss obvious issues.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Wraithan View Post
    A misstake you will continue to make over time, and it will frustrate you just as much every time because later you wont see it even though you know about it.

    a = 1 makes the value of a, 1
    a == 1 , checks to see if a is equal to 1
    Indeed. One little habit that can be helpful, if you intend a comparison with a constant value, to put the constant on the left hand side of the expression;
    Code:
    if( 1 == a) something();
    
    if ('R' == answer) something_else();
    This will trigger a compiler error should you do assignment (one = sign) rather than comparison.

    It doesn't stop problems if the code is comparing two variables (as either assignment will often be acceptable to the compiler). But being in the habit of asking yourself what goes on the left hand side will often have a side effect of making you think about what you're typing.....

  6. #6
    Registered User darknite135's Avatar
    Join Date
    Dec 2007
    Posts
    16
    Quote Originally Posted by Wraithan View Post
    A misstake you will continue to make over time, and it will frustrate you just as much every time because later you wont see it even though you know about it.

    a = 1 makes the value of a, 1
    a == 1 , checks to see if a is equal to 1
    Thank you, now I can finally work on my stupid Database thing lol. Thanks for everyone else for explaining the compiler issues.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What is happening here please explain
    By jayee_spicyguy in forum C Programming
    Replies: 5
    Last Post: 09-23-2008, 05:27 AM
  2. Explain me what is happening
    By capvirgo in forum C Programming
    Replies: 2
    Last Post: 02-18-2008, 08:26 AM
  3. Something funny happening with operator overloading
    By Argyle in forum C++ Programming
    Replies: 8
    Last Post: 04-14-2007, 04:59 PM
  4. Why is this happening?
    By caduardo21 in forum C++ Programming
    Replies: 5
    Last Post: 07-08-2005, 01:09 AM
  5. Weird things happening!!!!!!
    By korbitz in forum Windows Programming
    Replies: 4
    Last Post: 03-22-2004, 06:31 AM