0

我开始在 coderbyte 上做 C++ 挑战。第一个是:

使用 C++ 语言,让函数 FirstReverse(str) 接受传递的 str 参数并以相反的顺序返回字符串。

使用下面框中的参数测试功能使用不同的参数测试您的代码。

它为您提供了以下起始代码,然后您可以对其进行编辑和添加以创建程序:

#include <iostream>
using namespace std;

string FirstReverse(string str) { 

  // code goes here   
  return str; 

}

int main() { 

  // keep this function call here
  cout << FirstReverse(gets(stdin));
  return 0;

} 

我想出了以下几点:

#include <iostream>
using namespace std;

string FirstReverse(string str) {
    cout<<"Enter some text: ";
    cin>>str;
    string reverseString;

    for(long i = str.size() - 1;i >= 0; --i){
        reverseString += str[i];
    }

    return reverseString;

}

int main() {

    // keep this function call here
    cout << FirstReverse(gets(stdin))<<endl;
    return 0;

}

它给了我以下错误:“没有匹配的函数来调用gets”现在,为什么会发生这种情况,我该怎么做才能解决它?感谢您阅读本文,我们将不胜感激。

4

1 回答 1

3

gets方法在cstdio标头中声明。

尝试#include <cstdio>#include <stdio.h>

编辑 1:使用std::string
我推荐使用std::stringand std::getline

std::string text;
std::getline(cin, text);
std::cout << FirstReverse(text) << endl;
于 2016-02-03T23:12:29.830 回答