Nestharus
o-o
- Reaction score
- 84
so... I wrote this in about 10 minutes for a class (very straightforward and simple), but it is giving me cannot write to memory errors for some unknown reason. I have no clue why... the include files are just char*[]
city.h or w/e
sooooooo anyways... the cannot write to memory error is 100% random. It occurs at different points while writing to the files : |. I have no idea what is causing this because it is being so random and because it happens with any of the files at any point... D:
I do plan on cleaning up some of the code at some point for the class (putting the switch statement in the loop into the random methods and adding a gender argument to those methods as well as adding enums for reno, seattle, male, and female). I also plan on putting the outputs into an ofstream array.
city.h or w/e
Code:
char * city_names [] =
{
"Reno",
"Seattle"
};
sooooooo anyways... the cannot write to memory error is 100% random. It occurs at different points while writing to the files : |. I have no idea what is causing this because it is being so random and because it happens with any of the files at any point... D:
I do plan on cleaning up some of the code at some point for the class (putting the switch statement in the loop into the random methods and adding a gender argument to those methods as well as adding enums for reno, seattle, male, and female). I also plan on putting the outputs into an ofstream array.
Code:
// Macros
#define TO_STR(INT) #INT
/* -----------------------------------------------------------------------------
FUNCTION NAME: generateEmployeeData
PURPOSE: generate emp data
RETURNS: nothing
NOTES: none
----------------------------------------------------------------------------- */
void generateEmployeeData(const char* arguments[]) {
int employeeCount = employeeDataMenu();
int males = employeeCount/3 + 1;
int females = employeeCount/3 + 1;
int reno = employeeCount/2;
const int ARG_LAST_NAME = 1;
const int ARG_FIRST_NAME = 2;
const int ARG_MIDDLE_INITIALS = 3;
const int ARG_GENDER = 4;
const int ARG_TITLE = 5;
const int ARG_CITY = 6;
const int ARG_STATE = 7;
const int MALE = 1;
const int FEMALE = 2;
const int RENO = 0;
const int SEATTLE = 1;
int gender;
ofstream lastNames;
ofstream firstNames;
ofstream middleInitials;
ofstream genders;
ofstream titles;
ofstream cities;
ofstream states;
lastNames.open(arguments[ARG_LAST_NAME]);
firstNames.open(arguments[ARG_FIRST_NAME]);
middleInitials.open(arguments[ARG_MIDDLE_INITIALS]);
genders.open(arguments[ARG_GENDER]);
titles.open(arguments[ARG_TITLE]);
cities.open(arguments[ARG_CITY]);
states.open(arguments[ARG_STATE]);
if (!lastNames.is_open() || !firstNames.is_open() || !middleInitials.is_open() || !genders.is_open() || !titles.is_open() || !cities.is_open() || !states.is_open()) {
closeFiles(&lastNames, &firstNames, &middleInitials, &genders, &titles, &cities, &states);
exit(EXIT_CODE___CANNOT_OPEN_OUTPUT_FILE);
} //if
clear();
while (0 < employeeCount) {
lastNames << getRandomLastName() << endl;
if (males > 0) {
firstNames << getRandomFirstNameMale() << endl;
genders << "M" << endl;
males--;
}//if
else if(females > 0) {
firstNames << getRandomFirstNameFemale() << endl;
genders << "F" << endl;
females--;
}//else if
else {
gender = getRandomGender();
switch (gender) {
case MALE:
firstNames << getRandomFirstNameMale() << endl;
genders << "M" << endl;
break;
case FEMALE:
firstNames << getRandomFirstNameFemale() << endl;
genders << "F" << endl;
break;
default:
closeFiles(&lastNames, &firstNames, &middleInitials, &genders, &titles, &cities, &states);
exit(EXIT_CODE___INVALID_GENDER);
break;
} //switch
} //if
middleInitials << getRandomMiddleInitial() << endl;
titles << getRandomJobTile() << endl;
if (0 < reno) {
cities << getCity(RENO) << endl;
states << getState(getCity(RENO)) << endl;
reno--;
}//if
else {
cities << getCity(SEATTLE) << endl;
states << getState(getCity(SEATTLE)) << endl;
}//else
//city, state
employeeCount--;
} //while
closeFiles(&lastNames, &firstNames, &middleInitials, &genders, &titles, &cities, &states);
} //void generateEmployeeData(int argumentCount, const char* arguments)
FUNCTION NAME: getRandomInt
PURPOSE: get random
RETURNS: random int
NOTES: none
----------------------------------------------------------------------------- */
int getRandomInt(int min, int max) {
return rand()%(max + 1) + min;
} //getRandomInt(int min, int max)
/* -----------------------------------------------------------------------------
FUNCTION NAME: getState
PURPOSE: gets state via city
RETURNS: char*
NOTES: none
----------------------------------------------------------------------------- */
char* getState(char* city) {
if (city == "Reno")
return "Nevada";
return "Washington";
} //getState(char* city)
/* -----------------------------------------------------------------------------
FUNCTION NAME: getRandomLastName
PURPOSE: random last name
RETURNS: char*
NOTES: none
----------------------------------------------------------------------------- */
char* getRandomLastName() {
#include "name_l.h";
return names_last[getRandomInt(0, sizeof(names_last)/sizeof(names_last[0])) - 1];
} //getLastName()
/* -----------------------------------------------------------------------------
FUNCTION NAME: getRandomFirstNameFemale
PURPOSE: random first name female
RETURNS: char*
NOTES: none
----------------------------------------------------------------------------- */
char* getRandomFirstNameFemale() {
#include "name_f_f.h";
return names_female[getRandomInt(0, sizeof(names_female)/sizeof(names_female[0])) - 1];
} //getFirstNameFemale()
/* -----------------------------------------------------------------------------
FUNCTION NAME: getRandomFirstNameMale
PURPOSE: random first name male
RETURNS: char*
NOTES: none
----------------------------------------------------------------------------- */
char* getRandomFirstNameMale() {
#include "name_f_m.h";
return names_male[getRandomInt(0, sizeof(names_male)/sizeof(names_male[0])) - 1];
} //getFirstNameMale()
/* -----------------------------------------------------------------------------
FUNCTION NAME: getRandomMiddleInitial
PURPOSE: get random middle initial
RETURNS: char*
NOTES: none
----------------------------------------------------------------------------- */
char* getRandomMiddleInitial() {
#include "name_mi.h";
return names_middle_initial[getRandomInt(0, sizeof(names_middle_initial)/sizeof(names_middle_initial[0])) - 1];
} //getMiddleInitial()
/* -----------------------------------------------------------------------------
FUNCTION NAME: getRandomGender
PURPOSE: get random gender
RETURNS: (int) 1==MALE, 2==FEMALE
NOTES: none
----------------------------------------------------------------------------- */
int getRandomGender() {
return getRandomInt(1, 2);
} //getGender()
/* -----------------------------------------------------------------------------
FUNCTION NAME: getRandomJobTile
PURPOSE: get random title
RETURNS: char*
NOTES: none
----------------------------------------------------------------------------- */
char* getRandomJobTile() {
#include "title.h";
return job_title[getRandomInt(0, sizeof(job_title)/sizeof(job_title[0])) - 1];
} //getJobTile()
/* -----------------------------------------------------------------------------
FUNCTION NAME: getCity
PURPOSE: get city via index
RETURNS: char*
NOTES: 1 == RENO, 2 == SEATTLE
----------------------------------------------------------------------------- */
char* getCity(int index) {
#include "city.h"
return city_names[index];
} //getCity(int index)