帮帮我! [英] Help me Out!

查看:84
本文介绍了帮帮我!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我是C ++的新手,我被要求写一个程序来读取/获取客户详细信息,例如姓名,购买金额和税额.然后,该程序将计算营业税(税额*购买额)和应付额(营业税+采购额).我像下面的代码一样编写了代码,但是它第一次拒绝了税额,然后第二次接受了税额.然后,该程序将无法正确计算预期结果.请帮帮我!

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;
		}
	}
}

推荐答案

您有三个主要问题.
1.输入税率后,do { } while获取税率将始终至少执行一次.要么丢掉第一个获得税率的块,要么将do { } while块更改为while { }块.
2.获得税率后,需要将计算computedTaxsalexTaxamountDue的代码移至.
3.您不需要开关块.您已经完成了计算,只需要打印结果,所有税率都相同.

干杯,
彼得

投票答案,如果感到高兴就标记为已接受.
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.


这篇关于帮帮我!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆