Execution

M

muser

for the following code:

strncpy(temp_issue, &temp1[13], 4);
files.rec1.issue_rec[4] = atol(temp_issue);
cout<<files.rec1.issue_rec[4]<<endl;

on execution I get the following.

0x0fd10
a memory address no doubt.

but when doing the following:


strncpy(temp_customer_code1, &temp1[2], 5);
files.rec1.customer_code[5] = '\0';
files.rec1.customer_code[5] = atol(temp_customer_code1);
cout<< files.rec1.customer_code[5] <<endl;


I get a sensible

98581.

Have I gone out of bounds at all. Anyone who thinks they can solve
this mystery please respond. Real solutions please, as Bull****
doesn't get me anywhere.
 
V

Victor Bazarov

muser said:
for the following code:

strncpy(temp_issue, &temp1[13], 4);
files.rec1.issue_rec[4] = atol(temp_issue);
cout<<files.rec1.issue_rec[4]<<endl;

on execution I get the following.

0x0fd10
a memory address no doubt.

Really? For me that code doesn't compile. Perhaps you forgot
to mention that it's just a fragment. Neither of used symbols
is declared. So, I have all doubts in the world.

BTW, 0x0fd10 is 64784 decimal. Seems sensible enough.
but when doing the following:


strncpy(temp_customer_code1, &temp1[2], 5);
files.rec1.customer_code[5] = '\0';
files.rec1.customer_code[5] = atol(temp_customer_code1);
cout<< files.rec1.customer_code[5] <<endl;


I get a sensible

98581.

"Sensible"? And if it were printed as '0x018115' (the same
value, only in hex)?
Have I gone out of bounds at all. Anyone who thinks they can solve
this mystery please respond. Real solutions please, as Bull****
doesn't get me anywhere.

Real solutions to what? Two fragments of some code, no class
definitions, no function definitions... How do you expect us
to help you? If that's a continuation of some earlier discussion
you had with the group, why did you post a new message instead of
continuing in the same thread?

Post complete code. Read FAQ 5.8.

Victor
 
R

Rolf Magnus

muser said:
Have I gone out of bounds at all. Anyone who thinks they can solve
this mystery please respond. Real solutions please, as Bull****
doesn't get me anywhere.

Ask real questions please, as Bull**** doesn't get you anywhere.
 
M

muser

Dear John Harrison,

Because of the nature of my studying C++ (from home, without a tutor)
I must gain what little extra information I do get from newsgroups.
Some of it has lead me in directions I wouldn't have thought of
before. When I keep posting information although it may look tedious
as it refers to the same program and it may seem like I'm chasing my
own tail, though I have nothing to go on except what my tutor or a
person from the newsgroups points out to me.
I have several tutors, one actually told me to use a single character
array, another told me to use a multi dimensional array and then a
third told me use a sinlge character array! The program does actually
read the document I want it to read, but I want to see exactly what it
is reading and if that information is correct, so I have added a cout
statement to view exactly that.
The program has changed again, but I can't see where I have gone out
of bounds with my arrays. To answer your question, yes i do require
arrays because I'm reading a document from a file into an array, on
some of the entries such as customer address I have to make sure
rubbish isn't entered as later I have to seperate the rubbish from the
actual valid material.
For the first 4 entries i must make sure that they are numerical
digits.
If anyone can actually copy and paste my program into their own
version of C++ and execute it the problem will become apparent
immediately.
Thank you for your support.
















John Harrison said:
John Harrison said:
muser said:
for the following code:

strncpy(temp_issue, &temp1[13], 4);
files.rec1.issue_rec[4] = atol(temp_issue);
cout<<files.rec1.issue_rec[4]<<endl;

on execution I get the following.

0x0fd10
a memory address no doubt.

but when doing the following:


strncpy(temp_customer_code1, &temp1[2], 5);
files.rec1.customer_code[5] = '\0';
files.rec1.customer_code[5] = atol(temp_customer_code1);
cout<< files.rec1.customer_code[5] <<endl;


I get a sensible

98581.

Have I gone out of bounds at all.

If your data structures are the same as in your post of June 2003, then you
have gone out of bounds. I'm still not convinced that customer_code and
issue_rec need to be arrays at all. If you don't agree, then explain why. If
you don't understand, then ask. At the moment I don't think you are helping
yourself.

john
 
V

Victor Bazarov

muser said:
[...]
For the first 4 entries i must make sure that they are numerical
digits. [...]

"numerical digits" sitting one after another in a stream are
a number. If the stream contains '1' then '2' then '3', it
simply contains "123", which is a _single_ number. You may
read it into a _single_ variable, declared as 'int number;'
and you don't need to read it into an array. Do you understand
that?

You may also read those "numerical digits" into an array. But
it has to be an array of 'char', not an array of 'int'. Do you
understand that? Once those "digits" have been read into an
array of 'char', you can verify that all of them are decimal
digits by using 'isdigit' function (on each of them).

I can imagine that the ability to read "123" into either a single
value or into an array confuses the hell out of you. Try to see
the difference. The value is 'int', the array if of 'char'.
There is NO NEED to read them into an array of _int_. Besides,
it's not easy to read them into an array of int.

Victor
 
J

John Harrison

muser said:
Dear John Harrison,

Because of the nature of my studying C++ (from home, without a tutor)
I must gain what little extra information I do get from newsgroups.
Some of it has lead me in directions I wouldn't have thought of
before. When I keep posting information although it may look tedious
as it refers to the same program and it may seem like I'm chasing my
own tail, though I have nothing to go on except what my tutor or a
person from the newsgroups points out to me.
I have several tutors, one actually told me to use a single character
array, another told me to use a multi dimensional array and then a
third told me use a sinlge character array!

I expect all you tutors were right, but all were answering different
questions, even though you thought you were asking the same question.
Misunderstanding in other words.
The program does actually
read the document I want it to read, but I want to see exactly what it
is reading and if that information is correct, so I have added a cout
statement to view exactly that.
The program has changed again, but I can't see where I have gone out
of bounds with my arrays. To answer your question, yes i do require
arrays because I'm reading a document from a file into an array, on
some of the entries such as customer address I have to make sure
rubbish isn't entered as later I have to seperate the rubbish from the
actual valid material.

You must distinguish two cases. You want to read a number from a file, which
is 6 digits say, for that you need a char array, fine. But you then convert
that to a number, a long say. At that point you have a *single* number not
an array of characters. That is what is concerning me about your code. So
for instance

strncpy(temp_issue, &temp1[13], 4);
files.rec1.issue_rec = atol(temp_issue);
cout<<files.rec1.issue_rec<<endl;

temp1 is an array of chars, temp_issue is an array of chars, *but* issue_rec
should be a *single* long, not an array of longs.

For the first 4 entries i must make sure that they are numerical
digits.
If anyone can actually copy and paste my program into their own
version of C++ and execute it the problem will become apparent
immediately.
Thank you for your support.

I'd be perfectly willing to do that, but I don't have the code.

john
 
K

Karl Heinz Buchegger

muser said:
Victor there is no need to be pedantic,

Yes there is!
programming is all about beeing pedantic!

I realise I could forfeit the
use of arrays and just have a char variable hold the numerical digits,
but these numerical digits require validation as they are part of an
address, i.e. struct records{


first of all (and looking at your program) you need to ditinguish between
* how is the data represented at the file
* how is the data represented in memory
struct file1 {
char record_type;
char customer_name[21];
long customer_code[5];

Here are you sure, that one customer has
5 codes, each one beeing a number

No!

A customer has only one code. At the file level
this code consists of 5 digits (=5 characters),
but once those 5 digits (5 charcaters have been)
read and verified, those 5 digits form a *single*
number:

long customer_code;
char customer_address[61];
long customer_balance[9];

same here:
on the file, the balance takes up 9 characters. But in
the program, those 9 charcaters form a single number.
One number, not 9!

You seem to be under the impression, that

long customer_balance[9];

declares a number which has 9 digits. This is not true!
It declares 9 variables customer_balance, each one of them
beeing able to hold a complete long number.
long credit_limit[7];

same here.
}c_record;

It seems to me, you are trying to much for your ability.
And it also seems that either:
* your tutors are nuts
* you don't ask the right questions to them, and they
are unable to understand what you really want or where
your problem really is.

Given your performance in this group, I personally guess it
is the later one.
struct file2 {
char record_type;
long customer_code[5];
long part_num[7];
long issue_rec[4];
};

struct file3 {
char record_type;
long customer_code[5];
};
[snip]

int main()
{ [snip]
char temp_customer_code[5]; [snip]
files.rec1.customer_code[5] = '\0';

files.recl.customer_code is defined to have a length of 5.
Since arrays start with index 0 in C and C++, there is no array
element with index 5. Valid array indices run from 0 to 4.

[snip]
switch(temp_customer_code[5])

Same here. There is no index 5 in temp_customer_code.
{
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '0':
default: prnfile<< "Customer code is invalid, no numerical format
found";
};

Ever heard of isdigit() ?

The above could correctly be rewritten as:

for( int i = 0; i < 5; ++i ) {
if( !isdigit( temp_customer_code ) )
prnfile << "Customer code is invalid, no numerical format found\n";
}

Heck, there are so many problems in your code, that one doesn't know where
to start correcting them. Worse then that. The whole program is flawed. The
best thing (for us) would be to throw it away and start afresh. The best
thing for you: drop this assignment. It is obviously orders of magnitudes
above your head. Start with something simpler.


PS: All of that sounds harsh. It isn't ment to be. The problem is, that you
have problems in walking, but you want to join the New York marathon. Worse
then that - you ask us the equivalent of: How do I win the NY marathon?
Start by learning how to walk.
 
J

John Harrison

struct file1 {
char record_type;
char customer_name[21];
long customer_code[5];

Here are you sure, that one customer has
5 codes, each one beeing a number

No!

A customer has only one code. At the file level
this code consists of 5 digits (=5 characters),
but once those 5 digits (5 charcaters have been)
read and verified, those 5 digits form a *single*
number:

long customer_code;

muser, you really need to grasp this concept before you can go any further,
Karl is right, Victor is right, I am right.

You can think of a number as a sequence of digits, that's a char array, or
you can think of a number as a mathemetical entity, that's a single long,
not an array.

You use the char array to read in the number and to check that it is all
digits, but then you convert that to a single long. Make the changes Karl,
Victor and I have suggested.

john
 
D

Default User

Karl said:
long customer_code[5];

Here are you sure, that one customer has
5 codes, each one beeing a number

No!

A customer has only one code. At the file level
this code consists of 5 digits (=5 characters),
but once those 5 digits (5 charcaters have been)
read and verified, those 5 digits form a *single*
number:

long customer_code;


However, I don't think a long is appropriate for storing things like
customer codes or phone numbers or other things that have numeric
components but aren't used in numeric processing. I believe that was
what Victor was getting at.

Such entities are really strings, even though all the characters are
numerals. I'd make customer_code a std::string (at the same time
replacing the char arrays for name and such). This gives the program
greater flexibility to represent codes. Thinking of a real-world
example, suppose the company decides to add an alpha character to the
customer code to indicate which region the customer lives in? Then a
long is useless.



Brian Rodenborn
 
M

muser

john i think you were right in the first instance, something a
newsgroup can noot help you with. But just to clarify a point or two.
Karl, victor and whoever else may feel that arrays aren't required in
my program, the customer code has something called a check digit, that
is the last digit, if that last digit isn't 0 or 6 then my program is
suppose to reject it as an incalid customer code. My fault for not
informing you. Every structure member has to be validated in some way.
My original question was; had i gone out of bounds with my arrays. A
simple yes or no would have sufficed. Thank you once again for your
support.

p.s. Karl so declaring a an array wouldn't be done thus: long
bad_boy[6]. Does that mean I have created 6 separate variables of the
bad boy type? Karl you are taking the p***. Karl if you can't answer
the simple questions effectively, what are you going to be like with
the difficult ones.
Karl remember that ignorance isn't a virtue.

Karl Heinz Buchegger said:
muser said:
Victor there is no need to be pedantic,

Yes there is!
programming is all about beeing pedantic!

I realise I could forfeit the
use of arrays and just have a char variable hold the numerical digits,
but these numerical digits require validation as they are part of an
address, i.e. struct records{


first of all (and looking at your program) you need to ditinguish between
* how is the data represented at the file
* how is the data represented in memory
struct file1 {
char record_type;
char customer_name[21];
long customer_code[5];

Here are you sure, that one customer has
5 codes, each one beeing a number

No!

A customer has only one code. At the file level
this code consists of 5 digits (=5 characters),
but once those 5 digits (5 charcaters have been)
read and verified, those 5 digits form a *single*
number:

long customer_code;
char customer_address[61];
long customer_balance[9];

same here:
on the file, the balance takes up 9 characters. But in
the program, those 9 charcaters form a single number.
One number, not 9!

You seem to be under the impression, that

long customer_balance[9];

declares a number which has 9 digits. This is not true!
It declares 9 variables customer_balance, each one of them
beeing able to hold a complete long number.
long credit_limit[7];

same here.
}c_record;

It seems to me, you are trying to much for your ability.
And it also seems that either:
* your tutors are nuts
* you don't ask the right questions to them, and they
are unable to understand what you really want or where
your problem really is.

Given your performance in this group, I personally guess it
is the later one.
struct file2 {
char record_type;
long customer_code[5];
long part_num[7];
long issue_rec[4];
};

struct file3 {
char record_type;
long customer_code[5];
};
[snip]

int main()
{ [snip]
char temp_customer_code[5]; [snip]
files.rec1.customer_code[5] = '\0';

files.recl.customer_code is defined to have a length of 5.
Since arrays start with index 0 in C and C++, there is no array
element with index 5. Valid array indices run from 0 to 4.

[snip]
switch(temp_customer_code[5])

Same here. There is no index 5 in temp_customer_code.
{
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '0':
default: prnfile<< "Customer code is invalid, no numerical format
found";
};

Ever heard of isdigit() ?

The above could correctly be rewritten as:

for( int i = 0; i < 5; ++i ) {
if( !isdigit( temp_customer_code ) )
prnfile << "Customer code is invalid, no numerical format found\n";
}

Heck, there are so many problems in your code, that one doesn't know where
to start correcting them. Worse then that. The whole program is flawed. The
best thing (for us) would be to throw it away and start afresh. The best
thing for you: drop this assignment. It is obviously orders of magnitudes
above your head. Start with something simpler.


PS: All of that sounds harsh. It isn't ment to be. The problem is, that you
have problems in walking, but you want to join the New York marathon. Worse
then that - you ask us the equivalent of: How do I win the NY marathon?
Start by learning how to walk.
 
K

Karl Heinz Buchegger

muser said:
john i think you were right in the first instance, something a
newsgroup can noot help you with. But just to clarify a point or two.
Karl, victor and whoever else may feel that arrays aren't required in
my program, the customer code has something called a check digit, that
is the last digit, if that last digit isn't 0 or 6 then my program is
suppose to reject it as an incalid customer code. My fault for not
informing you. Every structure member has to be validated in some way.
My original question was; had i gone out of bounds with my arrays. A
simple yes or no would have sufficed. Thank you once again for your
support.

p.s. Karl so declaring a an array wouldn't be done thus: long
bad_boy[6]. Does that mean I have created 6 separate variables of the
bad boy type?

No. it means you have created 6 variables of type *long*. Those 6 variables
are collectively named bad_boy. To distinguish between them you use
an index:

bad_boy[0] = 123;
bad_boy[1] = 21468;
bad_boy[5] = 0;
bad_boy[6] = ... That's a bug. bad_boy[6] does not exist.

Karl you are taking the p***. Karl if you can't answer
the simple questions effectively, what are you going to be like with
the difficult ones.

I think I let other of my replies in this and other newgroups speek
for themselfs.
 
J

John Harrison

muser said:
I do know of isdigit and had used it before in this self same program
I'am referring to, but for some reason or other it wasn't helpful and
I dropped it.
Can anyone copy and paste my program into their compiler, execute it
and tell me why they think the customer address isn't coming out at
all correctly when the customer code is.
All the boys talking about declaring arrays should finish their lunch
first, before spouting their rubbish. I've run the debugger on the
program and have eliminated the logical errors as well as the compiler
errors, so their is no need to do this.
Once again thank you in advance for your support.

muser, your attitude is amazing. Good luck in your future life as a
programmer, I think you are going to need it.

john
 
M

muser

John I believe that there a three main boys on C++, yourself, Victor
and Karl. Because you are the big boys, I feel that sometimes you
intimidate people who don't have the experience or knowledge you boys
have.
I think C++ is a game of certainities, insofar that if something is
wrong then there is only one real way of correcting it. A couple of
months ago I posted a problem I was having with this code, a couple
people used alot of diatribe and insulting language instead of trying
to help me through the problem, but then there was this gem of a
bloke, who posted what he thought the solution might be, and he did
this without the usual insults. The gem was correct, and my program
came on leaps and bounds.
I would like to thank Karl for taking the time to rewrite my program.
Recently I ran my program, and knew it was reading the disk in the A:
drive, but had no way of telling what it was reading. So I added a few
cout statements to verify this. One of the statements read ok, the
others haven't and although I suggested the arrays going out of bounds
it was all I could think the problem was at the time. What I wanted
was a certainity, a definite. This is the problem and either I have
the solution or i don't have the solution for you at this time.
Once you know what the problem is you got a 50/50 chance of solving
it.
Were you boys new to the game, who showed you the ropes back then. Was
it a rocky road to enlightenment?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top