Char to int using functions

This is a discussion on Char to int using functions within the C++ Programming forums, part of the General Programming Boards category; Hello all, I have a program that imputs some numbers and had 8 different functions that calculate the numbers in ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    30

    Char to int using functions

    Hello all,

    I have a program that imputs some numbers and had 8 different functions that calculate the numbers in some sort of way so in the end I have 8 different numbers. I have managed to get all the functions wroking and the number output are right but the last thing I have to do is output the corresponding characters to the integers and I can't seem to work it out. I have tried so many different things. Should I make another function to perform this or just do it in the main? Please help.

    Thanks so much

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,781
    Are you trying to convert a char to an int? If so, that is easy, since chars are ints with a limited range. An implicit cast will do:
    Code:
    char ch = 'A';
    int num = ch;
    If you do not want to use a temporary like that, you could always do an explicit cast:
    Code:
    char ch = 'A';
    std::cout << static_cast<int>(ch) << std::endl;
    If you are trying to convert a string to an int, on the other hand, then that needs a little more work, usually by using a stringstream with <sstream>, or atoi() from <cstring>, or perhaps something like Boost's lexical_cast.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    30
    yeah i'm trying to convert a char to an int but the int is a result from a function and for some reason i can't convert it.

    if the number is over 25 i have to number%26 to get a lower number so this is what i have for the function....

    Code:
    char convert (int number)
    {
        char character; 
        if (number >= 25)
          {number = number%26;}
        character = number;
        return character;
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,781
    It looks like you are trying to convert an int to a char, but in particular, you are trying to map integers to the alphabet. A simple solution would be something along the lines of:
    Code:
    char convert(int number)
    {
        const char* alphabet = "abcdefghijklmnopqrstuvwxyz";
        return alphabet[number &#37; 26];
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    30
    Thanks so much, it works. Yay!!

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    30
    Just wondering what the * after char does? Why won't it compile without it?
    Thanks

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,781
    That is just pointer notation, as in alphabet is a pointer to a null terminated string of characters. Perhaps more C++ish would be:
    Code:
    char convert(int number)
    {
        const std::string alphabet("abcdefghijklmnopqrstuvwxyz");
        return alphabet[number &#37; alphabet.length()];
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C problem with legacy code
    By andy_baptiste in forum C Programming
    Replies: 4
    Last Post: 05-19-2008, 06:14 AM
  2. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Replies: 4
    Last Post: 11-23-2003, 06:15 AM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM

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