Question involving converting array->int

This is a discussion on Question involving converting array->int within the C++ Programming forums, part of the General Programming Boards category; I have an array (char iiword[256];). I filled it with iistream.get()s. When i use printf("%s", iiword); the actual number I ...

  1. #1
    Unregistered
    Guest

    Unhappy Question involving converting array->int

    I have an array (char iiword[256];).
    I filled it with iistream.get()s.
    When i use printf("%s", iiword); the actual number I want is displayed.
    However, i do not know how I can convert the printed number into an integer.
    Anyone have any idea how to do this? Thanks.
    -Drekenzin

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    162
    Just write a simple function that accepts the character and converts it to it's respective integer. Basically something like:
    Code:
    int convert(char number)
    {
      if(number < '0' || number > '9'){
          return -1; //check for -1 when using function 
       }
       switch(number)
       {
        case '0':
             return 0;
        case '1':
             return 1;
         //etc.....
       }
    }
    Just loop through the array and check to be sure the chars are really 0-9 with a similar if statement like above, then send that element of the array to the function. Reply if this doesn't work and I will try to fix it(I did that out of my head).

  3. #3
    Unregistered
    Guest
    The thing is, the array holds the individual digits of the integer.
    for example:
    iiword[0] = 1
    iiword[1] = 5
    iiword[2] = 9
    ...
    i'd want to get 159 set to an integer.
    do you know how to do this?

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    162
    Oh, well in that case you could do something like this:

    Code:
    int convert(char *string)
    {
    int tempnumber = 0;
    int number = 0;
    int place;
       for(unsigned int i=0; i<strlen(string); i++)
       {
           switch(string[i])
           {
            case '0':
                 tempnumber = 0;
                 break; 
           case '1':
                 tempnumber = 1;
                 break;
           //etc...
           }
           //now find out what place value the number holds
           if(!strlen(string)-i)
              place = 1; 
           else{
              place = strlen(string)-i;
           number += (place * tempnumber);
          }
       }
    return number;
    }
    Something like that. You will have to complete the switch statement of course, and I am not sure if the place thing will work or not. Tell me if it does or doesn't.

  5. #5
    Unregistered
    Guest
    nope, that doesn't work =(

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    162
    Appearantly math isn't my strong point. Since our number system is base 10 I need to make the number 10 to the power of place * tempnumber. Stupid me! :-) Here is the revised code:

    PHP Code:
    int convert(char *string)
    {
    int tempnumber 0;
    int number 0;
    int place;
       for(
    unsigned int i=0i<strlen(string); i++)
       {
           switch(
    string[i])
           {
           case 
    '0':
                 
    tempnumber 0;
                 break; 
           case 
    '1':
                 
    tempnumber 1;
                 break;
           case 
    '2':
                 
    tempnumber 2;
                 break; 
           case 
    '3':
                 
    tempnumber 3;
                 break;
           case 
    '4':
                 
    tempnumber 4;
                 break; 
           case 
    '5':
                 
    tempnumber 5;
                 break;
           case 
    '6':
                 
    tempnumber 6;
                 break; 
           case 
    '7':
                 
    tempnumber 7;
                 break;
           case 
    '8':
                 
    tempnumber 8;
                 break; 
           case 
    '9':
                 
    tempnumber 9;
                 break;
           }
           
    //now find out what place value the number holds
           
    place strlen(string)-i;
           
    number += (pow(10,place) * tempnumber);
           
    cout << << "\n";
       }
    cout << "\n";
    return 
    number/10;

    Be sure to include math.h for the pow function.

  7. #7
    Unregistered
    Guest
    hi, you can go to the topic named "something wrong with this?". I posted a function where my code might be useful to you. this algorithm can be used without using strlen(); what it does is something like this:

    Code:
    //replacement code (used inside the code to replace the strlen() )
    
    long int storage=0; //initialize this outside the loop
    
    //inside the loop :
    
    int temp=<given number>;
    storage=storage * 10 + temp; /* this statement can be used to replace strlen() */

  8. #8
    Unregistered
    Guest
    also, don't you mean istream.get() ?
    is this better than getch() ?

  9. #9
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    1. Input as char array
    2. Find the length of the input.
    3. Create a loop that goes from length minus one till zero.
    4. Convert current number into an int
    5. Multiply that int by 10^loop # and add it to current temp
    6. Continue with loop until comple and return temp.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 06:07 PM
  2. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 12:01 AM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 11:09 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

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