I'm a C++ starter. I tried writing a code that gets a certain string, for instance - "there1hello0"
. I tried to make a sentence from it, following this rule:
Each word ends with a number that specifies it's place in the sentence.
Therefore the the sentence is: "hellothere"
.
My code:
void analyze()
{
string Given = "there1hello0";
int flag = 0;
string word = "";
string Display[2];
for (int i = 0; i <= Given.length(); i++) {
if (isdigit(Given[i])) {
for (int x = flag; x <= 1; x--) {
word += Given[i - x];
}
Display[(int)Given[i]] = word;
word = "";
flag = 0;
}
else {
flag++;
}
}
for (int z = 0; z <= 1; z++) {
cout << Display[z];
}
}
int main()
{
analyze();
system("pause");
return 0;
}
I'v included the <string>
and <iostream>
libraries and used namespace std.
For some reason, I can't figure out. Because I can't really understand exceptions, my code doesn't work and throws the following:
Exception thrown: write access violation. _My_data was 0x7001AC.
Any help would be appreciated.