Simple question about strings
Aug 9, 2014 at 5:50pm UTC
Hello, can someone please tell me what I need to do to make this work?
1 2 3 4 5 6 7 8 9 10 11 12
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
str.at(0) = 'A' ;
str.at(1) = '1' ;
cout << str;
return 0;
system("pause" );
}
Thank you for your time :)
Last edited on Aug 9, 2014 at 5:51pm UTC
Aug 9, 2014 at 6:02pm UTC
Line 6 creates an empty string.
Line 7 will fail because there is no existing character at index 0.
Line 8 will fail because there is no existing character at index 1.
1 2 3 4 5 6 7 8 9 10
#include <iostream>
#include <string>
int main()
{
std::string str;
str += 'A' ;
str += '1' ;
std::cout << str ;
}
Aug 9, 2014 at 6:07pm UTC
Thank you :)
Topic archived. No new replies allowed.