Thread: itoa()

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    18

    itoa()

    I'm trying to count how many times a number appears in an array. Basically you have something like this:

    Code:
    int array[3][3] ={	{'23', '31', '2349'},
    						{'7567', '1', '84'},
    						{'355', '75', '12 '}};
    int three = 0;
    int five = 0;
    Now I need to count how many times a number appears in that array. I.e. 355 would make three = 1 and five = 2. So I need to break down these numbers into chars or something where I can count each one. Can anyone tell me how to do this?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You could read the FAQ on Convert an int to a string (C++).

    I note that single quotes are used to delimit chars, so you should probably drop them from your example code.
    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
    Join Date
    Jan 2005
    Posts
    7,366
    Edit: I think I misread the problem. The values are already ints, you want to convert to string to make it easier. That makes more sense than what I was originally thinking you were trying to do (the quotes threw me off).

    An alternate solution to converting to a string would be to use division and modulus with multiples of 10 to find the separate digits of each number.
    Last edited by Daved; 06-26-2007 at 11:41 AM.

  4. #4
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    310
    If I was doing this, I'll just loop in the array...
    * PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
    * Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.

  5. #5
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    when you mod a decimal number by 10 you get the digit in the 0's place.
    when you divide it you truncate the number, given you divide it into an int and not a float.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem using itoa
    By g_p in forum C Programming
    Replies: 2
    Last Post: 05-03-2008, 06:38 AM
  2. Problem with itoa() perhaps?
    By TheSquid in forum C++ Programming
    Replies: 5
    Last Post: 05-08-2006, 02:04 AM
  3. Really Weird itoa Problem
    By Grantyt3 in forum C++ Programming
    Replies: 8
    Last Post: 12-20-2005, 12:44 AM
  4. itoa
    By coldcoyote in forum Linux Programming
    Replies: 4
    Last Post: 02-13-2003, 09:28 AM
  5. itoa
    By Thantos in forum C Programming
    Replies: 2
    Last Post: 09-18-2001, 02:23 PM