I've wrote some code about arrays and strings, which I'm not comfortable working with(I'm a newbie). This bug is a runtime issue, since the compiler didn't yell at me when this program compiled. I'm totally stuck about this, and don't know why this happened.
# include<iostream>
# include<cstdio>
# include<cstring>
# include<string>
using namespace std;
int main() {
char in[100];
gets(in);
int len = strlen(in);
std::string s(in);
int count = 0;
for (int i = 0; i < len; i++) {
if (s.at(i) == ' ') {
count += 1;
}
}
int i = 0;
char rec[100];
for (int j = 0; j < len; j++) {
if (s.at(j + 1) != ' ') {
i += 1;
rec[i] = s.at(j);
} else {
i += 1;
rec[i] = s.at(j);
}
}
for (int m = 0; m < i; m++) {
cout << rec[m];
}
//cout << count;
}
Suppose the user input is "Hello World" (without the quotes). It was supposed to return "Hello", but instead got a error saying the following:
terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::at
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
and what followed was a popup to report to the Microsoft team.