Trying to place code into a string.

Hello all. I am trying to place sections of code into a string and I would eventually like to cout the string and then place in a compiler, so I need all the characters to stay in place and I would like to keep it the same as it was written so it can be read later. I can not figure out how to break the lines correctly and read the whole string. If someone can help me . Thanks

P.S. also I can not figure out how to place the comments in the string.


#include <iostream>
#include <string>

using namespace std;

int main()
{
string txtbox;

txtbox = "#include \"engine.h\" \n
int main(int argc, char** argv) { \n
TWorld world; \n
TCamera cam; \n
Initialize(); \n
//Create a graphics context \n
Graphics(800,600); \n
//Create a world \n
world=CreateWorld(); \n
if (!world) { \n
MessageBoxA(0,\"Error\",\"Failed to create world.",0); \n
goto exitapp; \n
} \n
//Create render buffers \n
TBuffer gbuffer=CreateBuffer(800,600,BUFFER_COLOR|BUFFER_DEPTH|BUFFER_NORMAL); \n
TBuffer lightbuffer=CreateBuffer(800,600,BUFFER_COLOR|BUFFER_DEPTH); \n
//Create a camera \n
cam=CreateCamera(); \n
CreateListener(cam);";


cout << txtbox << endl;
return 0;
Instead of cout'ing the code, why don't you save it to a file. Then shell execute a compiler, passing the filename to it when you start it. Why do want to make a program this way though?
I am looking to make a simple gui to play around with. I would like to cin the code in sections and create just one string. Every time I cin I will += add to the string. At the end of the gui cout all of the code in one string all together.
You mean you are trying to make an IDE in the console that you will use to make your gui? Or are you just trying to execute code inputted by the user?
yep, just trying to execute code inputted by user.
Instead of getting code from the user, saving it to a file, then running a compiler, why don't you just make a small scripting language for whatever you need? Or just have a set of predefined commands?
I am just learning and if I could figure out how to break the lines in the string and allow the comments in the string it should be as simple as a hello world script. As I said I am only looking to do some low level stuff.

What am I doing wrong with the newline ?

Edit: also the cout of the string will not be used in the same compiler. just copy and paste when done.
Last edited on
To have quotes span multiple lines, do this:
1
2
string text = "Hello \
                      World!";


You just use '\' instead of '\n'.
Thanks for your replies. I guess I am not explaining my self very well. I am trying to have the output of the string look identical as the input. So then once done adding to the string I can cout, then select all and copy, then I can take all of the sections of code that I put together in the gui and paste it in a different compiler. That way when I paste in the other compiler I can read it like regular code.


If I do this it will cout the string across the screen. I want it to be like it was typed.

#include <iostream>
#include <string>


using namespace std;

int main()
{
string txtbox;

txtbox = "abcdefg\
hijk\
lmnop\
qrs\
tuv\
wxyz";
cout << txtbox << endl;
return 0;
}
What's the problem with '\n' for linebreaks?

1
2
3
string txtbox = "line1\nline2";

cout << txtbox << endl; // prints 2 lines as you'd expect 


If you don't want to cram everything on 1 line like that:

1
2
3
4
5
string txtbox = 
"line1\n"
"line2";

cout << txtbox << endl;


EDIT note that each line begins and ends with quotes, but there is no semicolon or comma or + or anything between lines.
Last edited on
Like this?
1
2
3
4
5
6
7
8
9
10
11
string total, input;

while (true){
    getline(cin, input);

    if (input=="done")
    break;
    else
    total += input;}

cout << total;
Thanks Disch Thats what I was looking for. I did not remember I could use quotation marks on your line three to keep the string going. Sorry for confusing everybody.
Thanks again Modshop.

This is what I was trying to do.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string txtbox;

	txtbox = "#include \"engine.h\"\n" 
"int main(int argc, char** argv) {\n"
"TWorld world;\n"
"TCamera cam;\n"
"Initialize();\n"
"//Create a graphics context\n"
"Graphics(800,600);\n"
"//Create a world\n"
"world=CreateWorld();\n"
"if (!world) {\n"
"MessageBoxA(0,\"Error\",\"Failed to create world.\",0);\n"
"goto exitapp;\n"
"}\n"
"//Create render buffers\n"
"TBuffer gbuffer=CreateBuffer(800,600,BUFFER_COLOR|BUFFER_DEPTH|BUFFER_NORMAL);\n"
"TBuffer lightbuffer=CreateBuffer(800,600,BUFFER_COLOR|BUFFER_DEPTH);\n"
"//Create a camera\n"
"cam=CreateCamera();\n"
"CreateListener(cam);";

	
	cout << txtbox << endl;
	return 0;

}
Topic archived. No new replies allowed.