Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey out there, I am new to C++ and I was asked to write a program that reads/gets customer details like names, purchase amount and tax amount. The program is then to calculate the sales tax(tax amount * purchase amount) and amount due(sales tax + purchase amount). I wrote the code like the one below but it rejects the tax amount for the first time and then it accepts it for the second time. Then after that, the program does not compute the expected results correctly. Please help me out!

#include <iostream>

using namespace std;

void tax(void)
{
	char custName[100];
	double purchaseAmnt;
	int taxAmnt;

	cout << "\nCustomer Names : ";
	cin >> custName;

	system("cls");
	cout << "\nPurchase Amount : RM ";
	cin >> purchaseAmnt;

	system("cls");
	cout <<"	TAX MENU\n\n0% - Tax Exempt\n3% - State Sales Tax";
	cout <<"\n5% - Federal Sales State Tax\n7% - Special Sales Tax";
	cout << "\n\nTax Amount.e.g. 3 for 3% : ";
	cin >> taxAmnt;
	
	double computedTax = taxAmnt/100;
	double salesTax = (purchaseAmnt * computedTax);
	double amountDue = purchaseAmnt + salesTax;

	do 
	{
		cout<<"Please enter the correct percentage rate : ";
		cin >> taxAmnt;
	}while ((taxAmnt!=0) && (taxAmnt!=3)&& (taxAmnt!=5) && (taxAmnt!=7));

	switch(taxAmnt)
	{
	case 0:	//Tax Exempt(0%)
		{			
			system("cls");
			cout << "\nNAME            : " << custName <<endl;
			cout << "PURCHASE AMOUNT : " << purchaseAmnt <<endl;
			cout << "TAX AMOUNT      : " << taxAmnt <<"%"<<endl;
			cout << "SALES TAX       : RM " << (purchaseAmnt * computedTax) <<endl;
			cout << "AMOUNT DUE      : RM " << amountDue <<endl;
			break;
		}

	case 3:	//State Sales Tax(3%)
		{
			system("cls");
			cout << "\nNAME            : " << custName <<endl;
			cout << "PURCHASE AMOUNT : " << purchaseAmnt <<endl;
			cout << "TAX AMOUNT      : " << taxAmnt <<"%"<<endl;
			cout << "SALES TAX       : RM " << salesTax <<endl;
			cout << "AMOUNT DUE      : RM " << amountDue <<endl;
			break;
		}

	case 5: //Federal and State Sales Tax(5%)
		{
			system("cls");
			cout << "\nNAME            : " << custName <<endl;
			cout << "PURCHASE AMOUNT : " << purchaseAmnt <<endl;
			cout << "TAX AMOUNT      : " << taxAmnt <<"%"<<endl;
			cout << "SALES TAX       : RM " << salesTax <<endl;
			cout << "AMOUNT DUE      : RM " << amountDue <<endl;
			break;
		}

	case 7:	//Special Sales Tax(7%)
		{
			system("cls");
			cout << "\nNAME            : " << custName <<endl;
			cout << "PURCHASE AMOUNT : " << purchaseAmnt <<endl;
			cout << "TAX AMOUNT      : " << taxAmnt <<"%"<<endl;
			cout << "SALES TAX       : RM " << salesTax <<endl;
			cout << "AMOUNT DUE      : RM " << amountDue <<endl;
			break;
		}
	}
}
Posted
Updated 8-Nov-10 20:57pm
v2

1 solution

You have three main problems.
1. The do { } while to get the tax rate will always be executed at least once, after you have already input the tax rate. Either throw away the first block getting the tax rate, or change the do { } while block to a while { } block.
2. You need to move the code that calculates computedTax, salexTax and amountDue to after you have got the tax rate.
3. You don't need the switch block. You've done the calculations, and you just need to print the results, which is the same for all tax rates.

Cheers,
Peter

Vote for answers, and mark as accepted if you're happy.
 
Share this answer
 
Comments
Chief894 9-Nov-10 0:40am    
Thank you a lot, before I did the do{ } while{ }, I tried the while{ } loop, but the thing is, it loops non-stop until I terminate the execution manually! But your response is so helpful, I now have a clear picture on what to do!

Cheesr!
Chief894 9-Nov-10 1:51am    
Thank you again, your response helped, it worked!
Dalek Dave 9-Nov-10 3:43am    
Good Answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900