繁体   English   中英

C语言中的数组错误

[英]Array errors in C Language

当我在菜单中为选择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();

}

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

特别是将scanf()与getch()混合在一起的恐怖。 由于缓冲的工作方式,这是不确定的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM