Hi there, I am new to C++, and was wondering if someone could write an example program for me to work with.

What I would like to do is take input from stdin and then display the translate output to stdout.



(color 0 0 0.1)  // These numbers can be any floating point numbers. 
                 // And there will always be parentheses.


0 0 0.1 setrgbcolor

I think learning how to do this will be enough to get me on my way.

Any help will be greatly appreciated.

I have managed to get started on the program.

Here is what my code looks like right now:

#include <iostream>
#include <sstream>
#include <string>
#include <stdlib.h>
using namespace std;

int main()
	string line;
	string word;
	stringstream iss;
	ostringstream oss;
	float r, g, b, lw;

	while(getline(cin, line))
		iss << line;
		iss >> word;
		if(word == "(color")
			iss >> r >> g >> b;
			oss << r << " " << g << " " << b << " setrgbcolor\n";
		else if(word == "(linewidth")
			iss >> lw;
			oss << lw << " setlinewidth\n";
	cout << oss.str();	
	return 0;

My question now is, how can I take in an input like this:

(color 0 0 0.1)(linewidth 2)

And get an output looking like this:

0 0 0.1 setrgbcolor
2 setlinewidth

So basically, how can I read the file one parentheses at a time?

You could try std::string::find (http://cplusplus.com/reference/string/string/find/) to find the first closing parenthesis, translate the first part, then go on.
So the code would look more or less like this:

string toTranslate;
int lastPos=0;
int size=toTranslate.size();
	int pos=toTranslate.find(')',lastPos);
	string current=toTranslate.substr(lastPos,lastPos-pos);
	// translate current here

Please debug it, as I wrote it in notepad without any checking.

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.