Converting String to Integer in C

BeaverC32BeaverC32 Member Posts: 670 ■■■□□□□□□□
Quick programming question:

How would I convert a string "1,756" into an integer using C?

I tried the atoi function but I get back "1" -- I believe the comma in the string is the cause of my trouble :)
MCSE 2003, MCSA 2003, LPIC-1, MCP, MCTS: Vista Config, MCTS: SQL Server 2005, CCNA, A+, Network+, Server+, Security+, Linux+, BSCS (Information Systems)

Comments

  • tierstentiersten Member Posts: 4,505
    Strip out the commas first. You could use sscanf if the string is always that format but I'd just use atoi after removing the commas.
  • BeaverC32BeaverC32 Member Posts: 670 ■■■□□□□□□□
    Thanks for the suggestion, I took your advice and found a function to do just that:

    char* squeeze(char s[], int c)
    {
    int i, j;

    for(i = j = 0; s != '\0'; i++)
    if (s != ','){
    s[j++] = s;
    }

    s[j] = '\0';

    return s;
    }
    MCSE 2003, MCSA 2003, LPIC-1, MCP, MCTS: Vista Config, MCTS: SQL Server 2005, CCNA, A+, Network+, Server+, Security+, Linux+, BSCS (Information Systems)
  • djhss68djhss68 Member Posts: 205
    I know a few things about C/C++, but God do I hate programming. icon_lol.gif

    Yet it's so appealing to me at the same time.
  • BeaverC32BeaverC32 Member Posts: 670 ■■■□□□□□□□
    djhss68 wrote: »
    I know a few things about C/C++, but God do I hate programming. icon_lol.gif

    Yet it's so appealing to me at the same time.

    I don't mind programming, it's just that I do it so infrequently that I lose a lot of what I learned in previous programming efforts. I like it from time to time actually, it is a nice change of pace. I could see how people would make a career out of it, there is always a certain degree of satisfaction when you finally figure out how to make your code do what it was intended to do.
    MCSE 2003, MCSA 2003, LPIC-1, MCP, MCTS: Vista Config, MCTS: SQL Server 2005, CCNA, A+, Network+, Server+, Security+, Linux+, BSCS (Information Systems)
  • adinarayana.pvadinarayana.pv Member Posts: 1 ■□□□□□□□□□
    I have joined newly in this forum...

    I found it very good for improving the knowledge..

    I found not more threads on C language...

    Can somebody suggest any forum for C related stuffff....
Sign In or Register to comment.