1

I almost have it working however, it is seperating the last names that start in caps and the ones that do not.

Example file

LastName FirstName DaysofRental BalanceDue

Smith Joe 15 100.50

Doe John 10 95.20

Anderson Paul 30 20.00

O'Donell Miriam 10 24.30

Foster Sam 30 15.00

Zom Pete 10 20.00

Mock Chilly 100 30

smitty Chris 200 200

xu Conor 1 200

anilo steve 0 0

What "Sorted" file is outputing

LastName FirstName DaysofRental BalanceDue

Anderson Paul 30 $20.00

Doe John 10 $95.20

Foster Sam 30 $15.00

Mock Chilly 100 $30.00

O'Donell Miriam 10 $24.30

Smith Joe 15 $100.50

Zom Pete 10 $20.00

anilo steve 0 $0.00
smitty Chris 200 $200.00
xu Conor 1 $200.00

#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <fstream>
#include <string.h>  
using namespace std;
const int STRINGSIZE = 30;
const int LISTSIZE  = 10;
const int HEADSIZE = 4;

typedef char STRING30[STRINGSIZE];
typedef STRING30 NAMES[LISTSIZE];

int main(int argc, char** argv)
{
ofstream outfile;
outfile.open("sorted.txt");

int count, 
    count2,
    mindex;

int rdays[LISTSIZE],
    hrdays;

double baldue[LISTSIZE],
       totalbaldue, 
       hbaldue, 
       tempnum;

NAMES first, 
      last, 
      header;

STRING30 mname, 
         tempname;  

ifstream in;
in.open("invoice1_test1.txt");

//Input Section

if(in.is_open())
{
    in >> header[0]
       >> header[1] 
       >> header[2] 
       >> header[3]; 

    int count = 0;

    while(!in.eof())
    {
        in >> last [count] 
           >> first[count]
           >> rdays[count]
           >> baldue[count];
        count++;
    }
    in.close();     
}

else 

{
    cout << "File failed to open" << endl;
}

    for(count = 0; count < LISTSIZE; count++)
{
    mindex = count;
    strcpy(mname, last[count]);
    for(count2 = count; count2 < LISTSIZE; count2++)
    {
        if(strcmp(last[count2], mname) == -1)
        {
            mindex = count2;
            strcpy(mname, last[count2]);
        }
    }

    strcpy(tempname, last[count]);
    strcpy(last[count], mname);
    strcpy(last[mindex], tempname);

    strcpy(tempname, first[count]);
    strcpy(first[count], first[mindex]);
    strcpy(first[mindex], tempname);

    tempnum = rdays[count];
    rdays[count]= rdays[mindex];
    rdays[mindex]= tempnum;

    tempnum = baldue[count];
    baldue[count] = baldue[mindex];
    baldue[mindex] = tempnum;
}
outfile << setiosflags(ios::showpoint | ios::fixed) << setprecision(2);

outfile << left << setw(20) << header[0] << setw(20) << header[1] << setw(20) << header[2] << setw(20) << header[3] << endl;

for(int count = 0; count<LISTSIZE; count++)
{
    outfile << left << setw(20) << last[count] << setw(20) << first[count] << setw(20) << rdays[count] << "$" << setw(20) << baldue[count] << endl;
}
Latusken
  • 17
  • 3
  • 1
    This is C++. Why are you sticking to `strcpy` and `strcmp` instead of `std::string`? Why are you not using `std::sort` or `std::stable_sort`? If this is homework, what other restrictions are you operating under? – ShadowRanger May 05 '18 at 02:18
  • Professor wants it to be done using string compare and string copy, I wouldn't know how to answer what other restrictions. But we can only use the string functions we learned in class. – Latusken May 05 '18 at 02:22
  • And that's how you teach ugly C/C++ mix... At least can we warn you against using [`while(!in.eof())`](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) and [`using namespace std;`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)? – O'Neil May 05 '18 at 02:41
  • He's already warned us about `using namespace std; ` but since it's a beginner course I think he did it to make it easier on us – Latusken May 05 '18 at 02:43

2 Answers2

2

Use if (strcmpi(last[count2], mname) < 0); for the comparison, instead of if (strcmp(last[count2], mname) ==-1);

strcmpi() functions same as strcmp() but it is not case sensitive.

Also add exit(1) if ifstream in fails to open.

seccpur
  • 4,680
  • 2
  • 9
  • 17
0

Convert all names to uppercase before storing.

John Perry
  • 2,283
  • 1
  • 15
  • 25
  • I would not know how to do that. – Latusken May 05 '18 at 02:45
  • Look at [toupper()](http://en.cppreference.com/w/c/string/byte/toupper), which will convert one character at a time. Looking at your data, I think you need merely convert the first letter in the last name. Or, you could convert the entire array, but you'd also have to convert back. Depends on whether they want "erroneous" information kept that way. – John Perry May 05 '18 at 02:47