C语言中的数组错误

时间:2015-12-11 02:23:50

标签: c arrays

当我在菜单中为选择2运行此代码时,在输入trn编号后,程序将显示没有第一个字母的投资类型,并且不会为其分配计算。同样,对于选择3,程序在菜单

中选择选项3后连续运行
    switch(option)
    {
    case 1:
        system("cls");
        printf(" How many clients are to be signed up ? \n");
            scanf(" %d",&num);
            system("cls");
            for(i=0;i<num;i++)
            {
                printf("\n Investment Type Available");
                printf("\n 1. Bonds 2. Stocks   3. MutualFund");
                printf("\n ");
                printf(" Enter the clients data \n");
                printf(" TRN              : ");
                scanf(" %s",&trn[i]);
                printf("\n Investment Type  : ");
                scanf(" %s",&p_type[i]);
                printf("\n Amount Investing : ");
                scanf(" %lf",&i_amount[i]);
                system("cls");
                t_amount=t_amount+i_amount[i];
            }
            getch();
            goto Menu;
        break;
    case 2:
        printf("\n Enter clients TRN : ");
        scanf("%s",&trn[i]);
        if(strcmp(trn[i],trn[i])==0)
        {
            printf("\n Investment type for this client : %s",&p_type[i]);

        }
        else
        {
            printf("No record found for this client");
            getch();
            return 0;
        }
        printf("\n TRN                     : %s",&trn[i]);
        printf("\n Investment Type         : %s",&p_type[i]);
        printf("\n Interest to be received  : $%0.2lf",s_interest);
        getch();
        goto Menu;
        break;
    case 3:
        printf("Each clients TRN & Investment Type :");
        for(i=0;i<num;i++)
        {
            if(i=num)
            {
                break;
            }
            else
            {
               printf("\n TRN : %s   Investment Type : %s",&trn[i],&p_type[i]);
            }
            if(lowest<i_amount[i])
            {
                lowest=i_amount[i];
            }
        }
        a_amount=t_amount/num;
        printf("The average amount that could be invested per person is : $%0.2lf",&a_amount);
        printf("The client with the least amount to invest ");
        break;
    case 4:
        printf("Exiting system");
        getch();
        return 0;
        break;
    default :
        printf("Invalid selection");
        getch();
        return 0;
    }
}

return 0;
getch();

}

1 个答案:

答案 0 :(得分:1)

问题是您正在尝试使用scanf()从键盘读取输入。那种方式就是疯狂。将所有scanf()调用更改为fgets()并更正必要的内容。

特别是将scanf()与getch()混合的恐怖。由于缓冲如何起作用,这是不确定的。