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 The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1
  • The Helper The Helper:
    New recipe is another summer dessert Berry and Peach Cheesecake - https://www.thehelper.net/threads/recipe-berry-and-peach-cheesecake.194169/

      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