My F Statement is not working...

Discussion in 'Mac Programming' started by cybrscot, Jan 21, 2011.

  1. cybrscot macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #1
    I'm not having any luck with my IF statement. I've tried changing around the variables, etc, but I'm not sure why it won't work. It does compile, but the output is always 0. I don't know why. I'm using a %d placeholder for the wind = variable. I think that C treats characters as int, so I should be able to use the %d placeholder for text, right? Any ideas? I have already written a similar program, that worked, but I don't see the difference. So I'm at a loss on this one.

    Code:
    #include <stdio.h>
    
    main ()
    {
    
    	int velocity, wind ;
    	int calm, light, breezy, gale, storm, hurricane;
    	
    	printf ("Enter wind velocity (knots): ") ;
    	scanf ("%d", &velocity ) ;
    	
    	if (velocity < 1 )
    		wind = calm ;
    	else if (velocity < 4 )
    		wind = light ;
    	else if (wind < 28 )
    		wind = breezy ;
    	else if (velocity < 48 )
    		wind = gale ;
    	else if (velocity < 64 )
    		wind = storm ;
    	else if (velocity > 63 )
    		wind = hurricane ;
    		
    	printf ("Conditions are... %d\n", wind) ;
    	
    	return ; 
    	
    }	

    Code:
    Light airScott-Deans-MacBook-Pro:~ scottdean$ gcc ~/documents/wind.c
    Scott-Deans-MacBook-Pro:~ scottdean$ ./a.out
    Enter wind velocity (knots): 2
    Conditions are... 0
    
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    What is calm set to? What is light set to? What are breezy, gale, storm, and hurricane set to? I don't see any assignment statement setting their value. They could be anything at all. What are you expecting them to be set to? If you DID print a character value (you have no char variables or constants) as an integer value, what would you expect to see?

    -Lee
     
  3. mydogisbox macrumors member

    Joined:
    Jan 16, 2011
    #3
    make an enum

    It looks to me like you either intended to create an enum (but never did), or you intended to initialize your variables (but never did).
     
  4. cybrscot thread starter macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #4
    hmmm, I guess I thought that each "wind = statement" was connected to it's above "if expresson", so that when it tested the velocity against the expression, it would print the wind = (calm, gale, hurricane, whatever is associated with that if). So if it tested velocity at 0, it's true that it's less than 1, so I thought it might print the wind = calm, which is why in my final printf I use (%d, wind) and was thinking that it would print the word that = wind for the tested true statement.

    Damn, what an terrible piece of code. Okay, back to the drawing board
     
  5. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #5
    That's a big step in becoming a programmer! (EDIT: http://xkcd.com/844/)

    You made calm, light, breezy, gale, storm, hurricane separate int variables that are just like velocity, wind and you didn't initialize them to a value.

    As mydogisbox said, look at enums, and if your goal is to print

    Code:
    Conditions are... hurricane
    then wind needs to be a string (char *) and use %s in your printf format string. As it stands you could only get

    Code:
    Conditions are... 1
    EDIT:Also this is a good place to learn about switch/case/break.

    B
     
  6. cybrscot thread starter macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #6
    Actually, I have another thread with that question. I DID try to do it in a switch/case/break, but I'm running into the problem that the case is supposed to be constant, and I need the cases to be a range of numbers that the user may input. How to do? I'll go check that other thread, haven't yet.

    Thanks
    Scott
     
  7. cybrscot thread starter macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #7
    Also, as far as the char* and %s, the book hasn't covered those yet, so I think I'm supposed to write this without those. The question in the book did ask that after the user input the wind velocity in knots, the program return the value, calm, gale, etc. So the author isn't looking for 0, or 1. I need to figure out how to get the corresponding word to print.

    Question from Book:
    Write a program that asks the user to enter a wind velocity (in knots), then displays the corresponding description.

    Velocity Description
    Less than 1 Calm
    1-3 Light Air
    4-27 Breeze
    etc
    etc
    etc.
     
  8. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #8
    With the tools you have at hand you can put the printf in the if statements.

    Code:
    #include <stdio.h>
    
    main ()
    {
    
    	int velocity;
    	
    	printf ("Enter wind velocity (knots): ") ;
    	scanf ("%d", &velocity ) ;
    	
    	if (velocity < 1 )
    		printf ("Conditions are calm\n") ;
    	else if (velocity < 4 )
    		printf ("Conditions are light\n") ;
    	else if (wind < 28 )
    		printf ("Conditions are breezy\n") ;
    	else if (velocity < 48 )
    		printf ("Conditions are gale\n") ;
    	else if (velocity < 64 )
    		printf ("Conditions are storm\n") ;
    	else if (velocity > 63 )
    		printf ("Conditions are hurricane\n") ;
    			
    	return ; 
    	
    }
    You could use an expression to take your ranges to an integer that could be used in a switch/case with an enum, but it sounds like this is where the author wants you.

    B
     
  9. cybrscot, Jan 21, 2011
    Last edited: Jan 21, 2011

    cybrscot thread starter macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #9
    Wow, you are right on the money! I'm very disappointed though that I was overcomplicating the code. What you did was really pretty simple. I was going about it entirely the wrong way. Damn, up till now I've been pretty good about writing the correct "kind" of code, albeit some mistakes in the code. But on this one what I thought was appropriate was way off the mark. Thanks for the code and the time you took to help me. I appreciate it very much. Below was my most recent "attempt" at fixing my previous code, it still didn't work of course. LOL! :D Look at it, it's ridiculously complex compared to yours!!!

    Code:
    #include <stdio.h>
    
    main ()
    {
    
    	int velocity ;
    	char calm, light, breezy, gale, storm, hurricane ;
    	
    	calm       < 1 ;
    	light      < 4 ;
    	breezy     < 28 ;
    	gale       < 48 ;
    	storm      < 64 ;
    	hurricane  > 63 ;
    	
    	printf ("Enter wind velocity (knots): ") ;
    	scanf ("%d", &velocity ) ;
    	
    	if (velocity < 1 )
    		velocity = calm ;
    	else if (velocity < 4 )
    		velocity = light ;
    	else if (velocity < 28 )
    		velocity = breezy ;
    	else if (velocity < 48 )
    		velocity = gale ;
    	else if (velocity < 64 )
    		velocity = storm ;
    	else if (velocity > 63 )
    		velocity = hurricane ;
    		
    	printf ("Conditions are... %s\n", velocity) ;    /* I see mistake here I have %s, velocity is an int, I changed it, but still get output 0 */
    	
    	return ; 
    	
    }	
     
  10. notjustjay macrumors 603

    notjustjay

    Joined:
    Sep 19, 2003
    Location:
    Canada, eh?
    #10
    The other folks have given you some good explanations already. I think it's important to step back and look carefully at what you're trying to do -- sometime it's useful to just go over the basics again.

    These statements:

    Code:
    int velocity, wind ;
    int calm, light, breezy, gale, storm, hurricane;
    These define a bunch of variables, each of type "int". The names of the variables are velocity, wind, calm, light, etc. However, variable names are only used by YOU as a programmer. They could just as easily have been named x or VARIABLE_Y or intVar02312 or aTypeOfWindThatFeelsCalm or whatever. It will make no difference to how the code works, except that using a name like "velocity" is more readable to you than a name like "x".

    As others have pointed out, when you created a new variable called, say, "calm", the compiler presumably automatically assigned it a value of 0. By the way, you can't rely on a compiler to do this -- some won't -- so it's best practice to always explicitly set a default value or else make sure the variable gets set to some value. Otherwise, you might get unexpected results.

    Strings, like the actual word "calm", are a whole other kettle of fish in C, as a string in C is actually an array of characters, and you'll need to understand some basics about arrays and "pointers" in order to use them.

    Hmm... uh, without meaning to offend, Scott, that code suggests to me that you're still not quite understanding how variables work. I think it would be good for you to step back and practice playing with variables. Maybe write a program that just declares a few variables, does something with them (maybe x = y + z or something), prints out some values, stuff like that...
     
  11. cybrscot thread starter macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #11
    Thanks notjustjay. You're right. When I initialize a variable, must it be with an = symbol?

    like
    x = y + z

    or can it be with relational operators too?

    x < y + z
    or
    x =<y + z (read, x is less than y + z)

    In my terrible example code that you looked at, I did
    calm < 1 ;
    light < 4 ;
    etc
    etc
     
  12. cybrscot, Jan 21, 2011
    Last edited: Jan 21, 2011

    cybrscot thread starter macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
  13. ulbador, Jan 21, 2011
    Last edited: Jan 21, 2011

    ulbador macrumors 68000

    ulbador

    Joined:
    Feb 11, 2010
    #13
    I think you are greatly confusing things... At some point, you must do something like:

    calm = 1;

    When you do:

    if (calm < 4) { do stuff }

    You can do:

    calm = x < y;

    Which would put a 0 or 1 in there depending if it is true or false.

    Calm has to be set to something so you can compare it. I think you greatly confuse things when you start mixing your assignments and comparisons. You can do this (and there are some cases to actually do this), but I think you won't get the results you are expecting.
     
  14. cybrscot thread starter macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #14
    So the compiler can't figure out that 0, 1, 2, 3 are < 4?
    If I set calm = 1: then the user must input 1 to return the value "calm"
    So I would have to do this????
    calm = 1
    calm = 2
    calm = 3 {would this have worked? Although way too many numbers and variables to initialize, I'm just curious if it would have worked.
     
  15. notjustjay, Jan 21, 2011
    Last edited: Jan 21, 2011

    notjustjay macrumors 603

    notjustjay

    Joined:
    Sep 19, 2003
    Location:
    Canada, eh?
    #15
    You have to use an = sign, yes. "Set x to be equal to something".

    Relational operators are only used for comparing things, for example inside an if statement.

    You can't say

    x < y + z

    But you can say

    if (x < y + z)
    { // then do something
    }

    Or you can even say

    q = x < y + z;

    Which says "determine whether 'x < y + z' is TRUE or FALSE, and assign that answer to q". q will end up with a value of 0 if that expression "x < y + z" is FALSE, and non-zero if it is TRUE.

    Basically an expression is "MAKE this happen" and a condition is "DETERMINE whether this is true or false".

    Edit: One thing to keep in mind with an expression like "x < y + z" is that it is confusing. Which part is the computer going to calculate first? Will it figure out the sum of y + z, and then see if x is less than the total? Or will it look at "x < y", determine if that part is true (and give it a value of 0 if it is false), then add z to the result? I'm not sure. To be explicitly clear, you should write expressions like that using brackets, e.g.

    q = x < (y + z);

    or

    q = (x < y) + z;
     
  16. notjustjay macrumors 603

    notjustjay

    Joined:
    Sep 19, 2003
    Location:
    Canada, eh?
    #16
    Careful there. You just wrote the value "calm" in quotes, as if you expect the actual word "calm" (letters C, A, L, M) to appear on the screen. As I said, it won't. calm is just a variable name. The word "calm" has nothing to do with any of this. If you want to see the word "calm", that's a couple of steps ahead of where we are right now.
     
  17. cybrscot thread starter macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #17

    Thank you very much!! Trust me, I am getting better at this stuff! These are all questions I would ask a teacher (if I had one) for clarification, but I don't have one, only the friendly great folks here on the forum!

    Time for me to go to bed, so I've got to stop thinking about this code stuff, once my mind gets going on it, it can keep me up until 4am, (actually does every night, but I've got to be at work early tomorrow!) Thanks everybody!
     
  18. ulbador macrumors 68000

    ulbador

    Joined:
    Feb 11, 2010
    #18
    I think where you are having your disconnect is with the actual variable name:

    int calm = 0;

    With that variable, the name of it is "calm". But that is just the name you use to reference it in your code.

    That is not the name that gets printed to the screen when you try to print it out. When you try to print it out, you are printing out whatever the VALUE of the variable is. When you do:

    printf("The variable is: %s", calm);

    This tells the program to print the VALUE of calm as a "string". This doesn't mean that it is going to print "calm", this means it is going to try to convert whatever is stored IN the variable to a string and print it out.
     
  19. notjustjay, Jan 21, 2011
    Last edited: Jan 21, 2011

    notjustjay macrumors 603

    notjustjay

    Joined:
    Sep 19, 2003
    Location:
    Canada, eh?
    #19
    You're welcome. Good luck :)

    Here's a quick little program for you to study and play with, that will hopefully help you understand what's going on.
    Code:
    #include <stdio.h>
    
    main ()
    {
    	int myNumber = 71;  // Ssh, don't tell anyone!
    	int yourGuess = 0;
    	
    	printf ("Guess what number I am thinking of: ") ;
    	scanf ("%d", &yourGuess ) ;
    	
    	if (yourGuess < myNumber )
    		printf("Sorry, %d is too low!\n", yourGuess);
    	else if (yourGuess > myNumber )
    		printf("Sorry, %d is too high!\n", yourGuess);
    	else if (yourGuess == myNumber )
    		printf("Yes, %d is correct!\n", yourGuess);
    	return; 
    }
    
    You only get one shot at the guess (there is no "go back and guess again" loop). Also, that last "else if" isn't really necessary because if your guess is NOT too low AND your guess is NOT too high then it MUST be equal, so I could have just used an "else" instead of "else if".
     
  20. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #20
    I think your instinct is right, you just don't have the tools to do what your instinct tells you yet.

    Here's some sample code that might help you get your switch/case working.

    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv) {
    
    	int velocity;
    	
    	printf ("Enter wind velocity (knots): ") ;
    	scanf ("%d", &velocity ) ;
    
    	int condition;
    
            condition = (velocity > 1) + (velocity > 4) + (velocity > 28) + (velocity > 48) + (velocity > 64);
            printf ("%d\n", condition);
    
    }
    condition will have a value between 0 and 5 corresponding to {calm, light, breezy, gale, storm, hurricane}.

    B
     
  21. ulbador macrumors 68000

    ulbador

    Joined:
    Feb 11, 2010
    #21
    I love utterly simplistic code like this. So perfect and elegant.
     
  22. notjustjay macrumors 603

    notjustjay

    Joined:
    Sep 19, 2003
    Location:
    Canada, eh?
    #22
    I believe this is only true for C++, where "true" evaluates to 1 when converted to an int. I've read that with straight-up C, the only guarantee is that false is 0 and true is some non-zero value, possibly 1, possibly even -1.
     
  23. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #23
    http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf

    B
     
  24. notjustjay macrumors 603

    notjustjay

    Joined:
    Sep 19, 2003
    Location:
    Canada, eh?
    #24
    Cool. I stand corrected. I did some forum searches before I posted that, but couldn't find a definitive answer.
     
  25. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #25
    I was reading the spec a bit after one of cybrscot's other threads and tested it before I posted. :p

    B
     

Share This Page