a question about strtok.

This is a discussion on a question about strtok. within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by matsp Lets start with the "octal" bit. First of all it's 1, 2, 3, 4, 5, 6, ...

  1. #16
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    492
    Quote Originally Posted by matsp View Post
    Lets start with the "octal" bit. First of all it's 1, 2, 3, 4, 5, 6, 7, 10, 11, ... there's no 8 OR 9 in the number system.

    Second, in the computer, numbers are stored in binary. Octal and hex are popular ways to represent binary numbers so that they can be human readable and still easy to translate back to binary. This works because in octal, 3 bits (binary digits) are represented by one octal digit (0..7) and in hex 4 bits are represented by one hex digit. So it's very easy for a human to work out that 031 in octal is 000 011 001. We can also do the same the other way, using hex: 0 0001 1001 becomes 19.

    Decimal is much harder to deal with because the only way to do it is to divide by 2 or 10 depending on which way we're going, until there is nothing more to divide. This is because 10 is not a "nice" number of bits - each multiple of ten is approximately 3.3 bits... It would have been MUCH easier if we humans were born like cartoon characters (e.g. the Simpsons), with one thumb and three fingers. Then we would have used octal as our number base in the first place, and we wouldn't have had this problem.

    As to knowing if something is a number or not, you probably really want to put that into a function that resolves the symbols into numbers too, since you don't really care which they are when you see a particular instruction - you just want to translate it into a number, whcihever it is. Note that for labels that are forwards in the code, you will need a two-pass approach, so you read the entire file once, store away where each instruction belongs, and what the value of each label is, and then pass through it again and flll in any "gaps".

    --
    Mats
    thanks Dear Mats, so to achieve an octal based counter is to convert the decimal one to the octal ! ok big deal . tanx

    about the labels i havent started that part yet.i have some idea about how to implement and or convert them , ill tell you then .

    i have a question . do i have to dump the whole stuff user inputs to the emulated memory of the machine? or i should pars it and then the parsed string that is a combination of operation code , offset and possible addressing modes , and then dump them in memory?noticing that we have a 16 bit line for each instruction ?
    and then execute it from its memory?
    Code:
        ORG 9      //stating where to start               
                                                
    10. RED 6                                               
    11. LDA 5                                               
    12. SBA 6                                              
    13. JNG 17                                              
    14. WRI 5                                              
    15. JMP 20                                             
    17. WRI 6                                              
    20. HLT                                                 
    
    -----------------
         Machine Language    
     1110   000  000  000  101 
     1110   000  000  000  110 
     0001   000  000  000  101 
     0100   000  000  000  110 
     1000   000  000  001  111 
     1111   000  000  000  101 
     0111   000  000  010  000
     1111   000  000  000  110 
     0000   000  000  000  000
    would you help me on this?
    Last edited by Masterx; 11-13-2008 at 09:29 AM.
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Actually, if you are READING numbers, it's about converting an octal set of digits into a binary number, and if you are writing octal numbers out, it's about converting a binary number to octal digits - but the process is the same as for base 10 - if you read in decimal digits (say 123), do you know how to actually convert that into an integer value?

    I'm pretty sure that what you want in your emulated memory is the parsed and translated version of what the user inputs - but that's just what I think, I don't know the specification of your project.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #18
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    492
    Quote Originally Posted by matsp View Post
    Actually, if you are READING numbers, it's about converting an octal set of digits into a binary number, and if you are writing octal numbers out, it's about converting a binary number to octal digits - but the process is the same as for base 10 - if you read in decimal digits (say 123), do you know how to actually convert that into an integer value?

    I'm pretty sure that what you want in your emulated memory is the parsed and translated version of what the user inputs - but that's just what I think, I don't know the specification of your project.

    --
    Mats
    many tanx .
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  4. #19
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    492
    why doesnt this loop work?
    it just loops only once!
    Code:
     
    char label[15];
    string string1 = "start:              sta                  50";
    
    for (int i=0 ; string1[i]!=':'; i++ )
       { label[i]=string1[i];
        
        }label[++i]='\0';
        int b=i;
    but when i use it to print the string1 characters it works fine!
    Code:
        for (int i=0;string1[i]!=':';i++)
       cout<<string1[i];
    Last edited by Masterx; 11-14-2008 at 11:31 AM.
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  5. #20
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    492
    and another question how is it possible to access any element in a string with this format :char * string=" blah blah"?
    what do we call this kind of string ? !
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  6. #21
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,525
    >> why doesnt this loop work?
    The loop probably does work, but you have another problem on a different line:

    label[++i] = '\0';
    foo.cpp(14) : error C2065: 'i' : undeclared identifier

    Remember that if you initialize a variable in the for statement, it is local to the for statement. Meaning the variable i is undeclared where you are using it.

    In the future, please indicate that you've compiled your code by including any errors and/or warnings with your post.

    >> how is it possible to access any element in a string with this format :char * string=" blah blah"?
    If my memory serves me correctly, this used to not be the case. The ability to do that eventually came as a convenience to C programmers, who would declare an array first and then declare another pointer to ... erm, point at it. Particularly, a convenience to the C programmers who complained about it. After all, either an array or just a pointer worked in the given situation, and in pre-ANSI C you had to come up with two variable names for the same data. It's in C++ mainly because of its design being based on top of the C language.

    Do not abuse this feature. String literals are meant to be read-only, and should only be treated as constants in your code.

    >> what do we call this kind of string ? !
    pointer to string literal, perhaps. *shrug*

  7. #22
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    It is essentially the same thing.
    Code:
    char* MyName1 = "Cody";
    char MyName2[5] = "Cody";
    
    std::cout << MyName1[0] << " Should be 'C' " << MyName2[0] << " Should also be 'C'\n";

  8. #23
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,525
    >> It is essentially the same thing.
    Don't just say that. You'll likely just confuse him more.

  9. #24
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Raigne View Post
    It is essentially the same thing.
    Code:
    char* MyName1 = "Cody";
    char MyName2[5] = "Cody";
    
    std::cout << MyName1[0] << " Should be 'C' " << MyName2[0] << " Should also be 'C'\n";
    There is one subtle difference tho':
    The first form is a constant, and you are not supposed (and most often NOT ABLE) to modify that string. It should really be
    Code:
    const char* MyName1 = "Cody";
    , so that the compiler tells you off for modifying the string, instead of discovering when the code crashes (or behaves weird) that you're writing to a constant - it's always easier to fix compile-time errors than those that occur in runtime.

    In the case of the latter, you have 5 bytes of space that is initialized with "Cody" (with a nul-char at the end). You can put any other 4 characters in it's place - if you put a 5th in there, you have no space for the end-terminator, which could [read: will most often] lead to trouble.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #25
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    492
    Quote Originally Posted by citizen View Post
    >> why doesnt this loop work?
    The loop probably does work, but you have another problem on a different line:

    label[++i] = '\0';
    foo.cpp(14) : error C2065: 'i' : undeclared identifier

    Remember that if you initialize a variable in the for statement, it is local to the for statement. Meaning the variable i is undeclared where you are using it.

    In the future, please indicate that you've compiled your code by including any errors and/or warnings with your post.

    >> how is it possible to access any element in a string with this format :char * string=" blah blah"?
    If my memory serves me correctly, this used to not be the case. The ability to do that eventually came as a convenience to C programmers, who would declare an array first and then declare another pointer to ... erm, point at it. Particularly, a convenience to the C programmers who complained about it. After all, either an array or just a pointer worked in the given situation, and in pre-ANSI C you had to come up with two variable names for the same data. It's in C++ mainly because of its design being based on top of the C language.

    Do not abuse this feature. String literals are meant to be read-only, and should only be treated as constants in your code.

    >> what do we call this kind of string ? !
    pointer to string literal, perhaps. *shrug*
    man tanx dear citizen . i was completely forgot that! now its fine! tanx
    Quote Originally Posted by Raigne View Post
    It is essentially the same thing.
    Code:
    char* MyName1 = "Cody";
    char MyName2[5] = "Cody";
    
    std::cout << MyName1[0] << " Should be 'C' " << MyName2[0] << " Should also be 'C'\n";
    tanx bro, but this way it didnt work for me , instead *(Myname1+subscript) worked pretty great!
    Quote Originally Posted by matsp View Post
    There is one subtle difference tho':
    The first form is a constant, and you are not supposed (and most often NOT ABLE) to modify that string. It should really be
    Code:
    const char* MyName1 = "Cody";
    , so that the compiler tells you off for modifying the string, instead of discovering when the code crashes (or behaves weird) that you're writing to a constant - it's always easier to fix compile-time errors than those that occur in runtime.

    In the case of the latter, you have 5 bytes of space that is initialized with "Cody" (with a nul-char at the end). You can put any other 4 characters in it's place - if you put a 5th in there, you have no space for the end-terminator, which could [read: will most often] lead to trouble.

    --
    Mats
    tanx again dear Mats.

    by the way i have another question
    how can i make a new data type!
    for example i want to make a data that is capable of holding 3 char instead of 1 char
    for example see the following codes , im planning to do such a thing! if it is not possible how can i achieve my plan in this case?
    storing a label in a label register (each element of label register is capable of holding a 3 characters long string .!so that whenever i need a specific label name , by referring to labelregister[index]. i can retrieve the name!
    i tried using :
    #define mychar char[3]
    to make new data type capable of holding my labels ! but no luck!

    mychar char[3];
    mychar label[15];

    char name[3];

    label[0]=name;
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 04:48 PM
  3. strtok question
    By neandrake in forum C++ Programming
    Replies: 1
    Last Post: 11-18-2003, 02:51 AM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21