C problem unusual error

Questions about programming languages and debugging
Post Reply
User avatar
DrVirus
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 383
Joined: 16 May 2007, 16:00
16
Contact:

C problem unusual error

Post by DrVirus »

Code: Select all

#include<stdio.h>
#include<conio.h>

float calculateCharges(float time);

int main()
{
clrscr();

	int loopCount=1;
	float hour1=0.0;
	float hour2=0.0;
	float hour3=0.0;
	float charge1=0.0;
	float charge2=0.0;
	float charge3=0.0;
	float totalH=0.0;
	float totalC=0.0;


	for(loopCount=1;loopCount<=3;loopCount++)
	{

	 switch (loopCount)
	 {
	  case 1 :
	  printf("Enter the hours of 1rst car : ");
	  scanf("%f",hour1);
	  charge1=calculateCharges(hour1);

	  case 2 :
	  printf("Enter the hours of the 2nd car : ");
	  scanf("%f",hour2);
	  charge2=calculateCharges(hour2);

	  case 3 :
	  printf("Enter the hours of the 3rd car : ");
	  scanf("%f",hour3);
	  charge3=calculateCharges(hour3);

	 }


	}
	totalH=hour1+hour2+hour3;
	totalC=charge1+charge2+charge3;

	printf("\nCar\t\tHours\t\tCharge");
	printf("\n1\t\t%.2f\t\t%.2f",hour1,charge1);
	printf("\n2\t\t%.2f\t\t%.2f",hour2,charge2);
	printf("\n3\t\t%.2f\t\t%.2f",hour3,charge3);
	printf("Total\t\t%.2f\t\t%.2f",totalH,totalC);


getch();
return 0;
}

float calculateCharges(float time)
{
	float extra=0.0;
	float charge=0.0;

if(time==24)
{
	return 10.00;
}

else
{
	if(time>=3)
	{
	extra=time-3;
	charge=2.00 + extra * 0.50;
	return charge;
	}

	else
	return 2.00;
}


}
THis is the code I got. It's not showing any errors while compiling. But once I run it, it's simply printing the first statement and then showing this : "scanf :floating point formats not linked. Abnormal program termination."

What's going on ?

User avatar
Lundis
Distorter of Reality
Distorter of Reality
Posts: 543
Joined: 22 Aug 2008, 16:00
15
Location: Deadlock of Awesome
Contact:

Post by Lundis »

I think scanf needs a pointer, not a variable.
try changing it to this:

Code: Select all

scanf("%f", &hour1); 
Anyway that's all the C I know, but I hope it helps...

Btw, that for and switch usage is rather unnecessary I think, unless you're planning on doing something else with it, you could write it like this:

Code: Select all

     printf("Enter the hours of 1rst car : ");
     scanf("%f",&hour1);
     charge1=calculateCharges(hour1);

     printf("Enter the hours of the 2nd car : ");
     scanf("%f",&hour2);
     charge2=calculateCharges(hour2);

     printf("Enter the hours of the 3rd car : ");
     scanf("%f",&hour3);
     charge3=calculateCharges(hour3); 
And maybe adding newlines would be a good idea :P

User avatar
DrVirus
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 383
Joined: 16 May 2007, 16:00
16
Contact:

Post by DrVirus »

Thanks. Jeez that was stupid of me. I never noticed the lack of "&". Thanks a ton . :D

Post Reply