我正在为HW编写POS系统,我不得不使用我的第一个使用数百个变量的代码,并使用数组将其更改为更紧凑的程序。这是我的新代码:
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
//---------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------
//------------------------------------------Declaring-Variables--------------------------------------------
//---------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------
//Discount Variables
int iPerPurchaseDiscountPercent = 0; //enter a number which will be later changed to a percentage
int iFinalPurchaseDiscountPercent = 0;//enter a number which will be later changed to a percentage
float fPerPurchaseDiscountPercent = 0;//the percentage value of discount per item
float fFinalPurchaseDiscountPercent = 0;//percentage value of final dicount
float fPerPurchaseDiscountPrice = 0;//price at which discounts will be applied
float fFinalPurchaseDiscountPrice = 0;//price at which the final shop discount will be appled
//Array values for math
float fItemPrice[100] = { 0 }; //Price of Item
int iItemQuantity[100] = { 0 }; //Quantity of that Item
float fBundleCost[100] = { 0 }; //Price before Discounts (fItemPrice[N] * fItemQuantity[N])
float fDiscountPerItem[100] = { 0 }; //What discount is recieved per item (fItemPrice[N] * iPerPurchaseAmount)
float fItemTotalDiscountRecieved[100] = { 0 }; //Total discount recieved on multiple items of same type (fDicountPerItem * iItemQuantity)
float fDiscountPrice[100] = { 0 };//Price for item after all discounts (fBundleCost - fDiscountRecieved)
//Values for while loop
bool bStillShopping = true;
int iItemCount = 0;
char cExitQuestion = 'y';
//---------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------
//-----------------------------------------------Prototyping-----------------------------------------------
//---------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------
//------------------------------------------------Main-Loop------------------------------------------------
//---------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------
int main(void)
{
printf("Welcome to Blacketts bookstore!\t\t\t\t\t\tPOS V2\n");
//Taking values for the per item discounts
printf("Please enter the price that discounts per item will be applied: ");
flushall();
scanf("%f", &fPerPurchaseDiscountPrice); //takes the value at which items will be discounted
printf("Please enter the percentage to be applied at this price: ");
flushall();
scanf("%d", &iPerPurchaseDiscountPercent); //takes a value eg 10 to be later changed to .1 for 10%
fPerPurchaseDiscountPercent = iPerPurchaseDiscountPercent/100; //changes the int to a float and makes it appropriate for percentage calculations
//Taking values for the final purchase discount
printf("Please enter the price that end of sale discounts will be applied to: ");
flushall();
scanf("%f", &fFinalPurchaseDiscountPrice); //takes the value at which the whole docket will be discounted by
printf("Please enter the percentage to be applied at this price: ");
flushall();
scanf("%d", &iFinalPurchaseDiscountPercent);//takes a value eg 5 to be later changed to .05 for 5%
fFinalPurchaseDiscountPercent = iFinalPurchaseDiscountPercent/100; //changes the int to a float and make it appropriate for percentage calculations
//While loop to take values and input them into appropriate places
while(bStillShopping == true)
{
iItemCount = 1; // Counting how many items are being purchased, 0 = 1st item. therefore Total quantity Items must equal iItemCount+1
printf("\nPlease enter the price of the first item to be purchased: "); //enter price of item
flushall();
scanf("%.2f", fItemPrice[iItemCount]);
printf("Please enter the quantity of that item: "); //enter quantity
flushall();
scanf("%d", iItemQuantity[iItemCount]);
printf("\nWould you like to enter any more Items? (y/n): "); //ask to continue
flushall();
scanf("%c", &cExitQuestion);
if(cExitQuestion == 'n')
{
bStillShopping = false; //if dont want to continue exit the loop
}
else
{
iItemCount++; //if do continue increment item loop and ask for more variables
}
}
getch();
}
当我在iItemQuantity[iItemAmout]
处输入数量时,它将使程序崩溃并出现未处理的异常。我还在代码之间做了一些调试打印语句以查看它到达的位置并输出我输入的变量以及返回的值不匹配,我收到的都是0.00
。
非常感谢帮助,我希望我不会在其他地方问一个已经回答的问题。仍然不知道如何浏览网站。
我知道代码目前没有做任何其他事情,但在第一个问题得到纠正之前,我没有看到这一点。
答案 0 :(得分:1)
在&
中使用scanf
表示数组元素
scanf("%.2f", &fItemPrice[iItemCount]);
scanf("%d", &iItemQuantity[iItemCount]);
答案 1 :(得分:0)
问题是scanf
需要指向值的指针,而不是实际值本身。你可以在循环之前正确地完成它,但不能用于循环内的输入。
改变,例如
scanf("%.2f", fItemPrice[iItemCount]);
到
scanf("%.2f", &fItemPrice[iItemCount]);
或
scanf("%.2f", fItemPrice + iItemCount);
对所有输入执行相同的操作。