Who can fix this big C++ code?
#include <fstream>
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <math.h>
#define println(x,y) if(y){cout<<x<<endl;}else{cout<<x;}
using namespace std;
class Iggy{
public:
Iggy(){};
void createVar(string name,vector<string> val){
vars["$"+name] = val;
};
void createVar(string name,string val){
vars["$"+name] = val;
};
void writef(string ln){
println(ln,false)
};
void writeln(string ln){
println(ln,true);
};
vector<string> getVar(string name){
return vars[name];
};
void createFunc(string name,vector<string> params){
funcs[name] = params;
}
void createFunc(string name){
funcs[name] = "void";
};
private:
map<string,vector<string>> vars,funcs;
}
int main(){
ifstream f("main.iggy");
string l;
Iggy i;
while(getline(f,l)){
try{
//----------------------
if(l == l.substr(0,l.find(" = "))+" = "+l.substr(l.find(" = ")+3)){
i.createVar(l.substr(0,l.find(" = ")),l.substr(l.find(" = ")));
};
//----‐-----------------
if(l == "writef "+l.substr(7)){
i.writef(l.substr(7));
};
//---------------------
if(l == "writeln "+l.substr(8)){
i.writeln(l.substr(8));
};
//---------------------
if(l == "getline $"+l.substr(9)){
string *a = new string(" ");
cin >> a;
i.createVar(l.substr(8),a);
delete a;
};
//---------------------
}catch(out_of_range e){};
};
f.close();
}
Hey, is it because you missed the semicolon after print line here?
void writef(string ln){
println(ln,false)
};
PS: i do not know c++ so this may be wrong. Someone can correct me in that case…
1 Like
what is the output of the program supposed to be try this code:
#include <fstream>
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <math.h>
#define println(x, y) if (y) { cout << x << endl; } else { cout << x; }
using namespace std;
class Iggy {
public:
Iggy() {};
void createVar(string name, vector<string> val) {
vars["$" + name] = val;
};
void createVar(string name, string val) {
vars["$" + name].push_back(val);
};
void writef(string ln) {
println(ln, false);
};
void writeln(string ln) {
println(ln, true);
};
vector<string> getVar(string name) {
return vars[name];
};
void createFunc(string name, vector<string> params) {
funcs[name] = params;
}
void createFunc(string name) {
funcs[name] = vector<string>();
};
private:
map<string, vector<string>> vars, funcs;
};
int main() {
ifstream f("main.iggy");
string l;
Iggy i;
while (getline(f, l)) {
try {
//----------------------
if (l == l.substr(0, l.find(" = ")) + " = " + l.substr(l.find(" = ") + 3)) {
i.createVar(l.substr(0, l.find(" = ")), l.substr(l.find(" = ") + 3));
};
//---------------------
if (l == "writef " + l.substr(7)) {
i.writef(l.substr(7));
};
//---------------------
if (l == "writeln " + l.substr(8)) {
i.writeln(l.substr(8));
};
//---------------------
if (l == "getline $" + l.substr(9)) {
string a;
cin >> a;
i.createVar(l.substr(8), a);
};
//---------------------
}
catch (out_of_range e) {};
};
f.close();
return 0;
}
hello @NataliaKazakiev . Welcome to the forums!
I have corrected the code for you:
#include <fstream>
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <math.h>
#define println(x,y) if(y){cout<<x<<endl;}else{cout<<x;}
using namespace std;
class Iggy {
public:
Iggy(){};
void createVar(string name, vector<string> val) {
vars["$"+name] = val;
};
void createVar(string name, string val) {
vars["$"+name] = val;
};
void writef(string ln) {
cout << ln;
};
void writeln(string ln) {
cout << ln << endl;
};
vector<string> getVar(string name) {
return vars[name];
};
void createFunc(string name, vector<string> params) {
funcs[name] = params;
}
void createFunc(string name) {
funcs[name] = vector<string>{"void"};
};
private:
map<string,vector<string>> vars, funcs;
};
int main() {
ifstream f("main.iggy");
string l;
Iggy i;
while(getline(f, l)) {
try {
if(l == l.substr(0, l.find(" = "))+" = "+l.substr(l.find(" = ")+3)) {
i.createVar(l.substr(0,l.find(" = ")),l.substr(l.find(" = ")+3));
};
if(l == "writef "+l.substr(7)) {
i.writef(l.substr(7));
};
if(l == "writeln "+l.substr(8)) {
i.writeln(l.substr(8));
};
if(l == "getline $"+l.substr(9)) {
string a;
getline(cin, a);
i.createVar(l.substr(8), a);
};
} catch(out_of_range e) {};
};
f.close();
}
your code still shows the error
.
Are you using chatgpt again?
It works. Thanks, chatgpt!
No, its just that I have gwammar.
Because I am trash at C++ so grammar saves me.
Yes, you’re trash using chatgpt only to create good grammars. Bat mai gramars is eksilent!
Anyway, your code has only 1 error, very small error what I have already fixed only for 1 minute.
1 Like
system
Closed
11
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.