hmm, maybe you could use itoa()
It should take a number like 0 and turn it into "0" i think...
Then you could add it to the string...
Printable View
hmm, maybe you could use itoa()
It should take a number like 0 and turn it into "0" i think...
Then you could add it to the string...
Quote:
Originally Posted by stickman
Why not use the += plus/equals operator
instead of:
Code:blahblah=blahblah+a;
could be:
Code:blahblah += a;
The plus/equals operator is basically the string class's version of the cstring "strcat()" function.
The Brain's tip o' the day. :cool:
Sorry, I think my faq link was broken. Anyways check it out (I think the link works now). Here's code taken from the faq:
It's a nice little function you can use like this:Code:string IntToString(int num)
{
ostringstream myStream; //creates an ostringstream object
myStream << num << flush;
/*
* outputs the number into the string stream and then flushes
* the buffer (makes sure the output is put into the stream)
*/
return(myStream.str()); //returns the string form of the stringstream object
}
Be sure to #include <sstream>Code://...
int main()
{
std::string blah("Your number is: ");
int num=1;
blah+=IntToString(num);
}
well atoi() would return an integer:
alphanumeric to integer
You want itoa() which isn't standard.
It said that it had to few arguement and showed me that itoa() should be used like this:
itoa(int, char*, int)
Which that makes no sense to me. Where do I put variable a and what should I put for the other two arguements?
Yeah, that works, but it doesn't return the integer, it returns the character/symbol.Quote:
Originally Posted by The Brain
http://www.cplusplus.com/ref/cstdlib/itoa.htmlQuote:
Originally Posted by stickman
I was under the assumption that int a was already converted to string... once 'int a' becomes 'string a' then you can concantinate using plus/equals.
:)
>>Here is the link to the FAQ for an easy example of how to stream from int to string.
I guess I wasn't making it obvious enough...Or maybe no one can hear me :rolleyes:
Let me post here "right after" The Brains post, and not anyone else's, like JaWib, whom to my knowledge hasnt posted on this thread......
;)
Yes that FAQ article should be what your looking for.
since your using constant type, i believe your data already entered into the program, so why not use
[code]
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;
int main()
{
string mystring = "My words here!";
cout << mystring << endl;
mystring.insert(13, " \'more words\' ", 0, 14);
cout << mystring << endl;
getch();
}
[\code]
Output :
My words here 'more words' !
Here is a link for a refrence to insert, PDF Tutorial
I guess you guys just don't understand. I WANT IT TO RETURN AN INTEGER AND NOT A SYMBOL/CHARACTER. I tried itoa, returns symbol/character. I tried blah+=a, returns symbol/character. I want something that returns AN INTEGER. Let's do a test:
#include <iostream>
int main()
{
for (int x=0; x<10; x++)
{
char test;
std::cout << "\nThe Number Is: ";
std::cout << x;
}
std::cin.get();
return 0;
}
That returns:
The Number Is: 1
The Number Is: 2
The Number Is: 3
The Number Is: 4
The Number Is: 5
The Number Is: 6
The Number Is: 7
The Number Is: 8
The Number Is: 9
Now, how do I save "The Number Is: x" to a string so I can use it later?
P.S. Sorry if I'm going a little over edge, but nothing has been the correct output.
Here is the pseudocode for what ye' desire
1) covert 'int x' to 'string x' as per the example in the FAQ. I would probably pass int x into a function intTostring( ) for example.
Hint
where intTostring( ) will accept an integer argument.. and will have a string type return value.Code:
string result = "The number is: ";
result += intTostring(x);
2) Your "int to string" function will at least contain this syntax.. which will perform the actual conversion:
Hint
Code:
myStream << x << flush;
3) The only thing left.. is to pick your choice of data structure that will allow you to readilly store & access your new list of strings...
Hint
Code:
string answer[10];
for( int i = 0; i < 10; i++ )
answer[i] = result;
Hope this is somewhat close to what you are asking for :)
You must be tired, why don't you go to sleep and continue this conversation tomorrow? ;)
Code:#include <iostream>
#include <string>
#include <sstream>
std::string IntToString(int num)
{
std::ostringstream myStream; //creates an ostringstream object
myStream << num << std::flush;
/*
* outputs the number into the string stream and then flushes
* the buffer (makes sure the output is put into the stream)
*/
return(myStream.str()); //returns the string form of the stringstream object
}
int main()
{
std::string blah("Your number is: ");
std::string arr[10];
for (int x = 0; x <10; x++)
{
arr[x] = blah + IntToString( x );
}
for (int x = 0; x <10; x++)
{
std::cout << arr[x] << '\n';
}
return 0;
}
I'm sorry, I skipped this post. This is exactly what I wanted. Thanks to everyone who was so kind to help me out. Now I better get some rest. Thanks to all. Your help is much appreciated.Quote:
Originally Posted by JaWiB