#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2000
    Location
    Jackson, TN, USA
    Posts
    18
    Rep Power
    0

    data type conversions - student help


    Hello,

    I am a college student learning C++. This is a class administered over the internet and we have a terrible teacher who will not respond to any questions at all. He will not help us out in any way.

    Our current assignment requires that we convert a couple of pointers to other data types. They start off as char and one is to be converted to an int and another to a long. We have studied static_cast and had a little about void *, but neither had an example of how to use them and the book is EXTREMELY convaluted and nearly impossible to understand.

    This problem is class wide, we have a week extension just for this one exersice.

    I have been able to convert the data types using atoi and atol, but these are not studied until a much later chapter, so I don't think it will be accepted. The teacher won't even tell us if they will or not.

    Some of us really want to learn to be good C++ programmers, but we can't without help!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2001
    Posts
    34
    Rep Power
    13
    Ok, by character pointers I assume you mean strings, because it unless your array is only one element long, or you know it's length, it's impossible to convert it without causing a core dump when you go past your memory space.


    Assuming that you have a null terminated character string, what you need to do to convert it to a int is use

    strlen (3) to determine the lenght of the string. That will determine the number of digits you need. You must then loop through the character array get the character, then convert the character to an int, this is really easy to do,

    for example
    if(character == '4'){

    return 4;

    }

    multiply the int result by the length,

    add that result to the return number, increment you array counter and decrement the length counter. Repeat.


    When you're done with the iteration return your return number.


    The long function works exactly the same except you must return long.


    Check the man page for strlen (3)
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2000
    Location
    Jackson, TN, USA
    Posts
    18
    Rep Power
    0
    Thanks so much for responding. I fairly well understand what you are saying, but I'm not real clear on how to implement it in my program - bear in mind we are only on Chapter 4 in our book and I've had no prior C++ experience. Visual Basic and PHP are all I know and that's not that extensive.

    Our program:

    The user inputs a phone number (555) 555-5555. Then using strtok we break it down to 3 variables (using pointers) basically resulting in:

    char *areacode = 555;
    char *prefix = 555;
    char *number = 5555;

    Then we have to concatenate the prefix and number resulting in:

    char *conc_number = 5555555;

    All that I can do. Next we are to convert *prefix to an int data type and *conc_number into a long data type. Now I know the length (by the way, I'm familiar with strlen), but I don't understand exactly how to do the rest.

    Pardon my stupidity, I'm just not far enough along to understand this.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2001
    Posts
    34
    Rep Power
    13
    Do make sure that you can't use atoi(3) and atol(3)

    if you can't then you basically have to code your own atoi() and atol()

    as said before,

    first strlen(3) the input string, then

    create a counter variable.
    create a return int (or long)

    Create a while loop

    like while(mystring[counter] != '\0'){

    get the data at the index which will be of type char.

    convert the char into a numeric value. You may want to create a helper method for that.

    then add the return int to the number times the length.

    returnint = returnint + (number*length)

    at the end of your loop, increment counter and decrement your length.

    counter++;
    --length.


    At the end of your function, return your return int (or long)

    at that's an implementation of atoi(3) and atol(3)
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Seattle WA
    Posts
    863
    Rep Power
    14
    Well, iterating through a pointer to a character array is easy enough. All you need is pointer arithmetic if you know the size of the array.

    Code:
    // Assume that pMyCharArray points to the beginning of a char[].
    for (int i = 0; i < strlen - 1; i++) // Assuming a null terminated array.
    {
        // Do stuff to the character at *pMyCharArray.
        pMyCharArray++; // Increment the pointer to the next array element.
    }
    Now how to get the integer out of each character? The brute force method was already mentioned, where you compare the value of each character via a huge if structure, but there is a way to actually calculate it.

    What happens when you recast a char to an int? What does this return?

    Code:
    char chNum = '0';
    int nNum = (int) chNum;
    std::cout << nNum << std::endl;
    What about if chNum = '1', or chNum = '2', etc? What does the number stored in nNum represent when you cast a char to an int?

    Don't forget how base10 numbers work: 1234 = (1 * 10^3) + (2 * 10^2) + (3 * 10^1) + (4 * 10^0).

    -Mike
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2001
    Posts
    34
    Rep Power
    13
    casting a char to an int will result in the ascii value of the char being returned, not the number.


    '1' = 56 I believe, so basically you can either use the brute force method, or fuddle around with ascii values, which requires you to know the ascii character set and it's quirks.


    char chNum = '0';
    int nNum = (int) chNum;
    std::cout << nNum << std::endl;
    Uses unintuitive ascii tricks to work, not to mention use of shifting operators and the like
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Seattle WA
    Posts
    863
    Rep Power
    14
    Heh, I was trying to be a little subtle and not give that away. When it comes to tutoring school assignments, I don't like to give out full answers, provide hints and such, and let the student understand the situation and apply the knowledge.

    Ascii codes for the numbers 0-9 are 48-57, consecutive, so it isn't hard to convert and cast an int from a char.
  14. #8
  15. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2000
    Location
    Jackson, TN, USA
    Posts
    18
    Rep Power
    0
    To all you guys:

    Thanks a million!!! I'm sure I will get this now. I happen to be a visual learner and seeing the examples makes it really clear to me. I have no problem applying it to my particular problem.

    You have been a great help. I got an email from another classmate that said that the instructor admitted to him that the book failed to cover come aspects of this assignment in the chapter. However, it hasn't inspired the instructor to offer the first bit of help, and I can't understand why he picked this particular assignment out of about 40.

    Anyway, you guys are great teachers and a huge credit to the C++ community. None of us are islands to ourselves, when we work together the whole benefits from the effort.

    Thanks again!!

IMN logo majestic logo spyfu logo threadwatch logo seochat tools logo