GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 17-Aug-2005, 09:49
soldier soldier is offline
New Member
 
Join Date: Aug 2005
Posts: 1
soldier is on a distinguished road

Convert Integer to String


CPP / C++ / C Code:
int testnumb;
 testnumb ++;
 char buffer [15];
 char* p;
 p = _itoa(testnumb, buffer, 10);

the last line gives an error. Can someone help me solve this.
Last edited by LuciWiz : 17-Aug-2005 at 10:47. Reason: Please insert your C code between [c] & [/c] tags
  #2  
Old 17-Aug-2005, 10:27
Kacyndra's Avatar
Kacyndra Kacyndra is offline
Member
 
Join Date: May 2005
Location: Maryland
Posts: 226
Kacyndra will become famous soon enough
i don't have a compiler, but, i believe this is what you need:
Code:
char str [1]; str[1] = SomeNumber; atoi (str);

i doubt if this will compile, but i think this is the fucntion you need
__________________
Xrum!
  #3  
Old 17-Aug-2005, 11:34
L7Sqr L7Sqr is offline
Member
 
Join Date: Jul 2005
Location: constant limbo
Posts: 148
L7Sqr has a spectacular aura aboutL7Sqr has a spectacular aura about
explain the errors you are getting.
Although they seems like an annoyance, you should learn to rely n them for information.
Are they run-time or compile-time errors?
Provide more information for a better response.
  #4  
Old 18-Aug-2005, 08:56
Badsy Badsy is offline
New Member
 
Join Date: Aug 2005
Location: Sri Lanka, Colombo
Posts: 14
Badsy is on a distinguished road
CPP / C++ / C Code:
int testnumb;
 testnumb ++;
 char* p;
sprintf(p,"%d",testnumb);

this should work.
Last edited by LuciWiz : 26-Mar-2006 at 12:57. Reason: Please insert your C code between [c] & [/c] tags
  #5  
Old 18-Aug-2005, 09:17
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,793
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by Badsy
int testnumb;
testnumb ++;
char* p;
sprintf(p,"%d",testnumb);

this should work.

There is one thing I like about this, and two that I don't like:

I much prefer the standard library sprintf() to the nonstandard _atoi(), so I like that part, but I don't know what you mean by "work".

It tries to store an unknown value into an undefined memory address. What do you think it stores and where does it store it?

As L7Sqr indicated, if the original poster would show his expectations (what is he trying to do) and his environment (what compiler, what error messages) maybe we could give more helpful advice.

Regards,

Dave
  #6  
Old 18-Aug-2005, 10:11
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,793
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by Kacyndra
i don't have a compiler, but, i believe this is what you need:
Code:
char str [1]; str[1] = SomeNumber; atoi (str);

i doubt if this will compile, but i think this is the fucntion you need

1. You declare an array of char that has one member. That's not illegal, but why would anyone want an array with only one member?

2. You store something (a single char) in an address that is outside the array That is illegal, since the only legal address will be at str[0].

3. The original post used the non-standard library function _itoa(). Compilers that have library support for this function use it to convert an int to a string, whereas the standard library function atoi() converts a string to an int. Maybe that is what the original poster needs, but how can we tell? As L7Sqr implied, the original poster needs to tell us more.

Regards,

Dave

P.S.
I know, I know: Sometimes I am so eager to try to help someone that I jump to a conclusion and try to answer a question he didn't ask. I'm working on it. We are all trying to help, and your intentions are admirable, but I think L7Sqr has the right approach: let them tell us what they mean, then try to help.
 
 

Recent GIDBlogWriting a book by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help wit my source code compiler errors Krandygrl00 C++ Forum 1 06-Jun-2005 09:14
Converting string to float CT++ C++ Forum 2 10-May-2005 12:29
How to convert int to string in MFC? george MS Visual C++ / MFC Forum 15 14-Apr-2005 12:18
Convert Unsigned short int to a string... Pissus C++ Forum 2 11-Jan-2005 09:28

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 10:43.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.