|
-
October 26th, 2002, 09:10 PM
#1
NEWBIE: converting int into string...
Ok, it's not like everywhere. What I want to get here is:
1. We have an int. For example 10.
2. We have a 10-byte string: "0000000000"
3. We want to place our int into that string, so it looks like this: "0000000010"
How would I do that?
-
October 26th, 2002, 10:30 PM
#2
okay.. i know this doing your homework
here is an example of how to achieve what you asked. There are many ways to go about it but this will work. I provided no error checking to insure the user entered an number and just so you know, the value of int a is bound by the limits of an int. I'm just saying you should add a bit of code to verify the input is correct BEFORE you try to manipulate it.
#include<iostream>
#include <string.h>
using namespace std;
int main(int argc, char* argv[])
{
int a;
cout << "Enter a number: " << endl;
cin >> a;
char str[10], str2[10]={0};
itoa(a, str,10);
for (int i = 10-strlen(str); i > 0; i--)
strcat(str2, "0");
strcat(str2, str);
cout << str2 << endl;
return 0;
}
Last edited by mfeik; October 26th, 2002 at 10:33 PM.
Of all the things I've lost, I miss my mind the most.
-
October 26th, 2002, 10:39 PM
#3
eh hem
did i just do someones homework??
Oh well... I actually need to write a routine which does exactly the opposite of what I posted so it was a good starter (I needed a function that can fill the end of an a string with whitespace up to the 19th indexable position after the last char). Guess I'm defending myself for doing for someone what they really should have done for themselves as if they get tested and didn't learn it on thier own... well.. we all know the outcome.
-Mel
Of all the things I've lost, I miss my mind the most.
-
October 27th, 2002, 12:13 AM
#4
Thank you it works just fine!
But for some reason I get "Debug Error" window. If I comment out the line "strcat(buf2, "0")", it doesnt give me an error, but of course it doesnt work. I am debugging it right now, looking for cause, but maybe you can help?
-
October 27th, 2002, 12:14 AM
#5
PHP Code:
char * buf1 = new char[spaceForKeyDigits];
char * buf2 = new char[spaceForKeyDigits];
//for ( int j = 0; j < spaceForKeyDigits; j++ ) buf2[j] = 0; // buf2[]={0}
itoa(keyDigits, buf1, 10);
for ( int i = 10-strlen(buf1); i > 0; i-- )
strcat(buf2, "0");
strcat(buf2, buf1);
// Insert buf2 into str
int strLen = strlen(str)/2;
i=0;
for ( int z = strLen; z < strLen+spaceForKeyDigits; z++ )
{
str[z] = buf2[i];
i++;
}
delete [] buf1;
delete [] buf2;
Hmm.. If I dont declare the first two buffers as a dynamic memory it works fine. Interesting.
-
October 27th, 2002, 12:45 AM
#6
I was not clear in understanding your debug error... can you post that? or better yet.. just send it to me email... [email protected] and I'll see if I can offer anymore help
Of all the things I've lost, I miss my mind the most.
-
October 27th, 2002, 05:27 AM
#7
You can also use strings and stringstreams
Code:
#include <iostream>
#include <string>
#include <sstream>
int main(void){
std::stringstream ss;
std::string str("0000000000");
int x = 0;
std::cout << "Enter a number" << std::endl;
std::cin >> x;
ss << x;
std::string strEnt = ss.str();
str.replace(str.length() - strEnt.length(),
strEnt.length(),strEnt);
std::cout << str << std::endl;
}
Of course you will want to add error checking, etc
-
October 27th, 2002, 09:19 AM
#8
Re: NEWBIE: converting int into string...
Originally posted by Silver Ghost
Ok, it's not like everywhere. What I want to get here is:
1. We have an int. For example 10.
2. We have a 10-byte string: "0000000000"
3. We want to place our int into that string, so it looks like this: "0000000010"
How would I do that?
try something like this
int i = 10;
char buf[11];
sprintf(buf, "%010d", i);
this works for me no need to reinvent the wheel and this will work for numbers with any amount of digits in it ie 110, 11000, etc and you dont need to start with a string set to all zero's
Last edited by ltachna; October 28th, 2002 at 09:10 PM.
-
October 28th, 2002, 07:09 AM
#9
In case it matters to anyone, itoa() isn't a implemented by most
ANSI C compilers. If you don't care about portability, then use it;
otherwise, stick to the sprintf() solution.
--Paul
-
October 28th, 2002, 02:24 PM
#10
homework help
just wanted to mention that I don't like to give a "best" case method when I feel I'm answering a homework post. I have two main reasons for this:
1) I'm selfish... learned to code on my own and....
2) I used to tutor C++ programming students in the New York states' college system callen SUNY. I found that more often than not, student where repremanded for using functions/libraries/routines beyond the scope of what the professor had presented (I always thought to be a reflection on the professors involved) but that is why I posted such a rudementary solution to the question at hand. All in all I can understand a teacher of a programming launguage (when its clearly an introductory course) dissallowing students from using code that clearly show they a) should have 'clepped the course' or b) got the solution from someone with more exerience.
Just thought I would mention this...
Of all the things I've lost, I miss my mind the most.
-
October 28th, 2002, 02:40 PM
#11
mfeik,
I agree with your stance on homework help. But I don't think that this particular question was a homework assignment. Well, I guess Silver Ghost can clear this up if he wants to
-
October 28th, 2002, 02:57 PM
#12
you could be right
You could be right. I just took it from the format of the question, the pluar tense, and from no apparent indication that it served a purpose other then to learn a bit about string manipulation to indicate it was a homework problem. Guess I should maybe reconsider how many assumptions I make and just provide the best answer (that I'm aware of) if I'm going to post at all.
I was just thinking that if I needed a similar routine for something I was working on I would post it in as generic a format as I could.
Of all the things I've lost, I miss my mind the most.
-
October 28th, 2002, 03:13 PM
#13
As a further clarification on my part, I wasn't trying to bash your
[mfeik's] answer at all and I hope that my post didn't imply that
the answer was unsatisfactory. I was just trying to give a
portable solution.
--Paul
-
October 28th, 2002, 04:06 PM
#14
to the contrary
Really, I didn't take any offense, and I was a glad to see your post. I cant even count the number of valid, pertinent info-bits picked up from comments like yours. I've learned a great many valid item that I didn't get out of books just from response like yours. 
-Mel
Of all the things I've lost, I miss my mind the most.
-
October 28th, 2002, 05:08 PM
#15
Okay, great. There are plenty of users who delight in trying to
belittle the posts of others; I am not such a user and I don't want
to come across as such. I, too, have seen a plethora of
informative posts in this forum; I've learned a lot from it.
--Paul
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|