Ok, so I am working on a project that changes:

cout << "this is a test script";
cin >> test

to

say ("hiya");
input (name);

It works fine but theres one problem, I don't want to code it into a complier like dev c++ I want to write it in something like notepad, save it then run a program that is in c++ that executes them lines. Is this possible and how? Thanks

Recommended Answers

All 8 Replies

So you want a program that generates a script, then executes a program that will interpret the script?

You need to work on how you ask things. Just like how your title has nothing to describe your predicament.

Sorry and yeah, something like that. I really don't know what they call it lol. Something like lua has when they open lua in c++ kinda thing. Any ideas?

what you need to do, is make the C++ program open the text file (argv), then read the file into a vector. Then interpret each line..... so you would have to write code that mimics this basic code idea:

for (int i=0; i < (int)somevec.size(); i++) {
     if (somevec[i].substr(0, 3) == "say") {
          // Processes "say" function.  You'll Need To Parse
          // The parenthesis and the quotes off of the typed in
          // say function: ie: say("hello") need to strip ( ) and " "
     }
}

Hi so something like:

if (line == "cout <<") {
   // change it to what i want it to do
 }

like that? I don't get the whole vector to file thing, sorry

Hi so something like:

if (line == "cout <<") {
   // change it to what i want it to do
 }

like that? I don't get the whole vector to file thing, sorry

I think I have to agree with Mosaic Funeral on this one. It's not clear at all, to me at least, what you are trying to do. How does this:

cout << "this is a test script";
cin >> test

get to this?

say ("hiya");
input (name);

What's the conversion from input to output? And what is "say" and "input"? Are they commands that the script will interpret?

Hey thanks for the reply, and yeah, basically all i'm trying to do is change

cout << "testing";
cin >> my_name;

to

say ("testing"); // will display on the screen
input (my_name); // allow the user to input something

It works when I use something like dev c++.

The thing that I want it to do is, let the user open up notepad and write:

say ("This is a line");

and run a program which then displays the "say" command in a dos box.

Is that any clearer? Sorry lol thanks :)

Hi so something like:

if (line == "cout <<") {
   // change it to what i want it to do
 }

like that? I don't get the whole vector to file thing, sorry

no, something like:

for (int i=0; i < (int)somevec.size(); i++) {
     if (somevec[i].substr(0, 3) == "say") {
          cout << somevec[i].substr(4, somevec.size()) << endl;
     }
}

This won't work 100% as you want, but you'll see what I meant by stripping off the ( ) and " "

Why don't you try this:

#include <iostream>

using namespace std;

int main(int argc, char **argv)
{
	string x = "say(\"Hello World\")";

	if (x.substr(0, 3) == "say") {
		cout << x.substr(4, x.length());
	}

	return 0;
}

Sounds like you are trying to build a parser. Not sure how familiar you are with them and whether you actually want to do one. Writing a parser is not trivial, but it may not be too bad for the limited vocabulary of your commands.

You need to decide exactly what assumptions you can make for input and what the grammar rules are as far as legal input and what should be done with it. Make life simple and assume one command per line, no spacing issues - i.e. you only have to worry about:

say("Hello")

not:

say  (  "Hello"   )

and every type of spacing someone might decide to use, including blank lines.

If you do that, you can take the approach suggested above. Look for say( . It will always be completely to the left of the line. You can use the find command from string to find the opening and closing quotes. Then take the substring between them . You haven't mentioned what you want to do with this:

input (my_name); // allow the user to input something
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.