我寫這顯示在問題下面的是,當我使用gets()函數,然後同時在代碼塊16.01執行代碼時獲取函數被調用,然後突然一個程序代碼塊停止工作。任何人都可以幫我解決這個問題。代碼塊已停止工作,同時使用gets函數
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <math.h>
#define PI 3.1415926535897932384626433832795
struct elements
{
float A, L, x1, y1, x2, y2;
double deg, c, s, E, esm[4][4];
int n1, n2;
} * ele;
int nonod, noele;
void elestiffmat(int);
void main()
{
int i;
char ch;
printf("\nEnter the number of nodes you want to create:");
scanf("%d", &nonod);
printf("\nEnter the number of elements you want to create:");
scanf("%d", &noele);
if (noele < (nonod - 1))
{
printf("\nThe problem is wrongly modeled.\nPress any key to abort.");
getch();
exit(0);
}
ele = (struct elements*)malloc(noele * sizeof(struct elements));
for (i = 0; i < noele; i++)
{
printf("\nCreating element %d", i + 1);
printf("\nCross sectional area?");
scanf("%f", &ele[i].A);
printf("\nYoung's Modulus?");
scanf("%Lf", &ele[i].E);
printf("\nStarting node number?");
scanf("%d", &ele[i].n1);
printf("\nIts coordinates:");
scanf("%f%f", &ele[i].x1, &ele[i].y1);
printf("\nEnding node number?");
scanf("%d", &ele[i].n2);
printf("\nIts coordinates:");
scanf("%f%f", &ele[i].x2, &ele[i].y2);
ele[i].L = sqrt(pow((ele[i].x2 - ele[i].x1), 2) + pow((ele[i].y2 - ele[i].y1), 2));
if ((ele[i].x2) - (ele[i].x1) == 0)
{
if (ele[i].y2 > ele[i].y1)
{
ele[i].deg = PI/2.0;
}
if (ele[i].y2 < ele[i].y1)
{
ele[i].deg = (3.0 * PI)/2.0;
}
}
if ((ele[i].y2) - (ele[i].y1) == 0)
{
if (ele[i].x2 > ele[i].x1)
{
ele[i].deg = 0.0;
}
if (ele[i].x2 < ele[i].x1)
{
ele[i].deg = PI;
}
}
if (((ele[i].y2) - (ele[i].y1))/((ele[i].x2) - (ele[i].x1)) < 0)
{
ele[i].deg = PI + atanf(((ele[i].y2) - (ele[i].y1))/((ele[i].x2) - (ele[i].x1)));
}
if (((ele[i].y2) - (ele[i].y1))/((ele[i].x2) - (ele[i].x1)) > 0)
{
ele[i].deg = atanf(((ele[i].y2) - (ele[i].y1))/((ele[i].x2) - (ele[i].x1)));
}
// printf("%g",ele[i].deg);
ele[i].c = cos(ele[i].deg);
ele[i].s = sin(ele[i].deg);
// printf("c=%g\ts=%g",ele[i].c,ele[i].s);
elestiffmat(i);
}
getch();
}
void elestiffmat(int i)
{
char choice;
ele[i].esm[0][0] = ele[i].c * ele[i].c * ele[i].A * ele[i].E/ele[i].L;
ele[i].esm[0][1] = ele[i].s * ele[i].c * ele[i].A * ele[i].E/ele[i].L;
ele[i].esm[0][2] = -ele[i].c * ele[i].c * ele[i].A * ele[i].E/ele[i].L;
ele[i].esm[0][3] = -ele[i].s * ele[i].c * ele[i].A * ele[i].E/ele[i].L;
ele[i].esm[1][0] = ele[i].s * ele[i].c * ele[i].A * ele[i].E/ele[i].L;
ele[i].esm[1][1] = ele[i].s * ele[i].s * ele[i].A * ele[i].E/ele[i].L;
ele[i].esm[1][2] = -ele[i].s * ele[i].c * ele[i].A * ele[i].E/ele[i].L;
ele[i].esm[1][3] = -ele[i].s * ele[i].s * ele[i].A * ele[i].E/ele[i].L;
ele[i].esm[2][0] = -ele[i].c * ele[i].c * ele[i].A * ele[i].E/ele[i].L;
ele[i].esm[2][1] = -ele[i].s * ele[i].c * ele[i].A * ele[i].E/ele[i].L;
ele[i].esm[2][2] = ele[i].c * ele[i].c * ele[i].A * ele[i].E/ele[i].L;
ele[i].esm[2][3] = ele[i].s * ele[i].c * ele[i].A * ele[i].E/ele[i].L;
ele[i].esm[3][0] = -ele[i].s * ele[i].c * ele[i].A * ele[i].E/ele[i].L;
ele[i].esm[3][1] = -ele[i].s * ele[i].s * ele[i].A * ele[i].E/ele[i].L;
ele[i].esm[3][2] = ele[i].s * ele[i].c * ele[i].A * ele[i].E/ele[i].L;
ele[i].esm[3][3] = ele[i].s * ele[i].s * ele[i].A * ele[i].E/ele[i].L;
printf("\nDo you want to print the element stiffness matrix (y/n)?");
gets(choice);
if (choice == 'y' || choice == 'Y')
{
printf("\nThe element stiffness matrix of the element %d is:-", i + 1);
int j, k;
for (j = 0; j < 4; j++)
{
printf("\n");
for (k = 0; k < 4; k++)
{
printf("%15g", ele[i].esm[j][k]);
}
}
}
}
嘗試使用char數組進行選擇。當寫入一個字符串時,你的char必須採取比它能容納的更多的值....這應該顯示在我猜的警告中。 –
在嘗試解決這個問題之前,*停止使用*'gets'。它是邪惡的,邪惡的,每次它被用在某個小貓死亡的地方。它被從標準庫中移除*,並且不再支持。改用['fgets'](http://en.cppreference.com/w/c/io/fgets)。其次,'gets'將'char *'作爲參數,你傳遞'char',所以在使用你不應該使用的函數之前,你不會使用它作爲記錄。我會鏈接文檔,但這可能會鼓勵您繼續使用它,最終,即使您這樣做,您也不希望這樣做。 – WhozCraig
首先,***永遠不會***使用'gets'。這是一個危險的功能,自C99標準以來已經過時,並在C11標準中完全刪除。改用['fgets'](http://en.cppreference.com/w/c/io/fgets)。其次,我懷疑你的IDE停止工作,但你的程序。要解決您的問題,首先使用調試器查找程序中發生崩潰的位置,然後檢查相關變量的值。如果您仍然無法弄清楚,請告訴我們該信息(位置和變量值)。 –