#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main()
{
//YOUR_CODE
string word;
cout << "enter a word: ";
getline(cin, word);
int middle = word.length();
middle = middle/2;
if(middle % 2 == 0)
{
middle = word.length();
middle = (middle/2)+1;
floor (middle);
}
int last = word.length();
cout << word[0] << word.at(middle) << word.at(last);
}
我不确定为什么 getline 不工作。检查我正在使用的书,调试器没有帮助。有谁知道它有什么问题?在调试器中,它显示值:<不可访问>。任何帮助表示赞赏。
最佳答案
word
的赋值没有问题。问题是 word.at(last)
。字符串位置从 0
到 length-1
(就像数组一样)。由于 last
是 word.length()
,您正试图访问该范围之外的字符。尝试:
last = word.length()-1;
关于c++ - getline 没有为字符串对象赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21640475/