【发布时间】:2021-09-23 00:36:05
【问题描述】:
我编写了一些代码,需要使数组的长度与用户输入的长度相同:
#include <iostream>
#include <string>
using namespace std;
void abrev(string word) {
int lastChar = word.length();
if (lastChar > 10) {
cout << word[0];
cout << lastChar - 2;
cout << word[lastChar - 1] << endl;
} else {
cout << word << endl;
}
}
int main() {
int n;
cin >> n;
string words[n];
for (int i = 0; i <= n - 1; i++) {
cin >> words[i];
}
for (int i = 0; i <= n - 1; i++) {
abrev(words[i]);
}
return 0;
}
我真的不知道我能做什么,我没有想法。我使用的是compiler,它只是解决了这个问题,所以直到我将此代码提交给 codeforces.com 时我才意识到这一点,其中出现了这些错误:
Can't compile file:
program.cpp
program.cpp(20): error C2131: expression did not evaluate to a constant
program.cpp(20): note: failure was caused by a read of a variable outside its lifetime
program.cpp(20): note: see usage of 'n'
program.cpp(23): warning C4552: '>>': operator has no effect; expected operator with side-effect
另外我不认为最后一个错误与它有任何关系,如果你能帮助解决这个问题,那就太棒了! 感谢您的帮助!
【问题讨论】:
-
std::vector<std::string> words(n); -
“我真的不知道我能做什么”-也许谷歌搜索“c++ vla Alternative”之类的东西,然后找到
std::vector -
使用
std::vector作为近乎直接的替代品
标签: c++