簡體   English   中英

turbo c 3.0 DOS box中程序異常終止

[英]Abnormal program termination in turbo c 3.0 DOS box

執行此代碼時出現錯誤異常程序終止。

程序的目標是從文件list.txt讀取數字數組,並對該加載的數組執行遞歸二進制和遞歸線性搜索。

這是我的代碼:

#include <stdio.h>
#include <conio.h>

void menu();

int a[30000],n;

void main()
{
    FILE *fp;
    int i, ch;

    fp = fopen("list.txt", "r");
    if(fp == NULL)
    {
        printf("\nCant read\n");
        exit(0);
    }

    for(i = 0; i < n; i++)
        fscanf(fp, "%d", &a[i]);

    fclose(fp);

    for(i = 0; i < n; i++)
        printf(" %d ", a[i]);

    menu();

    scanf("%d", &ch);
    if(ch == 1)
    {
        printf("ch1\n");
    }
    else if(ch == 2)
    {
        printf("ch2\n");
    }
    else
    {
        exit(1);
    }
}//end main



void menu()
{
    printf("\nEnter the number of elements in array\n");
    scanf("%d", &n);
    printf("\n1.Linear Search\n2.Binary Search\n3.Exit\nEnter your choice\n");
}

我有我選擇 1 和 2 的邏輯。我需要知道我上面的代碼有什么問題。 請幫我解決這個問題

渦輪C! 這勾起了我的一些回憶,但我再也沒有我的副本了。 無論如何,在menu()初始化它之前,您正在使用n (正如評論者指出的那樣。)您還可以考慮使用malloc()而不是固定大小來分配數組。

Borland Turbo C 附帶了 Turbo Debugger,實際上我認為 Borland 免費贈送了一段時間,因此運行它可以讓您了解異常終止發生的位置以及可以用來檢查導致異常終止的堆棧跟蹤它。 另一個好的做法是調用assert()以確保您對變量中的內容的假設是正確的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM