array of struct objects question

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 3044 Views - Last Post: 22 July 2010 - 01:17 PM Rate Topic: -----

#1 avalanchez71  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 15-June 10

array of struct objects question

Posted 21 July 2010 - 08:22 AM

Hello,
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

Is This A Good Question/Topic? 0
  • +

Replies To: array of struct objects question

#2 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: array of struct objects question

Posted 21 July 2010 - 08:39 AM

When referring to errors, you should always copy paste them verbatim, in their entirety. Not quote part of it or rephrase. That said, this time the problem is clear from your description.

Start with the error message. What do you not understand about it?
Was This Post Helpful? 1
  • +
  • -

#3 avalanchez71  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 15-June 10

Re: array of struct objects question

Posted 21 July 2010 - 08:50 AM

ok i will do that from now on.

I dont really understand the error, i get that it is having problems with the CustomerType, but it sounds like it is saying it cant convert a struct to a char, right?
Was This Post Helpful? 0
  • +
  • -

#4 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 965
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: array of struct objects question

Posted 21 July 2010 - 08:55 AM

int result = stricmp(customer[k], customer[k+1]);


First of all, you must know what parameter types does stricmp want. Well, it wants two const char *. What parameter types did you pass to your function? Well, since customer is an array of CustomerType, and CustomerType is a struct, then customer[k] and customer[k+1] are structs. Does stricmp wants structs? No. It wants const char *.

This post has been edited by sarmanu: 21 July 2010 - 08:56 AM

Was This Post Helpful? 1
  • +
  • -

#5 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: array of struct objects question

Posted 21 July 2010 - 08:58 AM

Quote

but it sounds like it is saying it cant convert a struct to a char, right?
Yes.

So what now? The ball is in your court. What are you trying to look at next?
Was This Post Helpful? 1
  • +
  • -

#6 avalanchez71  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 15-June 10

Re: array of struct objects question

Posted 21 July 2010 - 09:30 AM

View PostOler1s, on 21 July 2010 - 07:58 AM, said:

Quote

but it sounds like it is saying it cant convert a struct to a char, right?
Yes.

So what now? The ball is in your court. What are you trying to look at next?


i dont know, do i need to some how covert the struct to a char?
Was This Post Helpful? 0
  • +
  • -

#7 thecritic  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 12
  • Joined: 16-July 10

Re: array of struct objects question

Posted 21 July 2010 - 09:48 AM

View Postavalanchez71, on 21 July 2010 - 08:30 AM, said:

View PostOler1s, on 21 July 2010 - 07:58 AM, said:

Quote

but it sounds like it is saying it cant convert a struct to a char, right?
Yes.

So what now? The ball is in your court. What are you trying to look at next?


i dont know, do i need to some how covert the struct to a char?

Yes, you need to. And there is a very simple way.
Am I allowed to tell? (I ask this because, every body here seems to be deliberately, concealing the answer)
Was This Post Helpful? 1
  • +
  • -

#8 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2246
  • View blog
  • Posts: 9,236
  • Joined: 18-February 07

Re: array of struct objects question

Posted 21 July 2010 - 09:55 AM

Some people are more didactic than others. But if you know the solution there is no rule against pointing it out. But the OP will probably learn more if they figure it out themselves.

You might just hint that the function name is "sortByLastName" not "sortByCustomer" i.e. comparing two consomers would not make sense in this case... but comparing one of the fields of CustomerType would....
Was This Post Helpful? 1
  • +
  • -

#9 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: array of struct objects question

Posted 21 July 2010 - 10:01 AM

Quote

Am I allowed to tell? (I ask this because, every body here seems to be deliberately, concealing the answer)
You're not going to be docked or anything for giving away the answer, but our objective is to teach how to solve problems. Giving away the answer isn't very instructive.

Quote

do i need to some how covert the struct to a char?
Maybe. Also, for clarity, it's not a char type, but constant pointer to char.

Here's a question for you. On line 142 in your code: int result = stricmp(customer[k], customer[k+1]);
, what is the purpose of this code? Forget about stricmp, and the syntax. What are you trying to do?
Was This Post Helpful? 1
  • +
  • -

#10 avalanchez71  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 15-June 10

Re: array of struct objects question

Posted 21 July 2010 - 10:11 AM

View PostOler1s, on 21 July 2010 - 09:01 AM, said:

Quote

Am I allowed to tell? (I ask this because, every body here seems to be deliberately, concealing the answer)
You're not going to be docked or anything for giving away the answer, but our objective is to teach how to solve problems. Giving away the answer isn't very instructive.

Quote

do i need to some how covert the struct to a char?
Maybe. Also, for clarity, it's not a char type, but constant pointer to char.

Here's a question for you. On line 142 in your code: int result = stricmp(customer[k], customer[k+1]);
, what is the purpose of this code? Forget about stricmp, and the syntax. What are you trying to do?


I am trying to compare the data in one element of the array to the next element in the same array

oh shoot, am i suppose to add the "type" to the struct?
like,
customer[i].lastName


Was This Post Helpful? 0
  • +
  • -

#11 avalanchez71  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 15-June 10

Re: array of struct objects question

Posted 21 July 2010 - 10:21 AM

View Postavalanchez71, on 21 July 2010 - 09:11 AM, said:

View PostOler1s, on 21 July 2010 - 09:01 AM, said:

Quote

Am I allowed to tell? (I ask this because, every body here seems to be deliberately, concealing the answer)
You're not going to be docked or anything for giving away the answer, but our objective is to teach how to solve problems. Giving away the answer isn't very instructive.

Quote

do i need to some how covert the struct to a char?
Maybe. Also, for clarity, it's not a char type, but constant pointer to char.

Here's a question for you. On line 142 in your code: int result = stricmp(customer[k], customer[k+1]);
, what is the purpose of this code? Forget about stricmp, and the syntax. What are you trying to do?


I am trying to compare the data in one element of the array to the next element in the same array

oh shoot, am i suppose to add the "type" to the struct?
like,
customer[i].lastName




ok i tried that and it got rid of the error i posted on but i get a warning that states
1>c:\documents and settings\littletop1\my documents\visual studio 2008\projects\pj902_m\pj902_m\pj902_m.cpp(153) : warning C4996: 'stricmp': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _stricmp. See online help for details.
1>        c:\program files\microsoft visual studio 9.0\vc\include\string.h(215) : see declaration of 'stricmp'


is that built in to the stricmp function?
Was This Post Helpful? 0
  • +
  • -

#12 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: array of struct objects question

Posted 21 July 2010 - 10:26 AM

Quote

Instead, use the ISO C++ conformant name: _stricmp. See online help for details.
The solution is right there in the message...

Quote

is that built in to the stricmp function?
I expect there's a preprocessor pragma that instructs the compiler to emit this warning.

EDIT: I see that it's marked as _CRT_NONSTDC_DEPRECATE(_stricmp) in the string.h header file, so either there's additional preprocessor code or the compiler directly understands that and emits the appropriate warning.

This post has been edited by Oler1s: 21 July 2010 - 10:30 AM

Was This Post Helpful? 1
  • +
  • -

#13 avalanchez71  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 15-June 10

Re: array of struct objects question

Posted 21 July 2010 - 10:42 AM

View PostOler1s, on 21 July 2010 - 09:26 AM, said:

Quote

Instead, use the ISO C++ conformant name: _stricmp. See online help for details.
The solution is right there in the message...

Quote

is that built in to the stricmp function?
I expect there's a preprocessor pragma that instructs the compiler to emit this warning.

EDIT: I see that it's marked as _CRT_NONSTDC_DEPRECATE(_stricmp) in the string.h header file, so either there's additional preprocessor code or the compiler directly understands that and emits the appropriate warning.


ok that worked, i had received that error before and tried adding the underscore before and got more errors:)
i was a little nervous to try it here but it worked. I going to keep hammering at it and see if i can finish this beast now. Thank you all for your help.
Was This Post Helpful? 0
  • +
  • -

#14 avalanchez71  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 15-June 10

Re: array of struct objects question

Posted 22 July 2010 - 06:32 AM

well i hope some people are still reading this, i thought i would have been able to work the rest out on my own but now i am having problems doing the sort. I had wrote a program before that sorted alphabeticlly before but from input from 2 different files. So if i have two different items say item A and B. I would compare A to B and if A was larger, per the stricmp function, and having something like below,
char swap;
swap = customerA;
customerA = customerB;
customerB = swap;



so this would take the larger A and put the smaller B in front of it so it would be in order of B and A. However its not working in this case, i think it has something to do with this being an array struct but i am not sure, and on top of that i dont think it would even completely work in this case, lets say i have A, B, and C. they are in the order of B, C, A. When it would compare B and C it would see that B is smaller and leave B first, then it would compare C and A and see that A is smaller and try to run my code above(if it worked) it would swap C and A to A and C. Then it all would be in the order of B, A, C, which still isn't 100% right.
Here is my new code so far,


//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);

	sortByLastName(customer, countCust);

	cout << 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;
	char temp;
	while(k < countCust-1)
	{
		int result = _stricmp(customer[k].lastName, customer[k+1].lastName);
			if(result < 0)
			{
				cout << setw(7) << customer[k].firstName << "         " << left << setw(16) << customer[k].lastName << setw(25) << customer[k].streetAddress << customer[k].city << setw(10) << customer[k].state << customer[k].zipCode << endl;

			}
			else if(result > 0)
			{
				char swap;
				swap = *customer[k].lastName;
				*customer[k].lastName = *customer[k+1].lastName;
				*customer[k+1].lastName = swap;
				cout << setw(7) << customer[k].firstName << "         " << left << setw(16) << customer[k].lastName << setw(25) << customer[k].streetAddress << customer[k].city << setw(10) << customer[k].state << customer[k].zipCode << endl;
			}

		k++;
	}
}



Does anyone have any ideas on what i can do?

thanks

This post has been edited by avalanchez71: 22 July 2010 - 06:39 AM

Was This Post Helpful? 0
  • +
  • -

#15 avalanchez71  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 15-June 10

Re: array of struct objects question

Posted 22 July 2010 - 09:18 AM

Is anyone on today?
I have made some headway since my last post. I have got the sort by alphabet to work somewhat with the following function code,
void sortByLastName(CustomerType customer[], int &countCust)
{
	int k, j;
	char temp;
	char temp2;

	for(k = 0; k < countCust; k++)
	{
		for(j = k+1; j < countCust-1; j++)
		{
			int result = _stricmp(customer[k].lastName, customer[j].lastName);

			if(result > 0)
			{
				temp = *customer[k].lastName;
				*customer[k].lastName = *customer[j].lastName;
				*customer[j].lastName = temp;
			}
		}
	}

	int h = 0;
	while(h < countCust-1)
	{
		cout << setw(7) << customer[h].firstName << "         " << left << setw(16) << customer[h].lastName << setw(25) << customer[h].streetAddress << customer[h].city << setw(10) << customer[h].state << customer[h].zipCode << endl;
		h++;
	}

}



everything else in the program hasn't changed from earlier posts.
The problem now is for some reason when it sorts it is only sorting the first letter of the name and not the whole name. It does sort the first letters correctly but it is not taking the whole name.
Anyone have an idea why?

thanks
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2