c++ simple program giving strange errors...

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

s3rius

Linux is only free if your time is worthless.
Reaction score
130
Maybe it's a caching issue since you're working on half a dozen files at once (just a wild guess).

Try setting this for each of your ofstreams:

Code:
    x.flags( ios::unitbuf );

That'll turn off chaching and force the program to write to the file immediately.

One way or another, if you are allowed to you should change your code so that it reads/writes to just a single file. File I/O is expensive.
 

Sgqvur

FullOfUltimateTruthsAndEt ernalPrinciples, i.e shi
Reaction score
62
>I have no clue why...

Well, you could try to use those programs called you know... debuggers. I've personally never had the need to use one (not because I am a guru at programming, no... because I've never written anything "usefull/substantial", I've only been writting little scripts from time to time =), but anyway...). I think a good place to start might be this tutorial for the gdb.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top