copying and swapping string problems

Hello I'm new to C++. I am learning from a book and I'm having trouble with copying and swapping strings. The code compiles okay but the .exe file wont run. instead it comes up with this in the command prompt..

terminate called after throwing an instance of 'std::length_error'
what(): basic_string::_S_create

This application has requested the run time to terminate it an unusual way.

Can someone please explain why this happens

Thank you

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <string>
#include <iostream>
using namespace std;


int main()
{
	string front;
	string back;
	string text =
	
	front.assign(text);
	cout<<endl<<"Front: "<<front<<endl;
	front.assign(text,0,27);
	cout<<endl<<"Front: "<<endl;
	back.assign(text,27,text.size());
	cout<<"Back: "<<back<<endl;
	back.swap(front);
	cout<<endl<<"Front: "<<front<<endl;
	cout<<"Back: "<<back<<endl;
		
	return 0;
}
What is that line 10-12 statement? It should not compile.


subpos in http://www.cplusplus.com/reference/string/string/assign/
sorry this is my fault I misread something in the book.

thanks for the help
Topic archived. No new replies allowed.