In the below code im getting a year from the user. but when I print of the year from scanf is doesnt print the correct value such as 2010 will be printed as 74.43434 (somthing like this).
If i change the scanf from %.3lf to %f or %d the program will crash.

ANY IDEAS ANYONE?

double year;

if (r <=-.9){
	
printf("Data are linearly correlated: r = %1.3f\n",r);
printf("What year do you want to predict? ");
scanf( "%.3lf",year);
	
predict = meany + b*year; 

printf("Predicted index in year %.3lf is %lf", year, predict);
	
	}
	else
		printf("Data are not linearly correlated: r = %lf\n",r);
		printf("Stopping");

Shouldn't the year be an integer. Therefore shouldn't you be using %d?

Yes I tried making year an int and used %d, but then the program crashed...

Any other ideas why?

>>Any other ideas why?

Most likely crased because you didn't change "%.3lf" to "%d" everyplace. If you did that then you have to post the code again

[IMG]C:\Documents and Settings\Hamid\Desktop\int.gif[/IMG]

int year;

if (r <=-.9){
	
	printf("Data are linearly correlated: r = %1.3f\n",r);
  printf("What year do you want to predict? ");
	scanf( "%d",year);
	
		predict = meany + b*year; 
	
	printf("Predicted index in year %d", year);
	
	}

Its just not normall that this happens.

scanf( "%d",year);

You need &,
as in

scanf( "%d", [COLOR="Red"]&[/COLOR]year);[/ICODE]

You should also start using a compiler which will warn you about printf / scanf format string errors before you get to run your code and have it fail in a variety of interesting ways.

gcc -W -Wall -ansi -pedantic -O2 prog.c

is usually a revelation the first time you try it

Edited 3 Years Ago by Reverend Jim: Fixed formatting

> scanf( "%d",year);
You need &,
as in scanf( "%d", &year); You should also start using a compiler which will warn you about printf / scanf format string errors before you get to run your code and have it fail in a variety of interesting ways. gcc -W -Wall -ansi -pedantic -O2 prog.c is usually a revelation the first time you try it

This is my code.

if (r <=-.9)
	{
	
		printf("Data are linearly correlated: r = %1.3f\n",r);
  		printf("What year do you want to predict? ");
		scanf( "%d",year);
		predict = a + b*year; 
		printf("Predicted index in year %d is %lf", &year, predict);
	
	}
	else if (r >-.9)
	{
		printf("Data are not linearly correlated: r = %1.3lf\n",r);
		printf("Stopping");
		
	}

This is the output that i get

Data are linearly correlated: r = -0.985
What year do you want to predict? 2010
predicted index in year 2293448 is -6710792614

The problem is when i re print 2010 the output it 2293448.

WHY???

This question has already been answered. Start a new discussion instead.