Apr 2, 2016 at 11:27pm Apr 2, 2016 at 11:27pm UTC
In my class I had to make a program where it took the name of county and its tax rate from a input file and displays it sorted. Everytime I test it with an output file (which is not needed for this program) It would only show whatever was the last thing on the input file as a the 10th county and all the tax rates would be 0.00%.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
#include<iostream>
#include<fstream>
#include<iomanip>
#include <stdlib.h>
using namespace std;
const int COUNTY_MAX = 10;
void sort(string county[], double tax_rate[]);
int main()
{
string county[COUNTY_MAX];
double tax_rate[COUNTY_MAX];
int index = 0;
ifstream input;
ofstream output;
string fname;
//Intro
cout << "Welcome to the Tennessee Sales Tax Report! \n" ;
cout << "Enter input file name.\n" ;
cin >> fname;
//opening file of correct
input.open(fname.c_str());
if (input.fail())
{
cout << "Input file " << fname << " does not exist.\n" ;
exit(0);
}
/* else
if (is_empty(fname))
{
cout << "Input file " << fname << " is empty.\n";
}
{
return 0;
}*/
while (input >> county[index]);
{
input >> tax_rate[index];
index++;
}
//Close input file
input.close();
//Call function to sort arrays in descending order by tax_rate
sort(county, tax_rate);
output.open("Guard.txt" );
//Write file to output
output << setw(15) << "COUNTY" << setw(15)<< "TAX RATE" << endl;
output << "------------------------------------" << endl;
for (index = 0; index<COUNTY_MAX; index++)
{
output << setw(15) << county[index] << setw(12) <<setprecision(2) << fixed << tax_rate[index] << endl;
}
//Close output file
output.close();
return 0;
}
//Function to sort both arrays in descendin order by tax_rate
void sort(string county[], double tax_rate[])
{
for (int i=0; i<COUNTY_MAX; i++)
{
for (int j=i+1; j<COUNTY_MAX; j++)
{
if (tax_rate[i] < tax_rate[j])
{
//Swap tax_rate
double temp = tax_rate[i];
tax_rate[i] = tax_rate[j];
tax_rate[j] = temp;
//Swap county
string t = county[i];
county[i]= county[j];
county[j]= t;
}
}
}
}
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
bleh 0.00
Last edited on Apr 2, 2016 at 11:30pm Apr 2, 2016 at 11:30pm UTC
Apr 3, 2016 at 12:46am Apr 3, 2016 at 12:46am UTC
Wow, I can't believe that actually worked. I wonder why the compiler was accepting it like that.
Apr 3, 2016 at 1:06am Apr 3, 2016 at 1:06am UTC
My code sorts it in ascending order, so how would I change the function at the end to do the opposite? Here is an example of what it's showing. (i know they are not real counties :P)
Milk 200.00%
Chocolate 160.00%
Fudge 2.00%
Last edited on Apr 3, 2016 at 1:15am Apr 3, 2016 at 1:15am UTC