Jump to content

(Extreme) Newbie help --> What is wrong with this code?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
12 replies to this topic

#1
ns_uni

ns_uni

    Newbie

  • Members
  • PipPip
  • 15 posts
As part of my prac I need to write a program for a police radar.

I am getting these error messages -
radarprog.c: In function 'main':
radarprog.c:23: error: parse error before 'else'
radarprog.c:30: error: parse error before 'else'
radarprog.c:37: error: parse error before 'else'
radarprog.c: At top level:
radarprog.c:42: error: parse error before 'return'

I am messing something up, so what is wrong with the "else if" statements? How am I not conforming to the syntax? Please bare with me if this seems silly, as it is only my second week (actually the 2nd starts tomorrow).. so please be kind!

Thanks in advance!

Here is the code -

/*****************************************************************
 Author: Nate
 Date: 17/02/09
 Purpose: Speed Radar and Fine calculation 
*****************************************************************/

#include <stdio.h>

int main()
{
	int speed;
	
	printf( "Please enter the speed: ");
	scanf( "%d", &speed );   

	/* State if speeding*/
	if(speed >59);
	{
	printf( "Speeding" );
	}
	
		/* State fine for 1-10 khm over" */
		else if(speed >59 && <=69);
		{
		printf("1-10kmh over the limit, fine is $80");
		}
	
	
			/* Stae fine for 11-30 khm over" */
			else if(speed >=70 && <=89);
			{
			printf("11-30kmh over the limit, fine is $150");
			}
		
		
				/* State fine for 30+ khm over" */
				else(speed >=90);
				{ 
				printf("30kmh or over the limit, fine is $500");
				}

	return(0);
}


#2
Phoenixz

Phoenixz

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 256 posts
There should be no ;'s on else and if statements, also on C there is no need for return 0; and int main, just main() will do.

This should fix your problems.

Edit: also, printf's also bind themselves to the nearest if/else statement so there is no need for { }'s unless you wish to use multiple statements.

Edit x2: don't forget \n's at the end of your printf to make a new line E.G. printf("Speeding\n");
Posted Image

#3
ns_uni

ns_uni

    Newbie

  • Members
  • PipPip
  • 15 posts
OK done.. now I am getting this error message --

radarprog.c: In function 'main':
radarprog.c:22: error: parse error before '<=' token

#4
Phoenixz

Phoenixz

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 256 posts
Repost new code, please?
Posted Image

#5
ns_uni

ns_uni

    Newbie

  • Members
  • PipPip
  • 15 posts
/*****************************************************************
 Author: Nate
 Date: 17/02/09
 Purpose: Speed Radar and Fine calculation
*****************************************************************/

#include <stdio.h>

main()
{
	int speed;
	
	printf( "Please enter the speed: ");
	scanf( "%d", &speed );   

	/* State if speeding*/
		if (speed >59)
		printf( "Speeding" );
		
	
		/* State fine for 1-10 khm over" */
		if(speed >59 && <=69)
			printf("1-10kmh over the limit, fine is $80");
		
	
	
			/* Stae fine for 11-30 khm over" */
			else if(speed >=70 && <=89)
				printf("11-30kmh over the limit, fine is $150");
			
		
		
				/* State fine for 30+ khm over" */
				else(speed >=90)
				printf("30kmh or over the limit, fine is $500");


}


#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
are you getting compiler errors, runtime errors, something else?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
ns_uni

ns_uni

    Newbie

  • Members
  • PipPip
  • 15 posts
it wont compile.

#8
Lance

Lance

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 276 posts

/*****************************************************************

 Author: Nate

 Date: 17/02/09

 Purpose: Speed Radar and Fine calculation

*****************************************************************/


#include <stdio.h>


main()

{

	int speed;

	

	printf( "Please enter the speed: ");

	scanf( "%d", &speed );   


	/* State if speeding*/

	if (speed >59)

		printf( "Speeding" );

		

	

	/* State fine for 1-10 khm over" */

	if(speed >59 && speed<=69)

		printf("1-10kmh over the limit, fine is $80");

		

	/* Stae fine for 11-30 khm over" */

	else if(speed >=70 && speed<=89)

		printf("11-30kmh over the limit, fine is $150");

			

		

	/* State fine for 30+ khm over" */

	else // (speed >=90)

		printf("30kmh or more over the limit, fine is $500");



}



#9
Lance

Lance

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 276 posts
A slightly smarter implementation:


/*****************************************************************

 Author: Nate

 Date: 17/02/09

 Purpose: Speed Radar and Fine calculation

*****************************************************************/


#include <stdio.h>


main()

{

	int speed; 

	

	printf( "Please enter the speed: ");

	scanf( "%d", &speed );   


       

	if (speed < 60 )

               return;


	/* State if speeding*/

	printf( "Speeding" );

		

	

        switch(speed/10)

        {

	case 5:	/* State fine for 1-10 khm over" */

		printf("1-10kmh over the limit, fine is $80");

	        break;	

	

	

	case 6:	/* Stae fine for 11-30 khm over" */

		printf("11-30kmh over the limit, fine is $150");

	        break;		

		

		

	default:	/* State fine for 30+ khm over" */

		printf("30kmh or over the limit, fine is $500");

                break;

        }


}


I don't know if you really intend it, but speed of 60 will be overspeed of 1 kmh.

#10
ns_uni

ns_uni

    Newbie

  • Members
  • PipPip
  • 15 posts
Thanks for your help, I thought the same thing. But I was going exactly as briefed on the prac document.

#11
ns_uni

ns_uni

    Newbie

  • Members
  • PipPip
  • 15 posts
OK I SORTED IT!!

And I added some constants.... as stated later in my Prac document.

/*************************************************************
Name: Nate
Date: 22/02/09
Purpose: Speed radar and fine calculation program 
**************************************************************/

#include <stdio.h>

main()

{

int speed;
/* This is the current fine in $ for speeding 1 - 10 km/h over the limit */
const int FINE_LEVEL1 = 80; 
/* This is the current fine in $ for speeding 11 - 30 km/h over the limit */
const int FINE_LEVEL2 = 150;
/* This is the current fine in $ for speeding 30 + km/h over the limit */
const int FINE_LEVEL3 = 500;

	/* This is to simulate the gun and the entry of data*/
	printf("Please select car, press trigger, then manually type speed: \n");
		scanf("%d", &speed);
		
	/*If they are speeding the computer will state this*/
	if (speed >60)
		printf("SPEEDING!\n");
			
	/*Calculates the fine for 1 - 10 km/h above the limit and states fine level/cost*/
	if ((speed >= 61) && (speed <= 70))
		printf ("Vehicles current speed is: %d km/h\nThe fine for 1 - 10 km/h above the limit is: $%d\n", speed, FINE_LEVEL1);
				
		/*Calculate the fine for 11 - 30 km/h above the limit and states fine level/cost*/
		else if ((speed >= 71) && (speed <= 90))
			printf ("Vehicles current speed is: %d km/h\nThe fine for 11 - 30 km/h above the limit is: $%d\n", speed, FINE_LEVEL2);
					
			/*Calculate the fine for 30 + above the limit and states fine level/cost*/
			else if (speed >= 91)
				printf ("Vehicles current speed is: %d km/h\nThe fine for 31 km/h and above the limit is: $%d\n", speed, FINE_LEVEL3);

return(0);

}



#12
Phoenixz

Phoenixz

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 256 posts
You do not need the return(0); you will need a getchar(); or when you press enter from the scanf it will end the program automatically.
Posted Image