what i am trying to do in this program is read in data from an external input file into an array in a struct. Then output it to a external file, then sort alphabeticaly the last name of the people from the input file then append the sorted list to the external output file. I have everything working up to sorting, i was told to use stricmp() function to sort the names but i get an error that states "'stricmp' : cannont covert parameter 1 from 'CustomerType' to 'const char *'".
I am not sure how to get it to use stricmp(). Here is my code,
//List preprocessor directives
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cctype>
using namespace std;
//global variabls
const int MAX_CLIENT_SIZE = 100;
const int FIRST_NAME_LEN = 11;
const int LAST_NAME_LEN = 13;
const int ADDRESS_LEN = 25;
const int CITY_NAME_LEN = 16;
const int STATE_LEN = 3;
//struct
struct CustomerType
{
char lastName[LAST_NAME_LEN];
char firstName[FIRST_NAME_LEN];
char streetAddress[ADDRESS_LEN];
char city[CITY_NAME_LEN];
char state[STATE_LEN];
int zipCode;
};
//function prototypes
void reportHeading(ofstream& outfile);
int getInfo(CustomerType customer[], istream& infile, int &countCust);
void printCustomers(ofstream& outfile, CustomerType customer[], int &countCust);
void sortByLastName(CustomerType customer[], int &countCust);
int main()
{
int countCust;
CustomerType customer[MAX_CLIENT_SIZE];
//open input file
ifstream infile("PJ902_customers.txt");
//input file check
if (!infile)//unable to open file, display msg and quit
{
cout << "Error: cannot open PJ902_customers.txt file\n";
return 1;
}
//allows external file to be created
ofstream outfile("PJ902_report.txt");
//output file check
if(!outfile)
{
cout << "Error: cannot create output file\n";
return 1;
}
getInfo(customer, infile, countCust);
printCustomers(outfile, customer, countCust);
// guard against bad input data
if (getInfo(customer, infile, countCust) == -1) {
cerr << "Error: bad data in input file.\n\n";
}
else if(getInfo(customer, infile, countCust) == 1) {
cerr << "Error: array size exceeded\n\n";
}
return 0;
}
//function to ouput report headiing to report file
void reportHeading(ofstream& outfile)
{
outfile << "Customer Information Report,\n";
outfile << "reported by Josh\n" << endl << endl;
outfile << "First Name" << setw(15) << "Last Name" << setw(14) << "Address" << setw(22) << "City" << setw(15) << "State" << setw(12) << "Zipcode" << endl;
outfile << "----------" << setw(15) << "---------" << setw(28) << "---------------------" << setw(15) << "-----------" << setw(8) << "-----" << setw(12) << "-------" << endl;
}
int getInfo(CustomerType customer[], istream& infile, int &countCust)
{
int i = 0;
while(i < MAX_CLIENT_SIZE && infile)
{
infile >> ws;
infile >> customer[i].firstName;
infile >> ws;
infile >> customer[i].lastName;
infile >> ws;
infile.get(customer[i].streetAddress, sizeof(customer[i].streetAddress));
infile >> ws;
infile.get(customer[i].city, sizeof(customer[i].city));
infile >> ws;
infile >> customer[i].state;
infile >> ws;
infile >> customer[i].zipCode;
i++;
}
countCust = i;
// guard against bad input data
if (!infile.eof() && infile.fail()) {
return -1;
}
// guard against too much data for array size
if (i == MAX_CLIENT_SIZE && infile >> ws && infile.good ()) {
return 1;
}
return 0;
}
void printCustomers(ofstream& outfile, CustomerType customer[], int &countCust)
{
int j = 0;
while( j < countCust-1)
{
cout << setw(7) << customer[j].firstName << " " << left << setw(16) << customer[j].lastName << setw(25) << customer[j].streetAddress << customer[j].city << setw(10) << customer[j].state << customer[j].zipCode << endl;
//outifle << setw(7) << customer[j].firstName << " " << left << setw(16) << customer[j].lastName << setw(25) << customer[j].streetAddress << customer[j].city << setw(10) << customer[j].state << customer[j].zipCode << endl;
j++;
}
cout << endl;
}
void sortByLastName(CustomerType customer[], int &countCust)
{
int k = 0;
while(k < countCust)
{
int result = stricmp(customer[k], customer[k+1]);
if(result > 0)
{
cout << "greater";
}
else
{
cout << "less";
}
}
}
my function "sortByLastName isnt finished yet, i just put in a output to the screen of "greater" or "less" just to test if it was working. Anyone have any ideas what i am doing wrong?
Thanks

New Topic/Question
Reply




MultiQuote






|