Hey everyone :) Hope you can help

Here is what I've done with the instructions given:

I need to know the average price difference between the cash and credit prices. But i'm pretty sure that only comes in at the end. Here is what my this function is supposed to do:
determine the difference between the cash price and credit price for an item and add this difference to the total of the differences. So what I've done below seems pretty basic to me- but I'm guessing this must be the error based on what I try to do with the code later on.]

void updateDifference(float cashPriceP, float creditPriceP, float & totalOfDifferencesP)
{
     float difference;
  difference = cashPriceP - creditPriceP;// This is probably wrong
  totalOfDifferencesP += difference;   
 }

Here is the main function:

int main()
{
for (int i= 1; i <= NR_ITEMS; i++)
{
	string description;
	int shelfStay;
	float wholesalePrice, cashPrice, creditPrice, totalOfDifferences, averagePriceDifference;
// initialise total
   totalOfDifferences = 0;



	inputAndValidate(description, shelfStay, wholesalePrice);
	determinePrices(shelfStay,wholesalePrice, cashPrice, creditPrice);
	updateDifference(cashPrice, creditPrice, totalOfDifferences);
	cout.setf(ios::fixed);
	cout.precision(2);
	cout << description << " is expected to stay on the shelf for " << shelfStay << " days and has a wholesale price"
         << endl << " of "
		<< "R" << wholesalePrice << endl;
		
		cout << " The cash price for " << description << " is R" << wholesalePrice << endl;
		
		cout << "The total of the difference between the cash and credit" << endl;
        cout << " prices is now R " << updateDifference << endl;

// updateDifference always gives me the same result which is R1 - so of course the average of the six items I enter will always be R1- this is obviously very wrong and that's why I say above that my function updateDifference must be the error.
     
       
 
        }
        //averagePriceDifference += updateDifference;
        //averagePriceDifference /= 6;// I want to put these 2 lines in the loop above but my compiler complains

		//cout << "The average price difference between the cash and credit prices is: " << endl;
		//cout << averagePriceDifference << endl;
		system("pause");
        return 0;
}

I'd love it if you could just point out any flaws you see in this piece of code. Thanks :)

Recommended Answers

All 7 Replies

Please use code tags.

Hmm i did, don't know what went wrong. Sure will next time

Do you have any error msgs?

please post them

Hey everyone :) Hope you can help

Here is what I've done with the instructions given:

I need to know the average price difference between the cash and credit prices. But i'm pretty sure that only comes in at the end. Here is what my this function is supposed to do:
determine the difference between the cash price and credit price for an item and add this difference to the total of the differences. So what I've done below seems pretty basic to me- but I'm guessing this must be the error based on what I try to do with the code later on.]

Yes it seems pretty basic.
I suppose it could be the error, if we had a clue what the error is. Without explanation of any kind, I can see either no error, or a possible error. But I'm only guessing.

When asking for help, assume we know nothing about your code, and your thoughts on the program. You need to explain in detail what you are trying to accomplish, and what leads you to believe there's an error.

Yes Sorry for not being so concise:
I've changed the program now and have no error complaints, but It's still wrong.

Here is the changed version:

#include <iostream>
#include <string>
using namespace std;
int const NR_ITEMS = 6;

void inputAndValidate(string & descriptionP, int & shelfstayP, float & wholesalePriceP)
{
	
	cout << "Enter the description: " << endl;
	cin >> descriptionP;
	cout << "Enter the number of days the item is expected to remain on the shelf: " << endl;
	cin >> shelfstayP;
	do
	{
	cout << "Enter the wholesale price of the item: " << endl;
	cin >> wholesalePriceP;
	}
	while (wholesalePriceP < 0);

}
void determinePrices(int shelfStayP, float wholesalePriceP, float & cashPriceP, float & creditPriceP)
{
     if (shelfStayP <= 7)
     cashPriceP = 1.1 * wholesalePriceP;
     else cashPriceP = 1.15 * wholesalePriceP;
     
     creditPriceP = 1.02 * cashPriceP;
 }

void updateDifference(float cashPriceP, float creditPriceP, float & totalOfDifferencesP)
{
 
  totalOfDifferencesP = cashPriceP - creditPriceP;

 }

int main()
{

	string description;
	int shelfStay;
	float wholesalePrice, cashPrice, creditPrice, totalOfDifferences;
    float totalOfDifferencesAvg = 0;

for (int i= 1; i <= NR_ITEMS; i++)
{
    totalOfDifferences = 0;
	inputAndValidate(description, shelfStay, wholesalePrice);
	determinePrices(shelfStay,wholesalePrice, cashPrice, creditPrice);
	updateDifference(cashPrice, creditPrice, totalOfDifferences);
	cout.setf(ios::fixed);
	cout.precision(2);
	cout << description << " is expected to stay on the shelf for " << shelfStay << " days and has a wholesale price"
         << endl << " of "
		<< "R" << wholesalePrice << endl;
		
		cout << " The cash price for " << description << " is R" << wholesalePrice << endl;
		
		cout << "The total of the difference between the cash and credit" << endl;
        cout << " prices is now R " << updateDifference << endl;
        totalOfDifferencesAvg += totalOfDifferences;
        cout << endl;
  
 
        }
        
        totalOfDifferencesAvg = totalOfDifferencesAvg / NR_ITEMS;
      
        cout << "The average price difference between the cash and credit is R" << totalOfDifferencesAvg;
        
		system("pause");
        return 0;
}

I give you the whole code just so you can see exactly what is going on. Just put questions next to code if you have a query about it.

The problem I have is with the totalDifferenceOfAverages.
I'm supposed to display the average price difference between the cash and credit prices. And if you see how I've done it above [totalOfDifferencesAvg += totalOfDifferences; and then outside the loop I divide by the number of items(6)] it should work perfectly ? But I keep on getting a negative answer with the following input:

Butter 514.99
Milk 316.99
Bread 26.75
Salami 1232.88
Wine 3955.49
Newspaper 15.99

I can give you the full question if you're not sure what's happening

Here's the context:

The CashAndCreditShop supermarket determines the retail price of a product based on the time it is expected to stay on the shelf. Any item that is expected to sell in one week or less is marked up 10%, and any item that is expected to stay on the shelf for more than one week is marked up 15% over the wholesale price.

Furthermore, all items are marked with two prices- one for paying cash, and one for buying on credit. Items on credit have a further markup of 2% on the initial markup.

Write a program to help CashAndCreditShop to determine the retail price for the items in their shop, based on the wholesale price and the number of days an item is expected to stay on the shelf.

I've got it. Here's the correct version:

#include <iostream>
#include <string>
using namespace std;
int const NR_ITEMS = 6;

void inputAndValidate(string & descriptionP, int & shelfstayP, float & wholesalePriceP)
{
	
	cout << "Enter the description: " << endl;
	cin >> descriptionP;
	cout << "Enter the number of days the item is expected to remain on the shelf: " << endl;
	cin >> shelfstayP;
	do
	{
	cout << "Enter the wholesale price of the item: " << endl;
	cin >> wholesalePriceP;
	}
	while (wholesalePriceP < 0);

}
void determinePrices(int shelfStayP, float wholesalePriceP, float & cashPriceP, float & creditPriceP)
{
     if (shelfStayP <= 7)
     cashPriceP = 1.1 * wholesalePriceP;
     else cashPriceP = 1.15 * wholesalePriceP;
     
     creditPriceP = 1.02 * cashPriceP;
 }

void updateDifference(float cashPriceP, float creditPriceP, float & totalOfDifferencesP)
{
 
  totalOfDifferencesP = creditPriceP - cashPriceP;

 }

int main()
{

	string description;
	int shelfStay;
	float wholesalePrice, cashPrice, creditPrice, totalOfDifferences;
    float totalOfDifferencesAvg = 0;
    totalOfDifferences = 0;
for (int i= 1; i <= NR_ITEMS; i++)
{
	inputAndValidate(description, shelfStay, wholesalePrice);
	determinePrices(shelfStay,wholesalePrice, cashPrice, creditPrice);
	updateDifference(cashPrice, creditPrice, totalOfDifferences);
	cout.setf(ios::fixed);
	cout.precision(2);

	cout << description << " is expected to stay on the shelf for " << shelfStay << " days and has a wholesale price"
         << endl << " of "
		<< "R" << wholesalePrice << endl;
		
		cout << " The cash price for " << description << " is R" << wholesalePrice << endl;
		
		cout << "The total of the difference between the cash and credit" << endl;
        cout << " prices is now R " << totalOfDifferences << endl;
        totalOfDifferencesAvg += totalOfDifferences;
        cout << endl;
  
 
        }
        
        totalOfDifferencesAvg = totalOfDifferences / NR_ITEMS;
      
        cout << "The average price difference between the cash and credit is R" << totalOfDifferencesAvg;
        
		system("pause");
        return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.