-3

Although I'll explain, here is the exercise detailed. So what the code is supposed to do is grab a double and apply a fixed tax to it then display the total amount of tax. Everything bellow 2000.00 is not taxed everything from 2000.01 to 3000.00 will be taxed in 8%, 3000.01 to 4500.00 with 18% and everything above 4500.00 with 28%. Therefore, when the user enter 4520.00 (this example is in the link) the code should sum (0.28 * 20 + 0.18 * 1500 + 0.08 * 1000) which is 355.60.

My code works not in the easiest way because I'm learning and I try to think of alternative and or creative ways to make it work, so I used vectors with the while statement to make a loop. It works (or it should) by subtracting salario by the highest value (4500) from the const vector renda and if the amount is not negative it applies another const as the tax making a sum to the total tax value which starts at 0.0. Then it resets the value of salario because otherwise it would have applied the taxes only to what is above 4500 and then it repeats two times this time repeating a part of the previous loop as in the example of 4520.00 in the second loop it would subtract salario (4520) by 3000 which would leave 1520 but we can't apply taxes again to that 20 because we already have so we repeat the part of the previous loop that left 20 and subtract that as well, we do the same in the last loop.

The problem is that I don't have the slightest idea as to what is wrong in the code. When I enter 4520.00 it returns 606.00 for some reason and I don't see logic behind it. If anyone can help me on this I would pretty much appreciate it.


#include <iostream>
#include <iomanip>
#include <vector>

using namespace std;

int main(){
    const vector <int> renda {2000, 3000, 4500};
    const vector <double> taxa {0.08, 0.18, 0.28};
    double salario {};
    double imposto {0.0};
    int i {2};
    
    cin >> salario;
    
    const double salario_const = salario;
    
    if (salario <= 2000.00)
        cout << "Isento" << endl;
    
    else {
        while (i >= 0) {
            if (i == 2) 
                salario -= renda.at(i);
            else 
                salario -= renda.at(i) - (salario - renda.at(i+1));
            if (salario > 0) 
                imposto += salario*taxa.at(i);
            salario = salario_const;
            i -= 1;
            }
        cout << setprecision(2) << fixed;
        cout << "R$ " << imposto << endl;
    }
    
    return 0;    
    }
  • 3
    Welcome to Stack Overflow! It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: [How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/) and [Debugging Guide](http://idownvotedbecau.se/nodebugging/) – NathanOliver May 05 '22 at 15:43
  • Can you tell us, in words, what this code is supposed to do and what it is doing? Right now, I get constant output `Isento` and no errors, which makes sense because `salario` is the constant `0`, so the `else` block never even triggers. – Silvio Mayolo May 05 '22 at 15:43
  • You may also want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) – Andreas Wenzel May 05 '22 at 15:47
  • 1
    Welcome to Stack Overflow. Please read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Also please learn how to *[edit]* your questions to improve them, like telling us the input you give, the expected output, and the actual output. – Some programmer dude May 05 '22 at 15:48
  • 1
    It seems weird that in your `while` loop, you always change the value of `salario` regardless of what `i` is, and then you immediately set it back to `salario_const` which never changes. Is that intentional? – Nathan Pierson May 05 '22 at 15:48

0 Answers0