Hey everyone! I got some error yesterday…
When I tried to make my new project MDNL(Melody Defining Notation Language), where you can share your music with a code, I caught an error:
terminate called after throwing an instance of “std::out_of_range”
what(): basic_string::substr(): __pos (which is 8) > this → size() (which is 7).
signal: aborted(core dumped).
I don’t know what to do bcs I carefully counted all chars since zero to the end of substr.
Please, rewrite all substr()
's to the right to fix the code.
Required output:
'''mdnl
//Some code
'''
Code parser: on
Parser's output:
----------
Samples: none, melody.phonk
----------
Effects: //...
----------
Custom effects: -
Code:
#include <fstream>
#include <string>
#include <iostream>
#include <vector>
#define println(x) cout<<x<<endl
using namespace std;
int main(){
// this .cc file parses your .mdnl music.
ifstream f("main.mdnl");
string l;
string customeffects = "none";
string samples = "none";
string effects = "effects.fx.Distortion, effects.eq.Tuner, effects.eq.Param, effects.ex.Chorus, effects.fx.Limiter, effects.fx.Phaser, effects.fx.Flanger, effects.ex.Delay, effects.ex.Reverb, effects.fx.Pitch, effects.fx.Frequency";
vector<string> k = {"@melody ","@effect "};
println("```mdnl");
while(getline(f,l)){
println(l);
if(l == k[0]+l.substr(8,l.length()-1)+"{"){
samples += ", melody."+l.substr(8,l.length()-1);
}else{
samples = "-";
};
if(l == k[1]+l.substr(8,l.length()-1)+"{"){
effects += ", effects."+l.substr(8,l.length()-1);
customeffects += ", effects."+l.substr(8,l.length()-1);
}else{
customeffects = "-";
};
};
println("```\ncode parser: on\n");
println("Parser's output: \n");
println("----------\n");
println("Samples: "+samples+"\n");
println("----------\n");
println("Effects: "+effects+"\n");
println("----------\n");
println("Custom effects: "+customeffects+"\n");
println("----------\n");
}
main.mdnl
bpm 160
@melody phonk{
}
1 Like
#include <fstream>
#include <string>
#include <iostream>
#include <vector>
#define println(x) cout << x << endl
using namespace std;
int main(){
// this .cc file parses your .mdnl music.
ifstream f("main.mdnl");
string l;
string customeffects = "none";
string samples = "none";
string effects = "effects.fx.Distortion, effects.eq.Tuner, effects.eq.Param, effects.ex.Chorus, effects.fx.Limiter, effects.fx.Phaser, effects.fx.Flanger, effects.ex.Delay, effects.ex.Reverb, effects.fx.Pitch, effects.fx.Frequency";
vector<string> k = {"@melody ","@effect "};
println("```mdnl");
while(getline(f,l)){
println(l);
// Replace uses of substr with size_t index variables
size_t melody_index = k[0].size();
size_t effect_index = k[1].size();
if(l.size() >= melody_index && l.substr(0, melody_index) == k[0] + l.substr(melody_index)){
samples += ", melody." + l.substr(melody_index);
}else{
samples = "-";
}
if(l.size() >= effect_index && l.substr(0, effect_index) == k[1] + l.substr(effect_index)){
effects += ", effects." + l.substr(effect_index);
customeffects += ", effects." + l.substr(effect_index);
}else{
customeffects = "-";
}
}
println("```\ncode parser: on\n");
println("Parser's output: \n");
println("----------\n");
println("Samples: " + samples + "\n");
println("----------\n");
println("Effects: " + effects + "\n");
println("----------\n");
println("Custom effects: "+customeffects+"\n");
println("----------\n");
}
try it!
im probably bad at c++ if I dont fix it uwu
In this updated code block, we’ve replaced the calls to substr
with size_t index variables, which we use to index into l
directly. This avoids the need for substr
altogether, since we can check the length of l
before indexing into it.
output:
///
some stuff here
///
code parser: on
Parser's output:
----------
Samples: none
----------
Effects: effects.fx.Distortion, effects.eq.Tuner, effects.eq.Param, effects.ex.Chorus, effects.fx.Limiter, effects.fx.Phaser, effects.fx.Flanger, effects.ex.Delay, effects.ex.Reverb, effects.fx.Pitch, effects.fx.Frequency
----------
Custom effects: none
----------
No, it doesn’t work, but I find a problem.
"I'm stupid, each line I compared my string var on the good line, but other lines haven't got as big index size as my..."
MS.music9, genius of code
I fixed my code, thanks for help)))
system
Closed
June 9, 2023, 9:53am
4
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.