Thread: How to convert string containing numbers (separated by chars) to an int?

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    16

    How to convert string containing numbers (separated by chars) to an int?

    I've read a little bit about atoi() and some similar functions, but I can't get them working to resolve my particular problem..

    I've scanned in some numbers in the thousands, and these have a dot "." in them, but I need to be able to treat them as integers.

    Example: my string is "17.334" and I want an int containing just the "17334"

    Thank you in advance for any help provided

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    Check each character in the input string. If the character is a digit copy it to a new string. Then use atoi on the new string.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    16
    I thought your post made enough sense that I would be able to code it, but after moving on and returning to this problem several times, I'm still lost..

    I tried implementing
    Code:
    char new_string_name;
    int int_conversion;
    if(isdigit(data[lineNo].variable)) {
    strcpy (new_string_name, data[lineNo].variable);
    }
    int_conversion = atoi(new_string_name);
    to check characters in the input string, however a few problems arise..

    First of all, I can't figure out how to make my program interpret the array.

    There might well be other problems with the above code that I haven't spotted.. Do I have the parameters right?

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    16
    Since I'm taking these values from an array of strings, I should of course copy and parse them into an array of int's, so it would look more like this:

    Code:
    char new_string[lineNo];
    int int_conversion[lineNo];
    
    for(lineNo = 1; lineNo < MAX_LINES; lineNo++)
    {  
       if(isdigit(data[lineNo].variable))
           {
              strcpy(new_string[lineNo], data[lineNo].variable;
           }
    }
    
    int_conversion[lineNo] = atoi( new_string[lineNo] );
    ..or am I missing/skipping some steps?
    Last edited by kensing; 11-26-2012 at 11:36 AM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Your compiler should be giving you some warnings. What are they and what do they mean to you? If you do not get any warnings, then compile at a higher warning level (and if you still don't get any warnings, then you should switch to a more helpful compiler).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    16
    Actually, yea, loads of warnings.. I should compile more often. I'll start from the beginning I guess, since this whole array subscript thing seems really tricky to me.

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by kensing View Post
    strcpy(new_string[lineNo], data[lineNo].variable;
    ...
    ..or am I missing/skipping some steps?
    To use DRK's suggestion you need to deal with each character; that means strcpy will not work.

    An alternate suggestion:

    You have a string for example "12.98323" stored in s1 and you want to store the integer in x.
    Code:
    squeeze(s1, ".");
    int x = atoi(s1);
    The function squeeze removes all occurences of "." from s1. This function is defined/suggested as an exercise in Chapter 2 of K&R. Advantages of this "homework" approach:
    1. trying the exercise yourself will give you practice with subscripting
    2. if you can't figure it out you can search online for published solution(s)

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    There is yet another possibility: perform the computation to arrive at the integer result yourself, ignoring invalid characters.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Nov 2012
    Posts
    16
    Ignoring invalid characters how?

    Another major problem, which is underlying most of my other problems, is how to reference this array that I've built up:

    Code:
    struct kampdata
    {
       char dato[STRENG_LAENGDE_MAX],
              tid[STRENG_LAENGDE_MAX];
            
    };
    typedef struct kampdata kampdata;
    
    kampdata data[LINJER_MAX];
    char linje[LINJE_LAENGDE_MAX];
    int linjeNr = 1;
    FILE *inputfil;
    
    int main()
    {
       inputfil = fopen("superliga-2011-2012.txt", "r");
       if (inputfil != NULL)
       {
          while (linjeNr < LINJER_MAX && fgets(linje, LINJE_LAENGDE_MAX, inputfil) != NULL)
          {
             sscanf(linje, "%s %s\n", data[linjeNr].dato, data[linjeNr].tid);
                 linjeNr++;
          }    
          fclose(inputfil);
        }
    
    printf("%s\n", data[8].dato);
    I got it working, and have no problem printing out all [linjeNr] values of for example data[linjeNr]dato

    But when I want to reference it within a function, for example in regards to this string-to-int-conversion, I'm compiling fine but get no result..

    Edit: apologies if this code was too much off-topic, I'm just a little stressed with this assignment

  10. #10
    Registered User
    Join Date
    Nov 2012
    Posts
    16
    This is what I'm trying to do:

    Code:
    for(linjeNr = 1; linjeNr < LINJER_MAX && (data[linjeNr].hold1score + data[linjeNr].hold2score) >= 5; linjeNr++)
    {
        printf("%s\n", data[linjeNr].tilskuertal);
    }
    ^Compiling fine but nothing is printed

    The code below prints the first value of data[linjeNr].tilskuertal within the inputfile that meets the >=5 requirement, and just keeps spamming that same value instead of continuing to print the next
    Code:
    for(linjeNr = 1; linjeNr < LINJER_MAX; linjeNr++)
    {
      if((data[linjeNr].hold1score) + (data[linjeNr].hold2score) >= 5)
        printf("%s\n", data[linjeNr].tilskuertal);
    }
    Sorry again for any unnecessary confusion caused..

  11. #11
    Registered User
    Join Date
    Nov 2012
    Posts
    16
    I solved the problem with the above code

  12. #12
    Registered User
    Join Date
    Nov 2012
    Posts
    16
    As for the string to int problem, it hit me that I could just scan it as a double instead, multiply by 1000, and then treat them with %.0lf..

    Do you think my teacher might think that this is a bit too much of an imaginative/lazy workaround?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. convert set of chars into double
    By doia in forum C Programming
    Replies: 13
    Last Post: 06-15-2010, 10:25 AM
  2. Convert Integer to Hex in an array of chars
    By anasimtiaz in forum C Programming
    Replies: 12
    Last Post: 07-22-2009, 05:29 PM
  3. Replies: 2
    Last Post: 03-15-2009, 03:17 PM
  4. Replies: 2
    Last Post: 10-22-2008, 07:20 PM
  5. chars and numbers in C++
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 01-10-2002, 05:49 PM