Thread: If statements

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    43

    If statements

    ok, I've just read through the IF statement tutorials and found that make the basic programs is pretty easy

    but, I'm currently stuck on a problem...

    "The Program should read an automobile speed (in km/h) and print the message “Speeding” if the speed exceeds 59kmh. The program should then also calculate the appropriate fine, $80 for 1-10kmh above the limit, $150 for 11-30kmh above the limit, $500 for 31kmh or more above the limit."

    I have no idea how to seperate the speeds into different categories...I tried doing this...
    insert
    Code:
    #include <stdio.h>
    
    
    int main()
    {
    	int speed;
    
    	printf( "Enter Speed:" );
    	scanf( "%d", &speed );
    	if ( speed < 59 ) {
    	  printf( "Not Speeding!\n");
    	}
    	else if ( speed >=59 ) && ( speed <=69 ){
    	  printf( "Speeding! Fine:$80\n" ) ;
    	}
    	else {
    	  printf( "Speeding!\n" );
    	}
    	return 0;
    }
    just to get me started I tried just doing an $80 fine between 60 and 70 but everytime I try to run it I get


    error: syntax error before '{' token|


    I think it must have something to do with AND ( speed <=69) because before I added it in, it was working

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Your else if needs ( ) around the whole expression.

    As in
    else if ( /* your boolean expression here */ )
    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.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    You can also reduce your logic here to this:
    Code:
    	else if ( speed <=69 ){
    	  printf( "Speeding! Fine:$80\n" ) ;
    	}
    since you've already test for < 59. Now that those are weeded out, you only have to test for < 69.
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    43
    ahh I get it, thanks!

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    43
    ok! done it! and it works!

    Code:
    #include <stdio.h>
    
    
    int main()
    {
    	int speed;
    
    	printf( "Enter Speed:" );
    	scanf( "%d", &speed );
    	if ( speed < 59 ) {
    	  printf( "Not Speeding!\n");
    	}
    	else if ( speed <=69 ){
    	  printf( "Speeding!\nFine:$80!\n" ) ;
    	}
    	else if ( speed <=89 ){
    	  printf( "Speeding\nFine:$150!\n" );
    	}
    	else
    	  printf( "Speeding!\nFine:$500\n" );
    	return 0;
    }
    Just as an extra question...does anyone know how to code so that if the user types in a letter or a negative number it comes up with an invalid error message?

    I assume it would be some like

    else ( speed == char) or something?

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Check the return value of scanf. If scanf() succeeds, it will return the number of conversions it successfully makes (in this case, 1). So if scanf() returns 0, that means the user did not enter in an integer. As for checking if the speed is a negative number... I think you can handle that one.
    bit∙hub [bit-huhb] n. A source and destination for information.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    60
    there you gooooo

    Code:
    #include <stdio.h>
    
    int main()
    {
    	int speed;
        int ret;
    	printf( "Enter Speed:" );
        ret = scanf( "%d", &speed );
        if( ret != 1 ) {
          printf("(error) input wrong type\n");        
        }
        else if ( speed < 0 ) {
          printf("Should not be negative!\n");
        }
    	else if ( speed < 60 ) {
    	  printf( "Not Speeding!\n");
    	}
    	else if ( speed < 70 ){
    	  printf( "Speeding!\nFine:$80!\n" ) ;
    	}
    	else if ( speed < 90 ){
    	  printf( "Speeding\nFine:$150!\n" );
    	}
    	else
    	  printf( "Speeding!\nFine:$500\n" );
    
    	return 0;
    }

  8. #8
    Registered User
    Join Date
    Aug 2009
    Posts
    43
    ok thanks again!

    took me a while to understand even with the explanation but I think I get it now

  9. #9
    Registered User
    Join Date
    Aug 2009
    Posts
    43
    lol I just realised how obvious the code for negative was...I didn't even think about that

  10. #10
    Registered User
    Join Date
    Aug 2009
    Posts
    43
    hmm...don't want to start a whole new topic

    I've finished the program and it works in Code Blocks and then I Compile the Current File and save it to desktop

    but when I run the program it says "Enter Speed" so I enter a number and instead of running the program, the window just closes :/

    I've added the getchar() bit to the code but I don't think that is the problem because the answer doesn't even flash for a second before closing

  11. #11
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    The speed limit is 59 km/h? That's a weird speed limit.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by DJ_Steve View Post
    hmm...don't want to start a whole new topic

    I've finished the program and it works in Code Blocks and then I Compile the Current File and save it to desktop

    but when I run the program it says "Enter Speed" so I enter a number and instead of running the program, the window just closes :/

    I've added the getchar() bit to the code but I don't think that is the problem because the answer doesn't even flash for a second before closing
    Console programs should be run from the console. If you didn't type the name of the program in to start it, you didn't do it right. [/i'm an old man now get off my lawn]

    That's the way Windows works -- once a program is finished, it's window closes.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. newbie question - if statements without conditions
    By c_h in forum C++ Programming
    Replies: 2
    Last Post: 07-18-2008, 10:42 AM
  3. C++ If Statements Help
    By moporho in forum C++ Programming
    Replies: 19
    Last Post: 01-18-2008, 08:40 AM
  4. Efficiency of case statements
    By Yasir_Malik in forum C Programming
    Replies: 26
    Last Post: 05-23-2006, 11:36 AM
  5. Statements and Functions
    By GeekFreak in forum C++ Programming
    Replies: 5
    Last Post: 08-15-2002, 12:34 PM