Execution

Discussion in 'C++' started by muser, Jul 29, 2003.

  1. muser

    muser Guest

    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.
     
    muser, Jul 29, 2003
    #1
    1. Advertising

  2. "muser" <> wrote...
    > 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
     
    Victor Bazarov, Jul 29, 2003
    #2
    1. Advertising

  3. muser

    John Dibling Guest

    On 29 Jul 2003 13:07:51 -0700, (muser) wrote:


    >Have I gone out of bounds at all.


    How can we possibly answer this question?

    </dib>
    John Dibling
    Witty banter omitted for your protection
     
    John Dibling, Jul 29, 2003
    #3
  4. muser

    Rolf Magnus Guest

    muser wrote:

    > 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.
     
    Rolf Magnus, Jul 29, 2003
    #4
  5. muser

    muser Guest

    Arrays

    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" <> wrote in message news:<bg6p8q$kn07n$-berlin.de>...
    > "John Harrison" <> wrote in message
    > news:bg6obi$lbjdb$-berlin.de...
    > >
    > > "muser" <> wrote in message
    > > news:...
    > > > 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
     
    muser, Jul 30, 2003
    #5
  6. Re: Arrays

    "muser" <> wrote...
    > [...]
    > 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
     
    Victor Bazarov, Jul 30, 2003
    #6
  7. Re: Arrays

    "muser" <> wrote in message
    news:...
    > 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
     
    John Harrison, Jul 31, 2003
    #7
  8. Re: Arrays

    muser wrote:
    >
    > 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.

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Jul 31, 2003
    #8
  9. Re: Arrays

    >
    > > 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
     
    John Harrison, Jul 31, 2003
    #9
  10. muser

    Default User Guest

    Re: Arrays

    Karl Heinz Buchegger wrote:

    > > 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
     
    Default User, Jul 31, 2003
    #10
  11. muser

    muser Guest

    Re: Arrays

    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 <> wrote in message news:<>...
    > muser wrote:
    > >
    > > 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.
     
    muser, Aug 1, 2003
    #11
  12. Re: Arrays

    muser wrote:
    >
    > 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.

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Aug 1, 2003
    #12
  13. Re: Arrays

    "muser" <> wrote in message
    news:...
    > 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
     
    John Harrison, Aug 2, 2003
    #13
  14. muser

    muser Guest

    Re: Arrays

    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?








    "John Harrison" <> wrote in message news:<bgfhjh$og5aa$-berlin.de>...
    > "muser" <> wrote in message
    > news:...
    > > 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
     
    muser, Aug 7, 2003
    #14
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kishor

    Dynamic Execution of Function/Proc

    Kishor, Sep 24, 2003, in forum: ASP .Net
    Replies:
    9
    Views:
    554
    Kishor
    Sep 27, 2003
  2. owais
    Replies:
    1
    Views:
    1,066
    Azhagan
    Oct 5, 2004
  3. =?Utf-8?B?TXVzdGFx?=

    To Know bat file, execution is completed using VB.NET

    =?Utf-8?B?TXVzdGFx?=, Jun 23, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    1,766
    Francois Bonin [C# MVP]
    Jun 23, 2005
  4. a_Conan

    Finding the execution time

    a_Conan, Sep 2, 2005, in forum: VHDL
    Replies:
    13
    Views:
    765
    a_Conan
    Sep 5, 2005
  5. priya
    Replies:
    0
    Views:
    1,242
    priya
    Oct 3, 2005
Loading...

Share This Page