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");

Recommended Answers

All 8 Replies

Member Avatar for iamthwee

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

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

> 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???

>>WHY???

because you failed to correct this line as Salem suggested: scanf( "%d",year); Make that right then retest.

Thank you guy, my bad, I corrected it in the wrong place. Works now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.