如何通过if循环指向数组

时间:2013-04-06 03:20:51

标签: c

好的,所以我正在尝试编写一个我在汽车上竞标的程序。我想要做的是,如果当前出价为零,那么最低出价是起始出价。如果当前出价不为零,则最低出价是当前出价加上最低出价。我一直收到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]; 

有什么想法吗?

2 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:0)

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

MIN_BID[0] = ...