Having problem with this code, it works but does not pass the \"Testing if funct
ID: 3670227 • Letter: H
Question
Having problem with this code, it works but does not pass the "Testing if function PromptForInteger is valid... " test. Below is the assignment, codethat I have so far, and the output. Thanks.
20.2 C++: Calculate salary: Using functions.
Separating calculations into functions simplifies modifying and expanding programs.
The following program calculates the tax rate and tax to pay, using functions. One function returns a tax rate based on an annual salary.
Run the program below with annual salaries of 40000, 60000, and 0.
Change the program to use a function to input the annual salary. The function MUST be called "PromptForInteger" and it must have as an argument a string.
Run the program again with the same annual salaries as above. Are results the same?
#include <iostream>
#include <vector>
#include <stdexcept>
using namespace std;
int PromptForInteger(const string userPrompt);
double GetCorrespondingTableValue(int search, vector<int> baseTable, vector<double> valueTable) {
int baseTableLength = baseTable.size();
double value = 0.0;
int i = 0;
bool keepLooking = true;
while ((i < baseTableLength) && keepLooking) {
if (search <= baseTable.at(i)) {
value = valueTable.at(i);
keepLooking = false;
}
else {
++i;
}
}
return value;
}
int main() {
int annualSalary = 0;
double taxRate = 0.0;
int taxToPay = 0;
int i = 0;
vector<int> salaryBase(5);
vector<double> taxBase(5);
salaryBase.at(0) = 0;
salaryBase.at(1) = 20000;
salaryBase.at(2) = 50000;
salaryBase.at(3) = 100000;
salaryBase.at(4) = 99999999;
taxBase.at(0) = 0.0;
taxBase.at(1) = 0.10;
taxBase.at(2) = 0.20;
taxBase.at(3) = 0.30;
taxBase.at(4) = 0.40;
// FIXME: Change the input to come from a function
cout << " Enter annual salary (negative to exit): " << endl;
cin >> annualSalary;
while (annualSalary > 0) {
taxRate = GetCorrespondingTableValue(annualSalary, salaryBase, taxBase);
taxToPay = static_cast<int>(annualSalary * taxRate); // Truncate tax to an integer amount
cout << "Annual salary: " << annualSalary <<" "<<
"Tax rate: " << taxRate << " "<<
"Tax to pay: " << taxToPay << endl;
// Get the next annual salary
// FIXME: Change the input to come from a function
cout << " Enter annual salary (negative to exit): " << endl;
cin >> annualSalary;
}
return 0;
}
Submitted: 08:51 am on 02/21/16
Total: 1/2
Unit test
0/1
Testing if function PromptForInteger is valid...
Compare output
1/1
Input
60000 40000 1000000 -1
Your output
Enter annual salary (negative to exit): Annual salary: 60000 Tax rate: 0.3 Tax to pay: 18000 Enter annual salary (negative to exit): Annual salary: 40000 Tax rate: 0.2 Tax to pay: 8000 Enter annual salary (negative to exit): Annual salary: 1000000 Tax rate: 0.4 Tax to pay: 400000 Enter annual salary (negative to exit):
"
Input
60000 40000 1000000 -1
Your output
Enter annual salary (negative to exit): Annual salary: 60000 Tax rate: 0.3 Tax to pay: 18000 Enter annual salary (negative to exit): Annual salary: 40000 Tax rate: 0.2 Tax to pay: 8000 Enter annual salary (negative to exit): Annual salary: 1000000 Tax rate: 0.4 Tax to pay: 400000 Enter annual salary (negative to exit):
Explanation / Answer
Here is your code corrected and added the function PromptForInteger.
#include <iostream>
#include <vector>
#include <stdexcept>
using namespace std;
int PromptForInteger(const string userPrompt)
{
int aSal =0; // declare a variable
cout << " " << userPrompt;
cin >> aSal; // Accept the input from the user.
return aSal; // return the input value to the calling function.
}
double GetCorrespondingTableValue(int search, vector<int> baseTable, vector<double> valueTable) {
int baseTableLength = baseTable.size();
double value = 0.0;
int i = 0;
bool keepLooking = true;
while ((i < baseTableLength) && keepLooking) {
if (search <= baseTable.at(i)) {
value = valueTable.at(i);
keepLooking = false;
}
else {
++i;
}
}
return value;
}
int main() {
int annualSalary = 0;
double taxRate = 0.0;
int taxToPay = 0;
int i = 0;
vector<int> salaryBase(5);
vector<double> taxBase(5);
salaryBase.at(0) = 0;
salaryBase.at(1) = 20000;
salaryBase.at(2) = 50000;
salaryBase.at(3) = 100000;
salaryBase.at(4) = 99999999;
taxBase.at(0) = 0.0;
taxBase.at(1) = 0.10;
taxBase.at(2) = 0.20;
taxBase.at(3) = 0.30;
taxBase.at(4) = 0.40;
// FIXME: Change the input to come from a function
// cout << " Enter annual salary (negative to exit): " << endl; I have commented it.
// cin >> annualSalary; You can delete these.
annualSalary = PromptForInteger("Enter annual salary (negative to exit): ");
while (annualSalary > 0) {
taxRate = GetCorrespondingTableValue(annualSalary, salaryBase, taxBase);
taxToPay = static_cast<int>(annualSalary * taxRate); // Truncate tax to an integer amount
cout << "Annual salary: " << annualSalary <<" "<<
"Tax rate: " << taxRate << " "<<
"Tax to pay: " << taxToPay << endl;
// Get the next annual salary
// FIXME: Change the input to come from a function
// cout << " Enter annual salary (negative to exit): " << endl; // you can delete these.
// cin >> annualSalary; as is the case.
annualSalary = PromptForInteger("Enter annual salary (negative to exit): ");
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.