943,936 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 12896
  • C++ RSS
Nov 11th, 2007
0

convert integer to array of character???

Expand Post »
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...
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include<string>
  3. #include<iomanip>
  4. using namespace std;
  5. int main()
  6. {
  7. char x[3];
  8. int n,j=100,i=0;
  9. cout<<"Enter";
  10. cin>>n;
  11. while(j>=1)
  12. {
  13. x[i]=n/j;
  14. n=n%j;
  15. j=j/10;
  16. i++;
  17. }
  18.  
  19.  
  20.  
  21. return 0;
  22. }
Last edited by sunrise2007; Nov 11th, 2007 at 7:56 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sunrise2007 is offline Offline
9 posts
since Nov 2007
Nov 11th, 2007
0

Re: convert integer to array of character???

Use the stringstream class:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <sstream>
  3. using namespace std;
  4.  
  5. int main() {
  6. stringstream ss;
  7. string user_input_string;
  8. int user_input_int;
  9.  
  10. cout << "Please enter a word and a whole number: ";
  11. cin >> user_input_string >> user_input_int;
  12. cin.ignore( 10000 );
  13.  
  14. ss << "The word is \"" << user_input_string
  15. << "\" and the number is " << user_input_int << ".";
  16.  
  17. cout << ss.str() << endl;
  18. return EXIT_SUCCESS;
  19. }
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.
Last edited by Duoas; Nov 11th, 2007 at 9:46 pm.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Nov 12th, 2007
0

Re: convert integer to array of character???

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sunrise2007 is offline Offline
9 posts
since Nov 2007
Nov 12th, 2007
0

Re: convert integer to array of character???

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 12th, 2007
0

Re: convert integer to array of character???

I understand
Thank you Narue for your help and sorry for weak language grammer...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sunrise2007 is offline Offline
9 posts
since Nov 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: rand() range help
Next Thread in C++ Forum Timeline: Tricky Sorting Algorthm Help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC