1 Replies - 398 Views - Last Post: 04 December 2012 - 02:14 PM Rate Topic: -----

#1 sammygirl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 10-October 12

Passing an array of structs into multiple functions

Posted 04 December 2012 - 01:41 PM

I have finished my homework and the output is perfect. I now want to break it up into functions. I am having trouble with how exactly I pass an array of structs. I think I only need to pass by value but an example of passing by reference would be helpful just in case.
/* =================================================================================== */
/* PROGRAM Computer Dating Service

AUTHOR:  Samantha Hildebrand
FSU MAIL NAME:  seh09
RECITATION SECTION NUMBER:  6
TA NAME:  Sean Rose
Cop 3014 - Fall 2012
PROJECT NUMBER:  6
PLATFORM:  Windows OS / MS Visual C++ Express 2012
DUE DATE:  Tuesday 12/4/2012

SUMMARY

This program matches clients of a dating service to their best opposite sex match. It
does this by reading in a clients gender, 10 answers, and their name. This information
is echoprinted.  This information is saved in 3 array of structs (Male, Female, and Void).
It then takes the answer information and finds the best match of the opposite sex by
taking the absolute value of their answers minus the potential females answer. Whichever
female has the lowest sum is the best match.  The program then prints out the best 
matches for males and females and then exits the program.

INPUT

The text file clients.txt.  Each line of the file will contain information on one client, 
in the following format:
Column 1:  a character indicating the client's sex, 'M' or 'F'
Column 2:  a blank
Columns 3 through 22:  the 10 answers, with a blank after each
Columns 23 through end of line:  client's name (starts in column 23)

This program holds up to 15 males and 15 females.  

BAD DATA CHECKING:  This program checks that gender is either 'M' or 'F'(if it is not
that client will not be included), that the answers are between 1 and 5 (if they are not,
the answer is changed to a 3), and that the name is not repeated in the file (if it is,
the repeated names will not be used). If any of the above occur in the program, an error
message is printed. The program also checks if the file is open.

OUTPUT

Consits of a 3 tables in the following formats:
     GENDER                ANSWERS                 NAME           
    _____________________________________________________________

Appropriate values will be printed under the column headings.  When there is an invalid
gender, answer, or name, an error message will be printed under the clients entry.

   ================================================================
		   <<< Most compatible dates for male clients >>>
		    Client name             Most compatible date
		    --------------------    --------------------

Appropriate values will be printed under the column headings.

   ================================================================
		  <<< Most compatible dates for female clients >>>
		    Client name             Most compatible date
		    --------------------    --------------------

Appropriate values will be printed under the column headings.

ASSUMPTIONS
1) There is at least one male and one female client stored in the file.
2) Each data line containsall data items in the correct position and of the correct data
type.

DATA STRUCTURES

This program reads the data from the file into 3 arrays of structs, one storing males,
one storing females, and one storing a void gender, in seperate data structures.  Inside
each client struct, there is an array member which stores the answers to the client's 
questions.  The typedef construct is used to set up data types for all arrays in this
program. After reading the data into the data structures, the program goes on to process
the information as required, utilizing those data structures.

*/

/* =================================================================================== */
/* GLOBAL DECLARATIONS */
/* =================================================================================== */

//INCLUDE FILES
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;

//GLOBAL NAMED CONSTANTS
const int ANSWERS = 10;
const int SIZE = 16;

// GLOBAL DATA TYPES
typedef int AnswerList [ANSWERS];

struct Client
{   char Gender;
	AnswerList Answers;
	string Name;
	bool genderFlag;
	bool answerFlag;
	bool nameFlag;
};
typedef Client ClientLibrary [SIZE];
// GLOBAL FUNCTION PROTOTYPES
void PrintHeading ();
void PrintMaleDates();
void PrintFemaleDates ();

/* =================================================================================== */
/* FUNCTION main */
/* =================================================================================== */

int main ()
{	ifstream inFile;				// the input data file
	ClientLibrary Male;				// the array of structs for male clients
	ClientLibrary Female;			// the array of structs for female clients
	ClientLibrary Invalid;			// the array of structs for the invalid gender
int		i,							// answer counter
		j,							// male counter
	    k,   // female counter
		l,   // invalid counter
	     F,
		 q,
		 currentScore,
		 bestScore;
	q=0;
	j=1;
	k=1;
	l=0;
	currentScore=0;
	bestScore=99;
   
	
	char gender;
	
	Female[k].nameFlag = false;
	Male[j].answerFlag = false;
	Female[k].answerFlag = false;
	Male[j].nameFlag = false;
	
	// print the opening output header
	PrintHeading ();
	
	[color="#FF0000"]// open the data file; exit if file does not open
	inFile.open("clients.txt");
	if (!inFile)
	{ 
		cout << endl << "ERROR - file did not open. Program terminated."
			 << endl;
		return (EXIT_FAILURE);
	}

	do
	{   i=0;
		inFile >> gender;
		
		if (gender =='M')
		{ Male[j].Gender ='M';
		  cout <<"   " << Male[j].Gender << "        ";	

			do 
			{ inFile >> Male[j].Answers[i];
				if (Male[j].Answers[i] < 1 || Male[j].Answers[i] > 5)
				{	Male[j].Answers[i] = 3;
					Male[j].answerFlag = true;
				}
				cout << Male[j].Answers[i] << "  ";
				i++;
			}
			while (i<10);
		
		getline (inFile, Male[j].Name); 

			for ( F=0; F<j; F++)
			{
				if (Male[j].Name == Male[F].Name)
				{Male[j].nameFlag = true;
				 cout << "       " << Male[j].Name << endl;
			     cout << " Duplicate - This Instance Not Included in Client List \n";}
		}//  end of for loop	

		if (Male[j].nameFlag == false)
		{cout << "       " << Male[j].Name << endl;
		j++;}
		Male[j].nameFlag = false;}

		else if  (gender == 'F')
			{	Female[k].Gender = 'F';
				cout << "   " << Female[k].Gender << "        ";
			do 
			{
			inFile >> Female[k].Answers[i];
			if (Female[k].Answers[i] < 1 || Female[k].Answers[i] > 5)
			{   Female[k].Answers[i] = 3;
				Female[k].answerFlag = true;
			}
			cout << Female[k].Answers[i] << "  ";
			i++;
			}
		while (i<10);
		getline (inFile, Female[k].Name);
		
		for ( F=0; F<k; F++)
			{
				if (Female[k].Name == Female[F].Name)
				{Female[k].nameFlag = true;
				 cout << "       " << Female[k].Name << endl;
			     cout << " Duplicate - This Instance Not Included in Client List \n";}
		}//  end of for loop	

		if (Female[k].nameFlag == false)
		{cout << "       " << Female[k].Name << endl;
		k++;}
		Female[k].nameFlag = false;}
else 
{Invalid[l].genderFlag = true;
	
	cout << "   " << gender << "        ";
			do 
			{
			inFile >> Invalid[l].Answers[i];
			if (Invalid[l].Answers[i] < 1 || Invalid[l].Answers[i] > 5)
			{   Invalid[l].Answers[i] = 3;
				Invalid[l].answerFlag = true;
			}
			cout << Invalid[l].Answers[i] << "  ";
			i++;
			}
		while (i<10);
		getline (inFile, Invalid[l].Name); 
		cout << "       " << Invalid[l].Name << endl
			 << "Invalid Gender Choice:  This instance will not be included in the client list" 
			 << endl;
		l++;}
		 [/color]
		
			
		if (Male[j-1].answerFlag == true)
		{cout << "At least one invalid answer changed to neutral answer " << endl;}
		if (Female[k-1].answerFlag == true)
		{cout << "At least one invalid answer changed to neutral answer" << endl;}
	    if (Invalid [l-1].answerFlag == true)
		{cout << "At least one invalid answer changed to neutral answer" << endl;}
	cout << "\n";
	

	}

	[color="#0000FF"]while (inFile);
	cout << "=======================================" << endl
		 <<"The number of male clients is :  " << j-1<< endl
	     << "The number of female clients is :  " << k-1 << endl;
		 PrintMaleDates();

for (int z=1; z < j; z++)
{ cout << left << setw(23) << Male[z].Name;
  for (int y=0; y < k; y++)
  { 
      for (int x = 0; x < 10; x++)
      { currentScore = currentScore + abs(Male[z].Answers[x] - Female[y].Answers[x]);}
  
     if (currentScore < bestScore)
	 { q=y;
	 bestScore = currentScore;
	 }
	 currentScore = 0;
  } 
  bestScore = 99;
  cout << left << setw(10) << Female[q].Name << endl;
};
q=0;
[/color]
[color="#9932CC"]PrintFemaleDates ();
for (int z=1; z < k; z++)
{ cout << left << setw(23) << Female[z].Name;
  for (int y=0; y < j; y++)
  { 
	  for (int x = 0; x < 10; x++)
      { currentScore = currentScore + abs(Female[z].Answers[x] - Male[y].Answers[x]);}
  
     if (currentScore < bestScore)
	 { q=y;
	 bestScore = currentScore;
	 }
	 currentScore = 0;
  } 
  bestScore = 99;
  cout << left << setw(10) << Male[q].Name << endl;[/color]
};
	return 0;
}

/* =================================================================================== */
/* FUNCTION PrintHeading */
/* =================================================================================== */
void PrintHeading ()
/*
This function prints out the initial heading for the programs output
*/

{	// PrintHeading
	// print output heading
	cout << "********************************************************************" << endl
		 << "*                                                                  *" << endl
		 << "* Welcome to the Cheap & Ineffective Dating Service of Tallahassee *" << endl
		 << "*                                                                  *" << endl
		 << "********************************************************************" << endl
		 << endl << endl
	     << " GENDER                ANSWERS                 NAME           "   << endl
         << " _____________________________________________________________"   << endl
		 << endl << endl;
}  // PrintHeading

/* =================================================================================== */
/* FUNCTION PrintMaleDates */
/* =================================================================================== */
void PrintMaleDates ()
{
	cout << "\n================================================================\n\n"
		 << "<<< Most compatible dates for male clients >>>\n\n\n"
		 << "Client name             Most compatible date\n"
		 << "--------------------    --------------------\n";
} //PrintMaleDates

/* =================================================================================== */
/* FUNCTION PrintFemaleDates */
/* =================================================================================== */
void PrintFemaleDates()
{
	cout << "\n================================================================\n\n"
		 << "<<< Most compatible dates for female clients >>>\n\n\n"
		 << "Client name             Most compatible date\n"
		 << "--------------------    --------------------\n";
} // PrintFemaleDates

/* =================================================================================== */
/* END of PROGRAM */
/* =================================================================================== */




Basically, I want to turn the reading from the file portion into a function, finding the male clients matches, and finding the female clients matches into functions.

Thank you!

Is This A Good Question/Topic? 0
  • +

Replies To: Passing an array of structs into multiple functions

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,596
  • Joined: 16-October 07

Re: Passing an array of structs into multiple functions

Posted 04 December 2012 - 02:14 PM

To be honest, the three different lists is something of a cluster fuck.

If you must have that, I'd start with something like this:
bool isDuplicate(ClientLibrary cl, int size);

void process(ClientLibrary cl, int &size, Client &client);

bool read(ifstream &, Client &client);

bool read(const char *filename, ClientLibrary Male, ClientLibrary Female, ClientLibrary Invalid, int &readMale, int &readFemale, int &readInvalid) {
	ifstream inFile;
	
	readMale = readFemale = readInvalid;
	
	inFile.open(filename);
	if (!inFile) { 
		cout << endl << "ERROR - file did not open. Program terminated."
			 << endl;
		return false;
	}  
	
	while (read(ifstream &inFile, Client &client)) {
		if (client.Gender = 'M') {
			process(Male, readMale, client);
		} else if (client.Gender = 'M') {
			process(Female, readFemale, client);
		} else {
			process(Invalid, readInvalid, client);
		}
	}
	inFile.close();
	return true;
}



I'd seriously consider just one list, though.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1