Help with converting integer to string

This is a discussion on Help with converting integer to string within the C Programming forums, part of the General Programming Boards category; So, after reading a few chapters, my C book tells me that I can convert an integer to a string ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    62

    Help with converting integer to string

    So, after reading a few chapters, my C book tells me that I can convert an integer to a string using gets(). Now, I am wondering HOW to do this. If I get user input into a variable and try gets(number); it just says that it's invalid going from int to char.

    So, how exactly do you convert an integer to a string?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Look up sprintf()

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    62
    Quote Originally Posted by CommonTater View Post
    Look up sprintf()
    I looked up every page referring to sprintf() and it doesn't say much other than how it works differently than printf()

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    gets reads a line from the keyboard. So if the line you wrote was "12345", then you would in effect have a string that was a number 12345 (except it was never technically a number, it was always just a string).

    If you actually want to take a number from somewhere other than the keyboard, and convert it to a string, then sprintf is a fine way to do so.

    So take a bit of time, figure out what it is that you actually want to be doing here, put it in to a nice little post here telling us what it is you are trying to do, and show us the code where you actually tried to do that, and explain how you are stuck/not getting what you expect.

    Also - gets is bad. Read the FAQ.


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

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    62
    Ok. Here is what I need to do.

    I have to get the user to input a number. As far as I can tell, this is supposed to be an integer like "123" or "3424". Next I need to "scan the user's response into a string variable (use gets())".

    I'm not sure if this want's me to convert an integer to a string or directly put this into a string. However it seems like it wants it converted.

    As of now, i'v only put in enough code to get this one part working. Afterward I need to use atoi to convert it back to an integer (which is touched on much more heavily so I should be able to do that without an issue).

    Here's what I typed out so far, just to test::

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    
    int main(void)
    {
        
        int usernum;
        
        gets(usernum);
        puts(usernum);
        
        
        
        getchar();
        getchar();
        return 0;
    }
    Again. It's really nothing but it's just because i'm trying to get this to even accept it. Once it accepts it I can move on with life.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Ok, well gets and puts use strings. So you need an array of characters to hold your input, not an integer.


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

  7. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    62
    Quote Originally Posted by quzah View Post
    Ok, well gets and puts use strings. So you need an array of characters to hold your input, not an integer.


    Quzah.
    i'll try it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting an integer to a string
    By linuxlover in forum C Programming
    Replies: 5
    Last Post: 03-19-2011, 01:15 AM
  2. Unable to compare string with 'getter' returned string.
    By Swerve in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2009, 06:56 PM
  3. Replies: 8
    Last Post: 04-25-2008, 03:45 PM
  4. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  5. Converting a string to an integer
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 08-31-2001, 11:01 PM

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