Thread: Trying to obtain int from inside a string.

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    53

    Trying to obtain int from inside a string.

    Having trouble reading an int from a string.

    categoryID is always of the format of <char>'000++' i.e. C0001 C0002...

    Code:
     strncpy(temp, current->categoryID, ID_LEN);
            sprintf(temp, "%04i", atoi(intTemp) + 1);
            printf("%d\n", intTemp); // this outputs a large number not the '000++' value.
    Last edited by TonyG; 05-17-2011 at 04:56 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It sounds like you actually want:
    Code:
    intTemp = atoi(current->categoryID + 1);
    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

  3. #3
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    86
    how about a combination of strcspn & strtol?
    Do not stare at my avatar.

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    53
    worked it out thanks.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by TonyG View Post
    Having trouble reading an int from a string.

    categoryID is always of the format of <char>'000++' i.e. C0001 C0002...
    sscanf ("C%d ",number);

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    >sscanf ("C%d ",number);
    Did you mean
    sscanf ("C%d ", &number);

    And to be more versatile
    Code:
    if( sscanf(  buffer, "%*c%d", &d ) == 1 ) { ... }
    else { print error }
    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  7. #7
    Registered User
    Join Date
    May 2011
    Posts
    53
    Actually I have change the plan for this process. And now i am trying to generate a string that will increment as above C0001, C0002, C0003... every time it does a loop.
    This is the code i have at the moment but for some reason the output is not correct.
    Code:
    void addCategory(GJCType* menu) {
    
        char newCatID[ID_LEN + 1] = {0};
        char catID[ID_LEN + 1] = {"C"};
        char* catIDPtr = catID;
    
        char temp[menu->numCategories][ID_LEN + 1];
        int index = 0;
        char* newCatIDPtr = newCatID;
    
        CategoryType* current = menu->headCategory;
    
        while (current != NULL) {
            index++;
            current = current->nextCategory;
            sprintf(newCatID, "%04i", atoi(newCatID) + index);
            strncat(catIDPtr, newCatID, ID_LEN);
            printf("%s\n", catIDPtr);
            strncpy (catID, "C\0", ID_LEN + 1);
        }
    
    }
    OP
    C0001
    C0003
    C0006
    C0010
    C0015
    C0021
    C0028
    Last edited by TonyG; 05-18-2011 at 12:56 AM.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Get rid of the atoi chunk, and just stick the index there. If you took a moment to look at your output you should have been able to see what you were doing wrong.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    May 2011
    Posts
    53
    Just one of those things.. Kept banging my head for hrs working this out.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by TonyG View Post
    Just one of those things.. Kept banging my head for hrs working this out.
    Hi Tony... There's a little trap I kept falling into, took me a while to get out of it too... When things start going wrong the problem appears nebulous in it's complexity, so the natural tendency is to keep adding or changing a lot of stuff... In truth, it makes more sense to stop, take a break, and try to think about how to *simplify* the problem to a level where you can *remove* code to fix it. Life got a lot easier when I finally got my head around that idea.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. string inside of a structure?
    By dyelax in forum C++ Programming
    Replies: 8
    Last Post: 06-17-2010, 07:09 PM
  2. Find String inside Array?
    By hajas in forum C Programming
    Replies: 8
    Last Post: 06-20-2007, 01:48 PM
  3. GCC not able to distinguish comments inside string
    By Yasir_Malik in forum C Programming
    Replies: 11
    Last Post: 06-23-2005, 12:11 PM
  4. Replies: 12
    Last Post: 10-14-2003, 10:17 AM
  5. Replies: 4
    Last Post: 01-22-2002, 11:13 PM