473,395 Members | 1,368 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Tax Rate Array

Can not get this array to work. Instead of displaying just one result , I get all of them.



Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cctype>
  4.  
  5. using namespace std;
  6.  
  7. int main ()
  8. {
  9.  
  10. const double income_amt[] = {0,10000,20000,50000,100000};
  11. double tax_rates[]  = {0,0.05,0.10,0.15,0.20};
  12. int i = 0;
  13. double gross_pay = 0 ;
  14. double Taxes;
  15. char ans = 'n';
  16.  
  17. do{
  18.  
  19.     cout<<"Enter the Gross Pay: ";
  20.     cin>> gross_pay;
  21.  
  22.     for(i = 0; i<5; i++){
  23.  
  24.      if(( gross_pay > income_amt[i] ) && ( gross_pay > income_amt[i] )){
  25.  
  26.  
  27.  
  28.      Taxes = (gross_pay * tax_rates[i]);
  29. }
  30.  
  31.      cout<<"The Taxes are: "<<setw(10)<<fixed<<setprecision(2)<<Taxes<<endl;
  32.  
  33.  
  34.       } 
  35.      cout<<"Enter Y or y to continue.  Anything else to quit:"<<endl;
  36.      cin>> ans;
  37.  
  38. }while(toupper(ans)== 'Y');
  39.  
  40. return  0;
  41.  
  42. }
  43.  
  44.  
Sep 15 '15 #1
4 2328
weaknessforcats
9,208 Expert Mod 8TB
I don't understand your issue.

The tax display is inside the loop.
Sep 15 '15 #2
Instead of just display one answer it is displaying all of them.
For example, I enter the gross pay: 50001 the display should be "Taxes are: 7500.15" but I am getting all of them:
The Taxes are: 0.00
The Taxes are: 2500.05
The Taxes are:5000.10
The Taxes are:7500.15
The Taxes are:7500.15
Sep 15 '15 #3
weaknessforcats
9,208 Expert Mod 8TB
I say again your display is inside your loop.

You will get a display on each cycle of the loop, which is what you say is happening.
Sep 15 '15 #4
donbock
2,426 Expert 2GB
The for loop on line 22 causes all of the lines between 22 and 34 to execute 5 times. Of those five loop iterations, the tax calculation on line 28 will only execute for those cases where gross_pay > income_amt[i]; for your example of gross pay of 50001, that would be for i=0-3, but not 4.

@weaknessforcats has pointed out that The-Taxes-Are print statement is inside the for-loop but outside of the if-block so it is executed all 5 times through the loop. Do you want to move it inside the if-block (so it executes every time the tax is calculated) or perhaps outside of the for-loop (so it executes exactly once)?

Some other comments:
  1. Lines 13 and 14: floating-point variables are imprecise. That may be acceptable for scientific calculations, but most people insist on precision for calculations involving money.
  2. Line 24: the if-condition is of the form if(A && A). The "&& A" portion is useless and redundant. That can't be what you want.
  3. I assume that your intention is to use
    • tax_rates[0] if the gross pay is between income_amt[0] and income_amt[1];
    • tax_rates[1] if the gross pay is between income_amt[1] and income_amt[2];
    • tax_rates[2] if the gross pay is between income_amt[2] and income_amt[3];
    • tax_rates[3] if the gross pay is between income_amt[3] and income_amt[4]; and
    • tax-rates[4] if the gross pay is above income_amt[4].
    Your current logic does not do this.
    Also, how do you want to handle cases when the gross pay is equal to the low or high income threshold?
xx
Sep 15 '15 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: phjones | last post by:
Need help programming mortagage calculator for 3 different loans 7 year, 15 year and 30 year. using java array I am a beginner with Java, This is what I have so far. Need to know if I am off the...
1
by: Chuy08 | last post by:
If I have a multidimensional array like the following: Array $records =Array 0 = 30 year, 6.0; 1 = 30 year, 6.0; 2 = Pay Option, 1.0; 3 = Pay Option, 1.0; How could I flatten this to...
4
by: Chuy08 | last post by:
If I have a multdimensional array like so: $records = array( array('product' ='30 year','rate'=6.0 'ProgramID' =>9514), array('product' ='30 year','rate'=6.0 'ProgramID' =>9514),...
1
by: Chuy08 | last post by:
Currently I have the following XML document: <Programs> <Program> <ProductTypeName>30 Yr Fixed</ProductTypeName> <Rate>6.250</Rate> <APR>6.274</APR> <InvestorName>Helos</InvestorName>...
5
by: M. Fisher | last post by:
Pardon my ignorance here... I have created arrays such as: var SDET_Lab130= new Array(); SDET_Lab130 = new Array(); SDET_Lab130 = ; SDET_Lab130 = ; SDET_Lab130 = ; SDET_Lab130 = ;...
6
by: =?Utf-8?B?Sm9obiBCdW5keQ==?= | last post by:
Hey guys, I've searched high and low for a way to populate a vb array with data from a javascript array. I can find 50 ways to do it with ASP but not one for VB. I appreciate what help you can...
1
by: cronet | last post by:
Hi, i want to do some sortings to a complex array. The array structure is at the end of this message Now I want the result sorted in that way: SingleRoom ($hotel) - lowest rate ($hotel )
4
by: kidko | last post by:
I'm writing a small game in Javascript (using just PNGs for graphics and DOM manipulation to move them), and most of the content is procedurally generated. This includes all of the weapons involved,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.