0

I am writing a POS system for HW, and I had to take my first code which used hundreds of variables and change it into a more compact program using arrays. This is my new code:

    #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();
}    

When I get to inputting the quantity at iItemQuantity[iItemAmout] it will crash the program with an unhandled exception. I also did some debugging print statements inbetween code to see where it gets to and output what variables I inputed and the returned values did not match up, all I received were 0.00.

Help would be much appreciated and I hope I am not asking an already answered question somewhere else. Still don't really know how to navigate the site.

I know the code does not do anything else at present time but I do not see the point in continuing until the first problems are corrected.

  • 1
    maaan... use a `struct` – Karoly Horvath Mar 19 '13 at 10:03
  • Oh God, why Hungarian notation, why? –  Mar 19 '13 at 10:03
  • You set `iItemCount` to one the beginning of the loop, so your increment at the end will be overwritten. Leave it be, it's already initialized to zero when you declare it. Just make sure it's less than 100 (the size of your arrays). – Some programmer dude Mar 19 '13 at 10:06
  • I have not learnt structs yet in my class, I have heard they are great but teachers won't let me use code we haven't been taught yet. Hungarian notation is what I use as I know what variable it is when its in the code. Thank you Joachim, I will have a look at that and see if it fixes it, newbie error. EDIT: Moved that variable out of the loop, but it still does not fix the issue I am having but is removing a future issue. – TheAngryBr1t Mar 19 '13 at 10:10

2 Answers2

1

use & in scanf for array elements also

like this

scanf("%.2f", &fItemPrice[iItemCount]);

scanf("%d", &iItemQuantity[iItemCount]);
999k
  • 6,257
  • 2
  • 29
  • 32
0

The problem is that scanf expects pointers to the values, not the actual values themselves. You do it correctly before the loop, but not for the input inside the loop.

Change e.g.

scanf("%.2f", fItemPrice[iItemCount]);

to

scanf("%.2f", &fItemPrice[iItemCount]);

or

scanf("%.2f", fItemPrice + iItemCount);

Do the same for all input.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621