+ 6

What's the problem?

Hey Guys!! I am using at function of string to assign values. But I am getting an error. Here is code 👇👇 #include <iostream> #include<string> using namespace std; int main() { string str1="hello"; string str2; str2.at(1)=str1.at(0); cout<<str2.at(1); return 0; } Can anyone tell me about this error. I have no Idea of it. link to code:- https://code.sololearn.com/caZ3as5bdJr6/?ref=app

30th Jun 2018, 6:45 AM
Deepak Kumar
Deepak Kumar - avatar
4 Respostas
+ 1
using the resize method #include <iostream> #include<string> using namespace std; int main() { string str1="hello"; string str2; str2.resize(str1.size()); str2.at(1)=str1.at(0); cout<<str2.at(1); return 0; }
30th Jun 2018, 7:26 AM
MO ELomari
MO ELomari - avatar
+ 5
Okay! So is there any other way to assign a string to a blank string using at function?
30th Jun 2018, 7:23 AM
Deepak Kumar
Deepak Kumar - avatar
+ 4
Mohamed ELomari thanks!
30th Jun 2018, 7:28 AM
Deepak Kumar
Deepak Kumar - avatar
+ 1
str2 is of length zero so you cant do at(1) maybe line 8 should say str2 = str1; then it be of length 5 so you can do the at(1)
30th Jun 2018, 7:14 AM
Neil Dolling
Neil Dolling - avatar