当我执行我的程序时,它会关闭

时间:2015-03-29 03:12:03

标签: c data-structures

当我执行我的程序时,它会关闭。编译完成后,程序窗口打开,然后我可以输入客户端的ID,然后冻结,然后新的弹出窗口显示程序已停止工作,需要关闭。我把问题隔离到了这一行,第一个=中间+ 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");
}  

2 个答案:

答案 0 :(得分:0)

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

你不是在if子句中重新计算中间位置。因此,如果你输入那个子句,你将首先设置为中间+ 1,然后你将再次旋转循环,中间是相同的值。所以你会再次这样做,这就是你挂起的原因。该子句不会推进你的循环。尝试像在else子句中那样更新中间版。

答案 1 :(得分:-1)

  1. main声明需要int main()而不是main()
  2. 缺少#include <stdlib.h>,因此系统函数:system()未定义
  3. 变量y在设置为已知值
  4. 之前使用

    解决这些问题,发布更正后的代码 然后我们会更好地帮助你。

    编译步骤需要始终启用所有警告; 警告是来自编译器的消息,它认为存在问题 这些警告很重要。
    修复代码,使其在编译步骤中不会产生警告。

    一般来说, 你浪费时间链接/运行编译警告的代码。

    typedef

    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;
    

    不是定义结构的好方法。 结构永远不应该是typedef d 因为这会使代码混乱,使编译器名称空间变得混乱 并没有帮助实现任何算法。

    这是定义结构的好方法:

    struct  Client //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];
    };
    

    然后对结构的所有引用都应该写成: struct Client ...

相关问题