I made this little program just to get better understanding of dealing with strings.But i stuck in a small problem. Here is the code.
#include<iostream>
#include<string>
using namespace std;
string& add( string&x ){
string t; // <= Is this the problem???Declaring local string variable
cout <<"Size of String :" <<x.size() << endl;
for(int i=0; i<x.size();i++){
int n = x[i] - '0';
t[i] = n + 2 + '0';
}
for(int i=0;i<x.size();i++)
cout <<"t["<<i<<"]="<<t[i]<<endl; //This line is showing output as I wanted
cout <<"\nt = " << t << endl; // <=why the output of this line is blank?
cout <<"size of t="<<t.size() << endl; // <=and why the size of string t is zero?
return t;
}
int main(){
string a;
cin >> a ;
string b = add(a);
cout << "b =" << b << endl;
system("pause");
return 0;
}
I/p :123
o/p:
size of String :3
t[0]=3 t[1]=4 t[2]=5
t=
size of t = 0
b =
I am having problem with referencing the variable, passing the string as a reference and returning the string.. can anybody help me ??