# If statements

• 08-12-2009
DJ_Steve
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
• 08-12-2009
Salem
Your else if needs ( ) around the whole expression.

As in
else if ( /* your boolean expression here */ )
• 08-12-2009
Dino
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.
• 08-12-2009
DJ_Steve
ahh I get it, thanks! :D
• 08-12-2009
DJ_Steve
ok! done it! and it works! :D

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?
• 08-12-2009
bithub
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.
• 08-12-2009
bvkim
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; }```
• 08-13-2009
DJ_Steve
ok thanks again!

took me a while to understand even with the explanation but I think I get it now :D
• 08-13-2009
DJ_Steve
lol I just realised how obvious the code for negative was...I didn't even think about that
• 08-13-2009
DJ_Steve
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
• 08-13-2009
brewbuck
The speed limit is 59 km/h? That's a weird speed limit.
• 08-13-2009
tabstop
Quote:

Originally Posted by DJ_Steve
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.
• 08-13-2009
Elysia