convert integer to array of character???

Reply

Join Date: Nov 2007
Posts: 9
Reputation: sunrise2007 is an unknown quantity at this point 
Solved Threads: 0
sunrise2007 sunrise2007 is offline Offline
Newbie Poster

convert integer to array of character???

 
0
  #1
Nov 11th, 2007
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...
  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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: convert integer to array of character???

 
0
  #2
Nov 11th, 2007
Use the stringstream class:
  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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 9
Reputation: sunrise2007 is an unknown quantity at this point 
Solved Threads: 0
sunrise2007 sunrise2007 is offline Offline
Newbie Poster

Re: convert integer to array of character???

 
0
  #3
Nov 12th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,541
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: convert integer to array of character???

 
0
  #4
Nov 12th, 2007
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 9
Reputation: sunrise2007 is an unknown quantity at this point 
Solved Threads: 0
sunrise2007 sunrise2007 is offline Offline
Newbie Poster

Re: convert integer to array of character???

 
0
  #5
Nov 12th, 2007
I understand
Thank you Narue for your help and sorry for weak language grammer...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC