Compiling Problem

Recommended Posts

Hey, trying to make a program that will reverse the string in the command line, but it wont compile. Here's the code: #include <iostream> #include <cstring> using namespace std; int main (char *argv[];) { cout << reverse(argv[1]); << endl; return 0; } Thanks. Any other tips or advice would be nice. -IV

Share on other sites
Oluseyi    2103
Where are the declaration and definition of reverse, or did you think it was a standard function?

Share on other sites
demonkoryu    980
For the start, it is

int main(int argcount, char* argarry[])

and if you use reverse, you need to

#include <algorithm>

And I am sure reverse doesn't work that way. If it works with char* arrays, you should

reverse(argv[1], argv[1] + strlen( argv[1]) );
cout << argv[1];

(this may be utter nonsense)

even then you shouldn't modify command line arguments in place, but copy it before.

It would be a good exercise to write a reverse function which works.

Thermo

Share on other sites
Hmmm, perhaps it isn't a very effective method, then.

What would be a better way of accomplishing my goal (reversing the command line string)?

-IV

Share on other sites
smart_idiot    1298
Maybe. . .

#include <algorithm>#include <string>#include <iostream>int main(int argc, char *argv[]) {  if(argc != 2)   {    std::cout << "Requires exactly one argument." << std::endl;    return -1;   }    std::string text(argv[1]);  std::reverse(text.begin(), text.end());    std::cout << "The reversed string is: " << text << std::endl;  return 0; }

Share on other sites
Alright, I tried it again, but instead I did it like this:

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int main (int agrc, char *argv[])
{

string text(argv[1]);
reverse(text.begin(),text.end());

cout << text << endl;

return 0;

}

Still won't compile.

Anyone?

-IV

Create an account

Register a new account