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.
  • Varine Varine:
    How can you tell the difference between real traffic and indexing or AI generation bots?
  • The Helper The Helper:
    The bots will show up as users online in the forum software but they do not show up in my stats tracking. I am sure there are bots in the stats but the way alot of the bots treat the site do not show up on the stats
  • Varine Varine:
    I want to build a filtration system for my 3d printer, and that shit is so much more complicated than I thought it would be
  • Varine Varine:
    Apparently ABS emits styrene particulates which can be like .2 micrometers, which idk if the VOC detectors I have can even catch that
  • Varine Varine:
    Anyway I need to get some of those sensors and two air pressure sensors installed before an after the filters, which I need to figure out how to calculate the necessary pressure for and I have yet to find anything that tells me how to actually do that, just the cfm ratings
  • Varine Varine:
    And then I have to set up an arduino board to read those sensors, which I also don't know very much about but I have a whole bunch of crash course things for that
  • Varine Varine:
    These sensors are also a lot more than I thought they would be. Like 5 to 10 each, idk why but I assumed they would be like 2 dollars
  • Varine Varine:
    Another issue I'm learning is that a lot of the air quality sensors don't work at very high ambient temperatures. I'm planning on heating this enclosure to like 60C or so, and that's the upper limit of their functionality
  • Varine Varine:
    Although I don't know if I need to actually actively heat it or just let the plate and hotend bring the ambient temp to whatever it will, but even then I need to figure out an exfiltration for hot air. I think I kind of know what to do but it's still fucking confusing
  • The Helper The Helper:
    Maybe you could find some of that information from AC tech - like how they detect freon and such
  • Varine Varine:
    That's mostly what I've been looking at
  • Varine Varine:
    I don't think I'm dealing with quite the same pressures though, at the very least its a significantly smaller system. For the time being I'm just going to put together a quick scrubby box though and hope it works good enough to not make my house toxic
  • Varine Varine:
    I mean I don't use this enough to pose any significant danger I don't think, but I would still rather not be throwing styrene all over the air
  • The Helper The Helper:
    New dessert added to recipes Southern Pecan Praline Cake https://www.thehelper.net/threads/recipe-southern-pecan-praline-cake.193555/
  • The Helper The Helper:
    Another bot invasion 493 members online most of them bots that do not show up on stats
  • Varine Varine:
    I'm looking at a solid 378 guests, but 3 members. Of which two are me and VSNES. The third is unlisted, which makes me think its a ghost.
    +1
  • The Helper The Helper:
    Some members choose invisibility mode
    +1
  • The Helper The Helper:
    I bitch about Xenforo sometimes but it really is full featured you just have to really know what you are doing to get the most out of it.
  • The Helper The Helper:
    It is just not easy to fix styles and customize but it definitely can be done
  • The Helper The Helper:
    I do know this - xenforo dropped the ball by not keeping the vbulletin reputation comments as a feature. The loss of the Reputation comments data when we switched to Xenforo really was the death knell for the site when it came to all the users that left. I know I missed it so much and I got way less interested in the site when that feature was gone and I run the site.
  • Blackveiled Blackveiled:
    People love rep, lol
    +1
  • The Helper The Helper:
    The recipe today is Sloppy Joe Casserole - one of my faves LOL https://www.thehelper.net/threads/sloppy-joe-casserole-with-manwich.193585/
  • The Helper The Helper:
    Decided to put up a healthier type recipe to mix it up - Honey Garlic Shrimp Stir-Fry https://www.thehelper.net/threads/recipe-honey-garlic-shrimp-stir-fry.193595/

      The Helper Discord

      Staff online

      • Ghan
        Administrator - Servers are fun

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top