c - 当我执行程序时,它关闭

标签 c data-structures

当我执行程序时,它会关闭。编译完成后,程序窗口打开,然后我可以输入客户端的ID,之后,它卡住,然后一个新的弹出窗口说程序已停止工作,需要关闭。我已将问题隔离到这一行,first = middle + 1;任何帮助将非常感激。该程序构建为在 Dev C++ 上以 C 语言运行。

#include <stdio.h>
#include <string.h>
typedef struct//Declares structure to hold seven created datatypes.
{
    int client_id;
    char client_business_name [30];
    char client_first_name [20];
    char client_last_name [20];
    char client_address [40];
    float client_budget;
    char client_business_info [300];
}Client;

main()
{
    Client c[100];
    int y;
    printf ("\nEnter Client ID:");
    scanf ("%d",&c[y].client_id);
    printf ("Enter Buisness Name:");
    scanf (" %[^\n]",c[y].client_business_name);
    printf ("Enter Client First Name:");
    scanf (" %[^\n]",c[y].client_first_name);     
    printf ("Enter Client Last Name:");
    scanf (" %[^\n]",c[y].client_last_name);     
    printf ("Enter Buisness Address:");
    scanf (" %[^\n]",c[y].client_address);
    printf ("Enter Client Budget:");
    scanf ("%f",&c[y].client_budget);
    printf ("Enter Buisness Information:");
    scanf (" %[^\n]",c[y].client_business_info);

    printf ("ID:%d\n",c[y].client_id);

    int key,max=100;
    printf ("\nEnter Client ID To Be Deleted:");
    scanf ("%d",&key);
    system ("cls");
    int first = 0;
    int last = max - 1;
    int middle = (first+last)/2;
    while( first <= last )
    {
        if (c[middle].client_id < key)
            first = middle + 1;    
        else if (c[middle].client_id == key) 
        { 
            c[middle].client_id=c[middle+1].client_id;
            strcpy(c[middle].client_business_name,c[middle+1].client_business_name);
            strcpy(c[middle].client_first_name,c[middle+1].client_first_name);
            strcpy(c[middle].client_last_name,c[middle+1].client_last_name);
            strcpy(c[middle].client_address,c[middle+1].client_address);
            c[middle].client_budget=c[middle+1].client_budget;
            strcpy(c[middle].client_business_info,c[middle+1].client_business_info);                     
            printf ("\nClient Removed!");  
            break;
        }
        else
            last = middle - 1;
        middle = (first + last)/2;
    }
    if ( first > last )
    {
        printf ("\nClient Not Registered\n");
    }
    system ("PAUSE");
}  

最佳答案

 while( first <= last )
 {
    if (c[middle].client_id < key)
       first = middle + 1;  
    else if (c[middle].client_id == key) 

您不会重新计算 if 子句中的中间值。因此,如果您输入该子句,您将首先设置 middle + 1,然后您将再次旋转循环,并且 middle 是相同的值。所以你会一次又一次地这样做,这就是你被吊死的原因。该子句不会推进您的循环。尝试像在 else 子句中那样更新 middle 。

关于c - 当我执行程序时,它关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29325232/

相关文章:

algorithm - 树中每对节点之间的距离

c - 使用 pthread_join 的 pthread 同步问题

python - 在 Python 中执行 N*M 迭代的最快算法

java - 使用java map /列表按相似结构分组

c - 结构元素值在执行期间被覆盖

Python数据结构/对象来建模静态多维表

java - 有效的 Java 项目 17 : How can overriding removeRange() improve performance?

c - 定义以下模块来显示系统摘要。

c - ARM IRQ 处理程序在 GCC 中无法正常工作

c - TTF/OTF 头表中的 checkSumAdjustment 有何用途?