2012-09-21 28 views
2

你好,我是新來的,在C編程。我不想問你可能認爲簡單的東西,但我已經要求我的同學,甚至我的編程老師看看他們能否找到錯誤,但直到今天,他們找不到(他們)。運行時檢查失敗#2 - 變量「d」周圍的堆棧已損壞。 (在Visual Studio下的C編程)

但首先讓我介紹我所知道的,它說:

「運行時檢查失敗#2 - 堆棧變量‘d’(有時M等 Y)已損壞」 。

我做我的工作,嘗試調試它,但問題始終顯示在最後的代碼行(主體),所以我不能準確地找到問題的所在,這裏附上的代碼,如果你發現問題並向我解釋爲什麼我會得到它(在未來不重複相同的錯誤)= D,我會非常高興。

#include <stdio.h> 
#include <stdlib.h> 
#include <math.h> 

/* Type Declaration */ 
#define true 1 
#define false 0 
typedef char boolean; 

/* Functions declaration */ 

boolean test(short int d, short int m, long int y); 
boolean nextday(short int d, short int m , long int y); 

int main(void) 
{ 
    /* Variables initialization */ 
    short int d = 0, m = 0; 
    long int y = 0; 

    do { 
     /* Data by user*/ 
     printf("Ingrese el numero de año: "); 
     scanf("%ld", &y); 
    } while (y < 0); 

    do { 
     printf("Ingrese el numero de mes: "); 
     scanf("%d", &m); 
    } while (m < 1 || m > 12); 

    do { 
     printf("Ingrese el numero de dia: "); 
     scanf("%d", &d); 
    } while (d < 01 || (test(d, m, y) == false)); // If the data is wrong then re-type the data 


    // If the nextday function return value is true then the next day is 01, if not just sum a day 
    if (nextday (d, m, y) == true) { 
     d = 01; 

     // If we start a new year the new month must be 01-01-01. 
     if (m == 12) { 
      m = 01; 
      y++; 
     } 

     // Just increase the month for any other month 
     else { 
      m++; 
     } 
    } 

    else { 
     d++; 
    } 

    printf("Mañana será: %d-%d-%ld\n", d, m, y); 

    return 0; 
} 

boolean test(short int d, short int m, long int y){ 
    int max; 

    switch(m) { 
     case 1:  
     case 3: 
     case 5: 
     case 7: 
     case 8: 
     case 10: 
     case 12: 
      max = 31; 
      break; 
     case 4: 
     case 6: 
     case 9: 
     case 11: 
      max = 30; 
      break; 
     case 2: 
      if (y % 400 == 0){ 
       max = 29; 
      } 
      else if (y % 100 == 0){ 
       max = 28; 
      } 
      else if (y % 4 == 0){ 
       max = 29; 
      } 
      else { 
       max = 28; 
      } 
      break; 
    } 
    if (d <= max){ 
     return true; 
    } 
    else { 
     return false; 
    } 
} 

boolean nextday(short int d, short int m, long int y) { 

    boolean x; 

    // If it's 28-02 in a secular year * 4 then it's a leap-year. (so it has 29 days) 
    if (m == 2 && d == 28 && y % 400 == 0) { 
     x = false; 
    } 

    // If it is an end of century but it isn't 4 multiply then it only has 28 days. 
    else if (m == 2 && d == 28 && y % 100 == 0) { 
     x = true; 
    } 

    // If it just a leap year it has 29 days. 
    else if (m == 2 && d == 28 && y % 4 == 0) { 
     x = false; 
    } 

    //If it's the last day of February and it's a leap year. 
    else if (m == 2 && d == 29 && y % 4 == 0){ 
     x = true; 
    } 

    // If we are in the end of the month. 
    else if ((d == 30 && (m == 4 || m == 6 || m == 9 || m == 11)) || 
       d == 31) { 
     x = true; 
    } 

    // Then if it is another day just sum a day 
    else { 
     x = false; 
    } 

    return x; 
} 
+2

可怕的是,你的老師沒有看到格式問題。 –

回答

5

您需要使用:

scanf("%hd", &m); 
scanf("%hd", &d); 

,因爲它們是短整型的。

使用「%d」,您基本上會在small int存儲空間中加載int大小變量。

一個int通常是4個字節,而small int是2個字節。

+0

哇真的很感謝熊貓! –

2
scanf("%d", &m); 

%d意味着 「這種說法指向一個int」。您正在提供一個指向short int的指針。可以使用%hd(對於short int),或者最好將m更改爲int(爲什麼在這裏打擾short int?)。

相關問題