Cash Register

I have been working on a code that I found on line and trying to add sales tax to it but every time I run the program the sales tax isn’t completely correct and I’m also missing decimals after the final total. Here’s a screen show of the program when finished showing the issues along with the code.

image

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	//declare arrays
	string itemName[100];
	double itemCost[100] = { 0.0 };
	const double saleTax = 0.07;

	//declare variables
	string names = "";
	double total = 0.0;
	int numItems = 0;
	//number of items
	cout << "Enter number of grocery items you will be entering: ";
	cin >> numItems;

	for (int i = 1; i >= 100; i++)
	{
		cin >> itemName[i] >> itemCost[i];
	}

	//input message
	cout << "Please enter the item names as one word only.Example: tomatoes\n";
	cout << "Enter the cost as a decimal number.Example: 2.44\n\n";

	//item name and price input
	for (int sub = 1; sub <= numItems; sub += 1)
	{
		cout << "Enter item " << sub << ":";
		cin >> itemName[sub];
		cout << "Enter the cost of the " << itemName[sub] << ":";
		cin >> itemCost[sub];
	}
	cout << "Items" << " " << "Cost" << endl;
	//Display Data
	for (int i = 1; i <= numItems; ++i)
	{
		cout << itemName[i];
		cout << ' ';
			cout << "$" << itemCost[i];
		cout << endl;
	}// end for

	for (int i = 1; i <= 100; ++i)
	{
		total = (total + itemCost[i]);

	}
	int Tax = total * saleTax;
	int Grandtotal = total + Tax;
	cout << "Sale Tax : " << Tax << endl; cout << "Grocery : " << total << endl;
	cout << "Total:$" << Grandtotal << endl;

	system("PAUSE");
	return 0;
}

You would need to set the precision using std::setprecision and std::fixed

std::cout << std::fixed << std::setprecision(2);

Then uses of floating point numbers after that should show up to two decimal places


Comments on code:

  1. Please don’t use using namespace std;. It’s bad practice.
  2. Do not declare all variables at the top of the function. Declare them at the point of use. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-introduce
  3. numItems should be compared to see if it’s valid, you’d have problems if a number > 100 or < 0 (not that 0 is useful) is entered.
  4. This loop does nothing as the condition is always initially false.
     for (int i = 1; i >= 100; i++)
     {
     	cin >> itemName[i] >> itemCost[i];
     }
    
  5. Array indexes start at 0 not 1. By convention you start at 0 and compare with < Amount .
  6. sub += 1 can be ++sub, also not quite sure what sub is supposed to stand for?
  7. for (int i = 1; i <= 100; ++i), this should check against numItems not 100 (also the 0 and < thing mentioned previously).
  8. total = (total + itemCost[i]); this can just be += (also the parens weren’t necessary).
  9. Tax and GrandTotal are inconsistent with the rest of your variable names. You ought to be consistent with your style.
  10. system("PAUSE"); please don’t use system calls. The terminal can be configured to stay open with whatever IDE you are using.

And please use a code block in the future using the </> button.

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms