Solved

TallBill

Lifer
Apr 29, 2001
46,017
62
91
Ok, I just mixed up my vb.net array naming conventions with the c++ naming conventions. Thanks!
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
We need to see how FirstName, LastName, hours, and pay are declared and initialized. And just to be clear, you are seeing what error precisely?
 

SunnyD

Belgian Waffler
Jan 2, 2001
32,675
146
106
www.neftastic.com
Err... out of curiosity, what is counter initialized to?

And just out of aesthetically syntactic preference, why aren't you using a for loop which all but guarantees counter would be initialized properly?

Using arrays... since array subscripts start at 0, I would think "while(counter != 5)" should more appropriately be "while(counter < 5)" - otherwise what happens if counter is initialized to 6 before hitting the loop? It still runs... indefinitely... because 6 != 5, and 7 != 5, etc... and more than likely you'll be out of your array bounds correct? For loops are much more "appropriate" to counter controlled loops...

for(counter = 0; counter < 5; counter++)
{
...
}

It handles initializing, conditional and incrementing all in one nice easy to read place.

Other than that, what degibson asks is valid too. If you're initializing the arrays too small, mayhaps you confused the 1-indexed array SIZE with the 0-indexed array OPERATOR? Perhaps you're initializing type FirstName[3] instead of [4]? Dunno, too much left out to make that assumption. But in any event, you get the idea.
 

TallBill

Lifer
Apr 29, 2001
46,017
62
91
string FirstName[4], LastName[4];
double pay[4], hours[4], TotalSalary = 0;
int counter;
ifstream inFile;
ofstream outFile;

Counter was initialized to zero, but I do that where SunnyD says now, and yes the text file has 10 names in it.

I set up the loop the way you put as well SunnyD and same errors.

"Unhandled exception at 0x......... Access violation"

then

"No symbols are loaded for any call stack frame... "

and it disassembles and goes to push esi, whatever that is.


My teacher basically said use whatever compiler you want, here is a program, write it.
 

SunnyD

Belgian Waffler
Jan 2, 2001
32,675
146
106
www.neftastic.com
Scratch that... update coming...

You say:

string FirstName[4], LastName[4];
double pay[4], hours[4], TotalSalary = 0;

And also (before your change...)

while (counter != 5)

There's your problem... alluded to earlier.

while(counter != 5) (or < 5 if you've changed it)... the subscripts are mixed up.

When you DECLARE an array, you use 1's index... ie:

string FirstName[4] means declare an array of 4 string type variables.

When you USE an array, you use 0's index... ie:

FristName[0] is element #1
FirstName[1] is element #2
FirstName[2] is element #3
FirstName[3] is element #4

where index (n - 1) is element (n).

Your conditional, you're testing against the index, which means you need to think in terms in (n - 1) rather than (n). Hence, if counter < 5 or whatnot, that means your loop will execute when counter == 4. But because an index of 4 is out of bounds for your arrays (yes, your arrays are of length 4, but the highest index is length - 1, which means 3), the final iteration of the loop will try to access an invalid element of the array.

eg: FirstName[4] is element #5... there is no element number 5!!!

Solution, test if counter < length of array - 1, or initialize counter to 1 and offset all of your indexes to [counter - 1]. Again, two different ways of doing the same thing, it's all your preference.

As a side note: Access Violation usually means you're trying to access a memory location that wasn't intended, such as this - a memory location out of the bounds of a given array. The other main reason for this error is that you're attempting to access memory that is not initialized/allocated - perhaps memory that has been freed or a pointer that hasn't been initialized.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
The old-school name for this is "fencepost error," or "off by one." Probably the number one source of segfaults in some of the legacy C code I have to work on.
 

TallBill

Lifer
Apr 29, 2001
46,017
62
91
Ok, something pops up at school about a keyserver. Apparently my school software blocks my program being run.. That's highly inconvenient.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
VC Express is free

You can develop pure C or C++ code with no problems using it.
 

TallBill

Lifer
Apr 29, 2001
46,017
62
91
Something about a license server pops up even when I try to execute on another compiler. I can debug fine, but can't run it.
 

sourceninja

Diamond Member
Mar 8, 2005
8,805
65
91
I wish people wouldn't remove their questions. Makes it harder for people in the future to find the answers and makes me wonder what the question was.