gpt4 book ai didi

c - 如何通过 if 循环指向一个数组

转载 作者:太空宇宙 更新时间:2023-11-04 06:03:02 25 4
gpt4 key购买 nike

好的,所以我正在尝试编写一个程序来为汽车出价。我想做的是,如果当前出价为零,则最低出价就是起始出价。如果当前出价不为零,则最低出价为当前出价加上最低出价。当从类型“float”分配给类型“float[5]”时,我不断收到一个错误,指出不兼容的类型

这是我的代码:

    #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]; 

有什么想法吗?

最佳答案

MIN_BID 是一个 float 数组,但您正试图为其分配一个 float。看起来您想要的是 MIN_BID[k-1] = CUR_BID[k-1] + MIN_BID[k-1];

关于c - 如何通过 if 循环指向一个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15846675/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com