From the given code below everytime i need to print the name as rama+value of the integer variable.

 if i=10;

then the output:rama10 should be printed... i have probelm to add string to the integer variable..please help anyone...thanks in advacne...

class abc
{
 private:
         int  a,i;
        string name;    

 public:
     void setvalues(int i) //set values of the data members of the abc class.
     {
       a=1; covalent_radius=1.6;
       name="rama"+i;
       cout<<name<<endl;
         }
}; 

Recommended Answers

All 9 Replies

change this....

name="rama"+i;
cout<<name<<endl;

to this just this...

cout << name << i << endl;

change this....

name="rama"+i;
cout<<name<<endl;

to this just this...

cout << name << i << endl;

i dont want to print like that..i want give names in that way..rama1,rama2,rama3...like that..

The suggestion offered you does exactly what you want.

int i = 3;
string s = "pineapple";

cout << s << i;

produces

pineapple3

your reply is confusing...

this...

string name("rama");
for (int ii = 1; ii <= 5; ++ii)
cout << name << ii << endl;

produces this output...

rama1
rama2
rama3
rama4
rama5

is this not the output you desire?
or is it the method of how this output is being produced?

btw, thank you Duoas for reinforcing my previous post.

Yes..i want the method to get that result....simply ...

string a,result;
int i;

if suppose i hase particular value than how can i add this i variable to the string

result=a+i;
can i add like this to get the result...

if a="rama" and i=10....then the result should be result=rama10...i want to use this result variable somewhere else...thanks for ur time and help...

your reply is confusing...

this...

string name("rama");
for (int ii = 1; ii <= 5; ++ii)
cout << name << ii << endl;

produces this output...

rama1
rama2
rama3
rama4
rama5

is this not the output you desire?
or is it the method of how this output is being produced?

oh, try this...

string name;
int ii = 123;
char buf[100];

name = "rama";
name.append(itoa(ii, buf, 10));

cout << name << endl;

this will produces this...

rama123

... and the string variable can be used elsewhere...

Or if you are feeling particularly C++ish, do it thus:

#include <sstream>
#include <string>

...

string catnum( const string& str, int num )
  {
  stringstream ss;
  ss << str << num;
  return ss.str();
  }

...

string rama1 = catnum( "rama", 1 );
string rama12 = catnum( rama1, 2 );

cout << "rama1  = \"" << rama1 << "\"\n"
     << "rama12 = \"" << rama12 << "\"\n";

Hope this helps.

Hi it works now..thanks for ur help..all the best..

oh, try this...

string name;
int ii = 123;
char buf[100];

name = "rama";
name.append(itoa(ii, buf, 10));

cout << name << endl;

this will produces this...

rama123

... and the string variable can be used elsewhere...

Hi it works now..thanks for ur help..all the best..

oh, try this...

string name;
int ii = 123;
char buf[100];

name = "rama";
name.append(itoa(ii, buf, 10));

cout << name << endl;

this will produces this...

rama123

... and the string variable can be used elsewhere...

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.