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?
Printable View
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?
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;
}
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
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?
Hmm.. If I dont declare the first two buffers as a dynamic memory it works fine. Interesting.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;
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
You can also use strings and stringstreams
Of course you will want to add error checking, etcCode:#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;
}
try something like thisQuote:
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?
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
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
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...
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 ;)
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.
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
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
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