Sort regardless of uppercase or lowercase in a .txt file

How do you make it to where it sorts from A-Z regardless of upper or lower case? The function so far sorts it from A-Z uppercase and then lowercase.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void sort(string county[], double tax_rate[])
{
   for (int i=0; i<COUNTY_MAX; i++)
   {
      for (int j=0; j<COUNTY_MAX; j++)
      {
           if (county[i] < county[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;
           }
      }
   }
}
You could convert the letters you are using for comparison to the same case on line 7 with something like tolower().

As a tangentially related recommendation, I'd make a struct to hold your county name and tax rate pairs instead of trying to manage parallel arrays. It makes managing them much easier by keeping them together.
Would it be something like this?
1
2
3
4
5
struct County
{
string county;
double tax_rate;
};
Yep. Then you can pass around an array of County objects instead of two arrays you have to manually keep in sync.
Edit:
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
#include<iostream>
#include<fstream>
#include<iomanip>
#include <stdlib.h>
#include<cctype>
using namespace std;
struct County
{
string county;
double tax_rate;
};
std::string to_lower( std::string str ) // return string converted to lower case
{
         for( char& c : str ) c = std::tolower(c) ;
             return str ;
}
const int COUNTY_MAX = 10;
void sort(County countyList[]);
void display(County countyList[]);
int main()
{
     County countyList[COUNTY_MAX];
     int index=0;
     ifstream input;
     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);
     }
     while (input >> countyList[index].county)
     {
          input >> countyList[index].county;
          index++;
     }
     //Close input file
     input.close(); 
     //Call function to sort arrays in descending order by tax_rate
     sort(countyList);
     display(countyList); 
     return 0;
}

//Function to sort both arrays in descending order by tax_rate
//void sort(string county[], double tax_rate[])
void sort(County countyList[])
{
     for (int i=0; i<COUNTY_MAX; i++)
     {
          for (int j=0; j<COUNTY_MAX; j++)
          {
               if ( to_lower(countyList[i].county) < to_lower(countyList[j].county))
               {
                    County temp = countyList[i];
                    countyList[i]=countyList[j];
                    countyList[j]=temp;
               }
          }
     }
}
//Function to display the sorted file content
void display(County countyList[])
{
     cout << setw(12) << "COUNTY" << setw(14) << "TAX RATE" << endl;
     cout << "-----------------------------------" << endl;
     for (int index = 0; index < COUNTY_MAX; index++)
     {
          cout << setw(12) << countyList[index].county << setw(12) << setprecision(2) << fixed << countyList[index].tax_rate << "%" << endl;
     }
} 
Last edited on
Not efficient, but this should suffice.

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
#include <iostream>
#include <string>
#include <cctype>
#include <iomanip>

std::string to_lower( std::string str ) // return string converted to lower case
{
    // http://www.stroustrup.com/C++11FAQ.html#for
    for( char& c : str ) c = std::tolower(c) ;
    return str ;
}

void sort( std::string county[], double tax_rate[], int num_counties )
{
   for( int i = 0 ; i < num_counties ; ++i )
   {
      for( int j = i+1 ; j < num_counties ; ++j )
      {
           if( to_lower( county[j] ) < to_lower( county[i] ) )
           {
                //Swap tax_rate
                {
                    const double temp = tax_rate[i];
                    tax_rate[i] = tax_rate[j];
                    tax_rate[j] = temp;
                }

                //Swap county
                {
                    const std::string temp = county[i];
                    county[i]= county[j];
                    county[j]= temp;
                }
           }
      }
   }
}

int main()
{
    const int num_counties = 6 ;
    std::string counties[num_counties] = { "cheshire", "berkshire", "norfolk", "SUSSEX", "CORNWALL", "somerset" } ;
    double tax_rates[num_counties] =      {   1.2,        3.4,         5.6,      7.8,      9.1,        0.1 } ;

    sort( counties, tax_rates, num_counties ) ;

    for( int i = 0 ; i < num_counties ; ++i ) std::cout << std::setw(10) << counties[i] << ' ' << tax_rates[i] << '\n' ;
}

http://coliru.stacked-crooked.com/a/fb191334f6e914cf
JLBorges, that fixes my lower/upper case problem. :)
The only thing I have left that i'm still trying to get is to use array of structures.
The point about creating structs to contain all of the county information is so you don't need 2 arrays.

Replace:
1
2
void sort(string county[], double tax_rate[]);
void display(string county[], double tax_rate[]);

with:
1
2
void sort(County county[]);
void display(County county[]);


Your comparison from your first post would be:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void sort(COUNTY countyList[])
{
   for (int i=0; i<COUNTY_MAX; i++)
   {
      for (int j=0; j<COUNTY_MAX; j++)
      {
           if (countyList[i].county < countyList[j].county)
           {
                //Swap tax_rate
                County temp = countyList[i];
                countyList[i] = countyList[j];
                countyList[j] = temp;
           }
      }
   }
}


In your second post, if you replace lines 17 and 18 with
Count countyList[COUNTY_MAX];
then lines 34 - 38 of your second post would be:

1
2
3
4
5
     while (input >> countyList[index].county)
     {
          input >> countyList[index].tax_rate;
          index++;
     }


You can probably figure out the rest from there.
Wouldn't countyList be County because I name my County structure 'County'?
You have so much leftover partially implemented stuff in your code that I decided not to try to reuse all your names. For instance, you have a struct named County and an object of that type also named County. That is a name collision and won't compile. Also you have an array of strings named county and a field within your struct named county. That will compile, but trying to differentiate which one I'm talking about would be miserable. I picked a new name countyList so that you would know what I'm talking about. You can name your array anything you want (as long as it compiles). Just be careful reusing names to mean different things. Sometimes it makes sense, but sometimes it just confuses things. For instance, I would not declare an array of County objects with the name "county" when the County struct contains a field also named "county".

Notice that you tried to create a single County element named County while I created an array of County elements called countyList. Remember, you are trying to replace 2 parallel arrays with an array of structures. Declaring a single struct to replace the 2 arrays won't work. It needs to be an array of structs like I show in my post.
Omg, that really makes a lot of sense. Thanks for pointing that out.

Nvm, i found the problem. I comment out the declaration.

So my problem now is that when it sorts, it only shows the tax percentage on the left and the right side shows a bunch of zeros.

I edit my code above to show what it is like now.
Last edited on
Where do you set the tax percentage to something other than 0? I think it is 0 because you never change it.
Topic archived. No new replies allowed.