| | |
convert integer to array of character???
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2007
Posts: 9
Reputation:
Solved Threads: 0
hi every body...
i have problem in my project,and i need help to solve it
the problem is ::
i have string variable and i want to store two values in it
one value is string that insert it by the user and another value is integer that is generated by the system...
the problem is in the integr value how can i append it to the string??
i think ,it must convert the integr and store it in array of characters ,then append the array to the string
see my code:::
that convert the integer into array of character the number consists three digits..
is it correct or not and what is the possible solutions???
thank you...
i have problem in my project,and i need help to solve it
the problem is ::
i have string variable and i want to store two values in it
one value is string that insert it by the user and another value is integer that is generated by the system...
the problem is in the integr value how can i append it to the string??
i think ,it must convert the integr and store it in array of characters ,then append the array to the string
see my code:::
that convert the integer into array of character the number consists three digits..
is it correct or not and what is the possible solutions???
thank you...
C++ Syntax (Toggle Plain Text)
#include <iostream> #include<string> #include<iomanip> using namespace std; int main() { char x[3]; int n,j=100,i=0; cout<<"Enter"; cin>>n; while(j>=1) { x[i]=n/j; n=n%j; j=j/10; i++; } return 0; }
Last edited by sunrise2007; Nov 11th, 2007 at 7:56 pm.
Use the stringstream class:
A string stream is like a file stream, but it works on a string instead of a file ;->
Get the string from the string stream with the str() member function (see line 17).
Hope this helps.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <sstream> using namespace std; int main() { stringstream ss; string user_input_string; int user_input_int; cout << "Please enter a word and a whole number: "; cin >> user_input_string >> user_input_int; cin.ignore( 10000 ); ss << "The word is \"" << user_input_string << "\" and the number is " << user_input_int << "."; cout << ss.str() << endl; return EXIT_SUCCESS; }
Get the string from the string stream with the str() member function (see line 17).
Hope this helps.
Last edited by Duoas; Nov 11th, 2007 at 9:46 pm.
•
•
Join Date: Nov 2007
Posts: 9
Reputation:
Solved Threads: 0
wooooooow:::
thank you Duoas so much...
it is great job ,
i really wander ,it is easy
but i have questions to fully understand the code;
the line 6 ::is it like declare a varible or pointer or what ?? then store the values inside it???
becouse ,when i print ss without str() i
see address ,what is that mean??
what is line 12,18 mean??
is there any different when i return 0 ??
---------------
i know ,i have stupied questions but,really i surprized!!!!
Thnak you for great help
thank you Duoas so much...
it is great job ,
i really wander ,it is easy
but i have questions to fully understand the code;
the line 6 ::is it like declare a varible or pointer or what ?? then store the values inside it???
becouse ,when i print ss without str() i
see address ,what is that mean??
what is line 12,18 mean??
is there any different when i return 0 ??
---------------
i know ,i have stupied questions but,really i surprized!!!!
Thnak you for great help
>the line 6 ::is it like declare a varible or pointer or what ??
It's an object definition. stringstream is a class, just like string.
>becouse ,when i print ss without str() i see address ,what is that mean??
It means that stringstream doesn't have an overloaded operator<< for stringstream, so cout treats it like a pointer and prints the address.
>what is line 12,18 mean??
The proper grammar is: "What do lines 12 and 18 mean?". Line 12 reads up to 10,000 characters or until a newline is found and throws them away. Line 18 returns success from main, obviously.
>is there any different when i return 0 ??
No.
It's an object definition. stringstream is a class, just like string.
>becouse ,when i print ss without str() i see address ,what is that mean??
It means that stringstream doesn't have an overloaded operator<< for stringstream, so cout treats it like a pointer and prints the address.
>what is line 12,18 mean??
The proper grammar is: "What do lines 12 and 18 mean?". Line 12 reads up to 10,000 characters or until a newline is found and throws them away. Line 18 returns success from main, obviously.
>is there any different when i return 0 ??
No.
I'm here to prove you wrong.
![]() |
Similar Threads
- Program reading in lines from a file. (each line individually) Please Help (C++)
- Convert integer to string (C++)
- Ordering an array (Java)
- Remove Letters from String to convert into integer (ASP.NET)
- MIPS - convert integer to floating point?? (Assembly)
- ascii to integer (C++)
- Integer to character conversion (Perl)
- Converting a String into an integer array (C)
- Help with folding a character array please (C)
Other Threads in the C++ Forum
- Previous Thread: rand() range help
- Next Thread: Tricky Sorting Algorthm Help
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






