0

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

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

    #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");

    }

我的程序可以运行到 while 循环else if (selection == 2)。它问我想要哪场拍卖。当我给它一个数字时,它只是冻结,崩溃,并且除了Process terminated with status -1073741510.

有任何想法吗?

4

1 回答 1

2

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

改变:

   scanf("%d", k);

   scanf("%d", &k);

并改变:

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

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

在两个地方。

于 2013-04-06T01:50:13.103 回答