指针和数组需要帮助

时间:2013-04-06 01:40:45

标签: c

好的,所以我正在编写一个从文件中读取输入并将它们放入数组的程序。我试图使用指针与数组,所以我可以指向数组中的某个位置,并将用户定义的浮点数添加到已存在的浮点数。

到目前为止,这是我的代码:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    int menu1();

    int main()
    {
        FILE * ifp = fopen("input2.txt","r"); //Open the input file
        int cars = 5, i , j, k; // Initialized cars and counters i, j, and k
        char *VIEW="VIEW", *BID="BID", *CLOSE="CLOSE", choice1[20]; //Initialize character arrays
        float START_BID[5]={0.00}, MIN_BID[5]={0.00}, CUR_BID[5]={0.00}, USR_BID[5]=0.00}; 
        int compareLimit = 100, selection=0;

        //Scan the file and appropriate the numbers into their respective arrays
        for (i = 0; i < cars; i++)
            {
                fscanf(ifp, "%f %f", &START_BID[i],&MIN_BID[i]);
            }


        printf("Welcome to the Silent Auction\n\n");
        menu1(); //Display the menu
        scanf("%s", &choice1); //



        int result = strncmp(choice1, VIEW, compareLimit); //Compare two strings
        if(result == 0)
        {
            selection = selection + 1;

        }


        int result2 = strncmp(choice1, BID, compareLimit); //Compare two strings
        if(result2 == 0)
        {
            selection = selection + 2;

        }

        int result3 = strncmp(choice1, CLOSE, compareLimit); //Compare two strings
        if(result3 == 0)
        {
            selection = selection + 3;
        }


        while (selection < 3)
    {
            if (selection == 1)
            {
                printf("Number\tCurrent Bid\tMinimum Increase\n");
                printf("1\t$%.2f\t\t$%.2f\n",CUR_BID[0], MIN_BID[0]);
                printf("2\t$%.2f\t\t$%.2f\n",CUR_BID[1], MIN_BID[1]);
                printf("3\t$%.2f\t\t$%.2f\n",CUR_BID[2], MIN_BID[2]);
                printf("4\t$%.2f\t\t$%.2f\n",CUR_BID[3], MIN_BID[3]);
                printf("5\t$%.2f\t\t$%.2f\n",CUR_BID[4], MIN_BID[4]);

                menu1();
                scanf("%s", &choice1);
            }


            else if (selection == 2)
            {
                int k;
                float usr_bid;

                printf("Which auction would you like to bid on? (1-5)\n");
                scanf("%d", k);

                if (CUR_BID[k - 1] = 0.00)
                    MIN_BID[k - 1] = START_BID[k - 1];
                else
                    MIN_BID[k - 1] = CUR_BID[k - 1] + MIN_BID[k - 1];

                printf("The minimum bid is %.2f\n", MIN_BID[k - 1]);
                printf("How much would you like to bid?\n");
                scanf("%f", usr_bid);

                if (usr_bid < MIN_BID[k-1])
                    printf("Sorry, that bid is not high enough.\n");
                else
                    CUR_BID[k - 1] = usr_bid + CUR_BID[k - 1];

                menu1();
                scanf("%s", &choice1);
            }

            else
            {
               int i;
               int auction = 1;

               for (i=0; i < cars; i++)
               {
                    for (auction = 1; auction < cars; auction++)
                    {
                        while (CUR_BID[i]!= 0.00)
                            printf("Auction %d sold for $%.2f", auction, CUR_BID);

                    }
               }
            }
    }



            fclose(ifp);



        return 0;
    }


    int menu1()
    {
        printf("Please make a selection (In all caps):\n");
        printf("\tView Auctions [VIEW]\n");
        printf("\tBid on an Auction [BID]\n");
        printf("\tClose Auctions [CLOSE]\n");

    }

我的程序适用于else if (selection == 2)所在的while循环。它问我哪个 拍卖我想要。当我给它一个数字时,它只是冻结,崩溃,除了Process terminated with status -1073741510之外没有给我任何错误。

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

传递给scanf()的指针不正确。

变化:

   scanf("%d", k);

   scanf("%d", &k);

并改变:

scanf("%s", &choice1); // 

 scanf("%s", choice1); // 

在两个地方。