Thread: Conditional Statements

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    Conditional Statements

    I have some code and the user inputs the values for floating point variables a and b.
    I want to write an if statement whereby; if 'a' and 'b' are zero, then some code will be executed. If not, then move on. I'm not sure how to do this. I have tried already but it doesn't work! The code only gets executed when a is zero and is only depenedent on a
    can anyone please help

    Code:
    if(a == 0 && b == 0)
    	{
    	printf("\wrong!\n");
    	thank_you();
    	exit(1);
    	}
    thanks

    Stuart

  2. #2
    Register User andor's Avatar
    Join Date
    Aug 2006
    Location
    Novi Sad
    Posts
    42

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    Lightbulb

    thanks andrew. I know understand why its not working but im not sure as to the change I'm suppose to make to my code. How would I write:

    Code:
    if(a == 0 && b == 0)
    Thanks

    Stuart

  4. #4
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    ((a==0) && (b==0))
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    Question

    happy reaper

    i tried your suggestion but unfortunately is doesnt work. It is still only reliant on 'a' being zero and appears to be independent of 'b'

    Help please

  6. #6
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    you'll have to show more code. I see that as being fine.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  7. #7
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Oh, actually, I just checked your post. You may want to be careful with floating-point arithmetic.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    Code:
    wrong(float a, float b)
    {
    	if((a == 0) && (b == 0))
    	{
    	printf("\nwrong\n");
    	thank_you();
    	exit(1);
    	}
    	return 0;
    }
    Is only dependent on value of 'a'. If a is zero, wrong will be displayed regardless of the value of b.

    thanks!
    Last edited by strokebow; 11-22-2006 at 09:18 AM.

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    Any ideas what I should do to overcome this hurdle?? It works when I change the varaiable and stuff to integers but I need them to be pointers. Can anyone please help!?!? Well and truely stuck!
    Last edited by strokebow; 11-22-2006 at 09:23 AM.

  10. #10
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    What are you sending to that function ?
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  11. #11
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    Code:
    main()
    {
    	float a, b;
    
    statements;
    /* a and b are initialised here by user input*/
    
    wrong(a, b);
    
    etc
    
    }
    
    
    
    wrong(float a, float b)
    {
    	if(a == 0 && b == 0)
    	{
    	printf("\wrong\n");
    	thank_you();
    	exit(1);
    	}
    	return 0;
    }

    cheers!

  12. #12
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    You don't ever initialize your variables ?

    EDIT : Did you ever try printing out what the user gives you ? Also, main returns an int.
    Last edited by Happy_Reaper; 11-22-2006 at 10:00 AM.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  13. #13
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    yep, i tried printing out my variables and they print out exactly what i type in. Its the conditional code thats the problem because I changed all variables to integers and then the programme worked. What do you mean by main is int?

    Thanks 4 ur help

  14. #14
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    And one last thing, you can avoid the floating-point stuff by doing :
    ((a < 0.00001 || a > -0.000001) && (b < 0.00001 || b > -0.000001))

    Or something of the sort. But I've never encountered this type of problem before.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  15. #15
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Main returns an int. It should be int main(void) or something of the sort.

    Check this out : link.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. problem with floats in conditional statements
    By Desolation in forum C Programming
    Replies: 3
    Last Post: 07-08-2006, 01:56 AM
  3. multi conditional if statements?
    By chaser in forum Game Programming
    Replies: 3
    Last Post: 07-26-2002, 10:52 PM
  4. Using floats in conditional statements
    By DBB in forum C++ Programming
    Replies: 3
    Last Post: 09-12-2001, 07:54 AM