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...

#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;
}

Recommended Answers

All 4 Replies

Use the stringstream class:

#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;
  }

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.

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

>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 understand
Thank you Narue for your help and sorry for weak language grammer...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.