0

好的,所以我正在尝试编写一个程序来竞标汽车。我想要做的是,如果当前出价为零,那么最低出价就是起始出价。如果当前出价不为零,则最低出价是当前出价加上最低出价。我不断收到一条错误消息incompatible types when assigning to type 'float[5]' from type 'float'

这是我的代码:

    #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 CAR[5]={1,2,3,4,5},START_BID[5]={0.00}, MIN_BID[5]={0.00}, CUR_BID[5]={0.00}, USR_BID[5]={0.00}; //Initialized float arrays
        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


        while (selection < 3)
    {
            scanf("%s", choice1);

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

            }


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

            }

            int result3 = strncmp(choice1, CLOSE, compareLimit); //Compare two strings
            if(result3 == 0)
            {
                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();
            }


            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 = CUR_BID[k-1] + MIN_BID[k-1];
                else
                    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();
            }

            else
            {
                int i;

                for (i=0; i<cars; i++)
                    if (CUR_BID!=0)
                        printf("Auction %d sold for $%f\n", CAR[i],CUR_BID[i]);
                    else
                        printf("Auction %d did not sell.\n");

                break;
            }
    }

            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)部分中所做的是询问我要出价的出价。然后我从 1-5 中选择的任何数字都在 int k 中。然后它检查该选择的当前出价,因此它在CUR_BID[k-1]数组中查找,如果数字为 3,则检查数组中的第二个位置。我收到以下错误:

    MIN_BID = CUR_BID[k-1] + MIN_BID[k-1]; 

有任何想法吗?

4

2 回答 2

5

MIN_BID是一个浮点数数组,但您试图只分配一个浮点数float。看起来你想要的是MIN_BID[k-1] = CUR_BID[k-1] + MIN_BID[k-1];.

于 2013-04-06T03:23:34.277 回答
0

MIN_BID有数组类型。你必须选择一个插槽。

MIN_BID[0] = ...
于 2013-04-06T03:23:29.617 回答