0

I started doing the C++ challenges on coderbyte. The first one is:

Using the C++ language, have the function FirstReverse(str) take the str parameter being passed and return the string in reversed order.

Use the Parameter Testing feature in the box below to test your code with different arguments.

It gives u the follow starting code, which you then edit and add to to create the program:

#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;

} 

I have come up with the following:

#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;

}

It gives me the following error: "No matching function to call to gets" Now, why does this happen and what can I do to fix it? Thank you for reading this and all help would be appreciated.

Magnus Hoff
  • 21,529
  • 9
  • 63
  • 82
Nikolay
  • 205
  • 1
  • 3
  • 12

1 Answers1

3

The gets method is declared in the cstdio header.

Try #include <cstdio> or #include <stdio.h>

Edit 1: Use std::string
I recommend using std::string and std::getline.

std::string text;
std::getline(cin, text);
std::cout << FirstReverse(text) << endl;
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154