Compiling my scripting language - c++

I have developed a frame work for a scripting language and I am trying to make a compiler for it to output onto a certain file a custom Hex code to be read by an interpreter for applications such as games and movies. The interpreter will read the hex and perform operations via loops and pretty much do the hard work for me. But I need a way to compile the strings of text into my custom hexadecimal. Here is an example of my compiler.
#include <iostream>
#include <string>
#include <Windows.h>
#include <wincon.h>
#include <fstream>;
using namespace std;
int main(){
char egScriptpath[999];
char path[999];
char title[999];
ifstream readfile;
ofstream myfile;
int optn;
cout << "Enter the path where your *.egSCRIPT file is: " << endl;
cin.getline(egScriptpath,999);
cout << "Enter your path where you want to compile your *.egSCRIPT file: " << endl;
cin.getline(path,999);
cout << "Enter your title for your program: ";
cin.getline(title,999);
cout << endl << "Enter for your option of file extention:" << endl;
cout << " Enter 1 for .egGame :" << endl;
cout << " Enter 2 for .egMovie: ";
cin >> optn;
if(optn==1){
readfile.open((string)egScriptpath);
ofstream egMovieFile((string)path+"\\"+(string)title+".egGame");
myfile.open((string)path+"\\"+(string)title+".egGame");
while(!readfile.eof()){
if(readfile.getline("validated()",1)){
myfile << " \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n"
" \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n"
" \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n"
" ////////////////\n"
" ////////////////\n"
" ||||||||||||||||\n"
" ||||||||||||||||\n"
" ||||||||||||||||\n"
" \1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\n"
" \2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\n";
}
}
myfile.close();
readfile.close();
}else if(optn==2){
readfile.open((string)egScriptpath);
ofstream egGameFile((string)path+"\\"+(string)title+".egMovie");
myfile.open((string)path+"\\"+(string)title+".egMovie");
while(!readfile.eof()){
if(readfile.getline("validated()",1)){
myfile << " \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n"
" \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n"
" \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n"
" ////////////////\n"
" ////////////////\n"
" ||||||||||||||||\n"
" ||||||||||||||||\n"
" ||||||||||||||||\n"
" \1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\n"
" \2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\n";
}
}
myfile.close();
readfile.close();
}
cout << "Compile complete" << endl;
system("pause");
return 0;
}
So what I want to do is in the if statement where it says if(readfile.getline("validated()",1)){} I want it to write onto the "myfile" ofstream variable the text that you can see just for an example. I hope I am clear on what I am saying. If the compiler reads "validated()" on my script file, then it will write the "hexadecimal" code onto the new file with the variable of "myfile" which will in return be interpreted by the interpreter for applications such as games and movies. Thanks if you know what is wrong and why it is not writing the hex code onto the new file that it makes.

Looks like your problem is located here:
if(readfile.getline("validated()",1)){
Sincerely, I don't even know why the compiller allows you to do so, but I would like to suggest to you the following change:
instead of:
while(!readfile.eof()){
if(readfile.getline("validated()",1)){
use this one:
std::string buffer;
while (std::getline(readfile, buffer, '\n')){
if (buffer == "validated()")
also, since you are using C++, instead of use arrays of char, you should relly more on std::string class.

Related

Possible to output console result into text file?

I am required to store the below console data into a text file:
enter image description here
However, the name of the textfile of where the output will be placed in is typed in by the user. I am unable to find a proper way to store all the console data in the requested text file. Since I use width and special spaces I am unsure how to go about doing this.
My Code:
if(message == "5"){
cout << "\n[View Data...]" << endl;
cout << "filtering criteria:" << optionTwo << endl;
cout << "sorting criteria:" << optionThree << endl;
cout << "sorting order:" << optionFour << endl;
cout << "" << endl;
if(optionTwo == "Point2D"){
std::cout.width(5); std::cout << std::right << "X";
std::cout.width(6); std::cout << std::right << "Y";
std::cout.width(9); std::cout << std::right << "Dist.";
std::cout.width(3); std::cout << std::right << "Fr" ;
std::cout.width(7); std::cout << std::right << "Origin" << endl;
}
}
if(message == "6"){
cout << "Please enter filename: " ;
cin >> message6;
}
When you are sending text into cout you are basically writing to a file already, just a system standard one called stdout. You simply need to open a different output file stream and send the output the same way.
#include <iostream>
#include <fstream>
using namespace std;
ofstream out = ofstream("file.txt", ios::out);
out.width(5);
out << std:;right << "X";
You do not strictly need to include the ios::out since it is the default mode for ofstream objects. You can read more at the cppreference
You can use filestreams for this:
std::cin >> message6; // read file name from the user
std::ofstream file {message6}; // open file with that name
file << "hello world"; // write data to the file
Similar to writing data to cout, you can use stream modifiers for file as well.
I don't know what kind of string you're using but, you can use the std::ostream for that:
ofstream myfile;
cout << "Please enter filename: " ;
cin >> message6;
myfile.open (message6);
Then instead of using cout, you'd use:
myfile << "Your string.\n";
But if you want to redirect he actual standard output of the program to a file. You can do it through calling arguments. And that can be set in the execution panel in your IDE or the terminal when the program is called
your_program >> output_file.txt

compiling is okay but my program doesn't really work

#include <iostream>
#include <string.h>
using namespace std;
int main()
{
while (1)
{
char name1[100];
char adrs1[100];
char rsn1[100];
char XXXXX[100];
cout << "input personal information" << '\n';
cout << "patient 1" << '\n';
cout << "input the name of the patient" << '\n';
cin.getline (name1,100);
cout << "input the address of the patient" << '\n';
cin.getline (adrs1,100);
cout << "input the reason" << '\n';
cin.getline (rsn1,100);
cout << "input the name of the patient" << '\n';
cout << "if you want to exit, input exit" << '\n';
cin.getline (XXXXX,100);
if (XXXXX==name1)
cout << adrs1[100] << rsn1[100] << '\n';
else (XXXXX=="exit");
break;
return 0;
}
}
that's my program, and compiling is okay. but when i start the program, it doesn't print any rsn or adrs, it just ends.
I want it to print rsn and adrs when it reads names.
Help me please
There are quite a few errors in your program.
The most important one is that you are trying to write an infinite loop. But it runs exactly once. You need to move your return statement out of the loop.
There is no need for a conditional statement for an else block. You can remove it along with the semi colon.
You're trying to print a character at the index 100 which goes out of bounds.
I don't know what XXXXX is supposed to be. May be you missed pasting the declaration on this website.
At this point, I really suggest picking up a book or trying to debug your code by going step-by-step through your code. It would be more helpful to you at this stage in your learning than this website,
To complete the answer above, the correct program would be:
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
while (1)
{
char name1[100];
char adrs1[100];
char rsn1[100];
char XXXXX[100];
cout << "input personal information" << '\n';
cout << "patient 1" << '\n';
cout << "input the name of the patient" << '\n';
cin.getline (name1,100);
cout << "input the address of the patient" << '\n';
cin.getline (adrs1,100);
cout << "input the reason" << '\n';
cin.getline (rsn1,100);
cout << "input the name of the patient" << '\n';
cout << "if you want to exit, input exit" << '\n';
cin.getline (XXXXX,100);
if (strcmp(XXXXX,name1) == 0)
cout << adrs1 << rsn1 << '\n';
else /*(XXXXX=="exit");*/
break;
//return 0;
}
}
You forgot to initilize the name1 variable, you can initialize it using char name1[100] = {};
You cannot directly compare the if (XXXXX==name1), use can use the strncmp function for the same. I will prefer the string class instead of char pointer. Use the following:
if (!strncmp(XXXXX,name1,100))
cout << adrs1 << rsn1 << '\n';
else if (!strncmp(XXXXX,"exit",100))
break;

String phrase ends at space?

I am wringing a simple code to learn more about string. When I ran my code it would not print my last name. Can someone explain why? I used string phrase to store it and it only appears to have stored my first name. Here is the code.
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
cout << "Exercise 3B" << endl;
cout << "Kaitlin Stevers" << endl;
cout << "String arrays" << endl;
cout << endl;
cout << endl;
char greeting[26];
cout << "Please enter a greeting: " << endl;
cin >> greeting;
cout << "The greeting you entered was: " << greeting << endl;
string phrase;
cout << "Enter your full name " << endl;
cin >> phrase;
cout << greeting << ", how are you today " << phrase << "?" << endl;
return 0;
}
I used string phrase to store it and it only appears to have stored my first name.
That makes sense.
cin >> phrase;
will stop reading when it encounters a whitespace character in the input.
To read the full name you can use one of the following approaches.
Use two calls to cin >>.
std::string first_name;
std::string last_name;
cin >> first_name >> last_name;
Use getline to read the entire line. getline will read everything in a a line, including whitespace characters.
getline(cin, phrase);
When you call cin >> phrase;, it only reads the string up to the first non-space character. If you want to include spaces in your name, best goes with getline(cin,phrase);.
IMPORTANT: getline() will reads whatever it is in the stream buffer up to the first \n. It means that when you enter cin >> greeting;, if you hit ENTER, getline() will read everything before that \n that is not already read, which is NOTHING into your phrase variable, making it an empty string. An easy way out is to call getline() twice. E.g.
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
cout << "Exercise 3B" << endl;
cout << "Kaitlin Stevers" << endl;
cout << "String arrays" << endl;
cout << endl;
cout << endl;
char greeting[26];
cout << "Please enter a greeting: " << endl;
cin >> greeting; //IMPORTANT: THIS ASSUME THAT GREETING IS A SINGLE WORD (NO SPACES)
cout << "The greeting you entered was: " << greeting << endl;
string phrase;
cout << "Enter your full name " << endl;
string rubbish_to_be_ignored;
getline(cin,rubbish_to_be_ignored); //this is going to read nothing
getline(cin, phrase); // read the actual name (first name and all)
cout << greeting << ", how are you today " << phrase << "?" << endl;
return 0;
}
Assuming you store that code in the file stackoverflow.cpp. Sample run:
Chip Chip#04:26:00:~ >>> g++ stackoverflow.cpp -o a.out
Chip Chip#04:26:33:~ >>> ./a.out
Exercise 3B
Kaitlin Stevers
String arrays
Please enter a greeting:
Hello
The greeting you entered was: Hello
Enter your full name
Kaitlin Stevers
Hello, how are you today Kaitlin Stevers?
Tested on ubuntu 14.04

Cygwin "using: command not found" for C++ program

Hoping to free myself from Eclipse and not wanting to keep using the online cpp.sh, I wrote a small program in Cygwin in nano and tried to run it. The code is included for clarity.
#include <iostream>
#include <string>
using namespace std;
int main()
{
int i;
string mystr;
cout << "Enter number: ";
cin >> i;
cout << "You entered: " << i;
cout << " and its double: << i*2 << ".\n";
cin.ignore();
cout << "Name: ";
getline(cin, mystr);
cout << "Hello " << mystr << ".\n";
cout << "Team? ";
getline(cin, mystr);
cout << "Go " << mystr << "! \n";
return 0;
}
Trying to run it returns a series of errors, as seen in the picture. Right now, I'm trying to understand why "using" is not recognized. Checking Google found many similar complaints, but never about the command "using," probably because "using" is a common enough word to be using in a different context.
You can't run source code directly. You must compile it first.
Well, a lot of g++ compile errors are because you have
cout << " and its double: << i*2 << ".\n";
when you probably meant
cout << " and its double: " << i*2 << ".\n";
with one more quotation mark
As soon as you insert the " into line 14 it will compile and run, but the last portion will not complete.
I would also create another string near mystr, and use it for getline(). Reusing mystr faults when I compile it.
int main()
{
int i;
string mystr;
string team;
`getline(cin, team);
cout << "Go " << team << "! \n";`
After changing and compiling, I get:
Enter number: 3
You entered: 3 and its double: 6.
Name: Aaron
Hello Aaron.
Team? Meow
Go Meow!

outFile C++ not writing to output text

Down below is my incomplete program. I am having problems with writing to a text file. For example I want to write the number of snow days to a text file, but nothing shows up in the textfile when I debug in VS 2010. It does display my info and name, but nothing else works. It wont write anything after that. its NOT writing to a text file.
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
const string INFORMATION = "College Class";
const string MY_NAME = "Tom Hangler";
int main(void)
{
ofstream outFile;
int numberOfSnowDays;
int greatestSnowDay;
int dayNumber;
double amounttOfSnow;
outFile.open("Ex1Out.txt");
outFile << setw(51) << INFORMATION << endl << setw(48) << MY_NAME << endl;
cout << "Please enter num of days it snowed: " << endl;
cin >> numberOfSnowDays;
outFile << setw(10) << "Number of days of snow is: " << setw(10) << numberOfSnowDays;
int index;
//Problem 1 for-loop
for (index = 0; index < numberOfSnowDays; index++)
{
cout << "Enter day: " << endl;
cin >> dayNumber;
cout << "Enter amount of snow: " << endl;
cin >> amountOfSnow;
};
return 0;
}
here is what my output displays:
College Class (centered)
Tom Hangler (centered)
If i try to write anything after this, Nothing is written ever to the output file. And the output text file IS in my VS project that contains my .cpp file. I added the text file to project.
Try closing the stream at the end of the function, it looks like the data isn't getting flushed.
outFile.close();
Your code compiles and works on gcc 4.4.5 (apart from typo in amounttOfSnow).
Is it possible that you are looking at an old Ex1Out.txt file ?
Its most likely created in the Release or Debug subdirectory in your project, not where the .cpp files are.
in your for loop, you only collect the amount of snow, but you don't write it to the text file.
Do you want to do something like this?
...
for (index = 0; index < numberOfSnowDays; index++)
{
cout << "Enter day: " << endl;
cin >> dayNumber;
cout << "Enter amount of snow: " << endl;
cin >> amountOfSnow;
// next line is new:
outFile << "Day#: "<< dayNumber << ", snow: "<< amountOfSnow<<endl;
};
outFile.close()
...