這是我的第一篇文章,我不得不承認,編程時我很糟糕。我是班上的那個人,儘管他的尾巴已經消失,但似乎永遠無法掌握編程以及其他同學。所以請保持好心情,我會盡力在下面解釋我的問題。Scanf_s警告?跳過用戶輸入(主題:龍格庫塔,流行模擬)
我有下面的代碼(評論刪除),但是當我運行它時,我收到類似於下面列出的警告。另外,當我運行程序時,第一個用戶輸入的值是允許的,但是突然之間,它跳轉到程序結束,不允許我輸入其他變量的值(例如變量「beta」 )。我有一個輸出圖像(http://i.stack.imgur.com/yc3jq.jpg),你可以看到我輸入了alpha,但是程序運行到最後。有什麼想法嗎?
非常感謝您的幫助! -Spencer
----------------------------- CODE ------------- ---
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
float alpha, beta, h;
float slope_k (float, float, float, float);
float slope_q (float, float, float, float);
float slope_p (float, float, float, float);
int main (void)
{
float t0=0, tf, h, S0, I0, R0, k1, k2, k3, k4, q1, q2, q3, q4, p1, p2, p3, p4;
int N;
char sim_file[1000];
FILE *out_file;
float *time_out, *s_out, *i_out, *r_out;
printf("Enter the value of the rate constant for infection (alpha) \n");
scanf("&f", &alpha);
printf("Enter the value of the rate constant for recovery or death (beta) \n");
scanf("&f", &beta);
printf("Enter the value of number of persons susceptible to the given contagion [S] at the initial time zero [i.e. S(t)=S(0) = ? ] \n");
scanf("&f", &S0);
printf("Enter the value of the number of persons infected [I] at the intial time zero [i.e. I(t) = I(0) = ?] \n");
scanf("&f", &I0);
printf("Enter the value of the number of persons that have already been infected but have recovered [or died] [R] at the initial time zero [i.e. R(t) = R(0) = ?] \n");
scanf("&f", &R0);
printf("Enter the final time for solution \n");
scanf("&f", &tf);
printf("Enter the solution step size (H) \n");
scanf("&f", &h);
N = (int)(tf/h);
printf("Enter file solution to store solution to simulation \n");
scanf("&s", sim_file);
out_file = fopen(sim_file, "w");
time_out = (float *)calloc(sizeof(float), N);
s_out = (float *)calloc(sizeof(float), N);
i_out = (float *)calloc(sizeof(float), N);
r_out = (float *)calloc(sizeof(float), N);
time_out[0]= 0;
s_out[0] = S0;
i_out[0] = I0;
r_out[0] = R0;
for(int i = 0; i < N; ++i);
{
int i = 0;
time_out[i+1] = (i+1)*h;
k1 = h*slope_k(time_out[i], s_out[i], i_out[i], r_out[i]);
q1 = h*slope_q(time_out[i], s_out[i], i_out[i], r_out[i]);
p1 = h*slope_p(time_out[i], s_out[i], i_out[i], r_out[i]);
k2 = h*slope_k(time_out[i]+(h/2), s_out[i]+(k1/2), i_out[i]+(q1/2), r_out[i]+(p1/2));
q2 = h*slope_q(time_out[i]+(h/2), s_out[i]+(k1/2), i_out[i]+(q1/2), r_out[i]+(p1/2));
p2 = h*slope_p(time_out[i]+(h/2), s_out[i]+(k1/2), i_out[i]+(q1/2), r_out[i]+(p1/2));
k3 = h*slope_k(time_out[i]+(h/2), s_out[i]+(k2/2), i_out[i]+(q2/2), r_out[i]+(p2/2));
q3 = h*slope_q(time_out[i]+(h/2), s_out[i]+(k2/2), i_out[i]+(q2/2), r_out[i]+(p2/2));
p3 = h*slope_p(time_out[i]+(h/2), s_out[i]+(k2/2), i_out[i]+(q2/2), r_out[i]+(p2/2));
k4 = h*slope_k((time_out[i] + h), (s_out[i]+k3), (i_out[i]+q3), (r_out[i]+p3));
q4 = h*slope_q((time_out[i] + h), (s_out[i]+k3), (i_out[i]+q3), (r_out[i]+p3));
p4 = h*slope_p((time_out[i] + h), (s_out[i]+k3), (i_out[i]+q3), (r_out[i]+p3));
s_out[i+1] = s_out[i] + (1.0/6)*(k1 + (2*k2) + (2*k3) + k4);
i_out[i+1] = i_out[i] + (1.0/6)*(q1 + (2*q2) + (2*q3) + q4);
r_out[i+1] = r_out[i] + (1.0/6)*(p1 + (2*p2) + (2*p3) + p4);
}
return 0;
}
float slope_k(float t, float s, float i, float r)
{
float slope_k_out;
slope_k_out = -alpha*s*i;
return slope_k_out;
}
float slope_q(float t, float s, float i, float r)
{
float slope_q_out;
slope_q_out = (alpha*s*i)-(beta*i);
return slope_q_out;
}
float slope_p(float t, float s, float i, float r)
{
float slope_p_out;
slope_p_out = beta*i;
return slope_p_out;
}
例警告:
warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
謝謝你!我意識到它與scanf_s無關。我爲「%f」放了一個&而不是%!哎呀! – user1309448 2012-04-03 04:16:54