• Advertisement
Sign in to follow this  

Compiling Problem

This topic is 4862 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
Share on other sites
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 this post


Link to post
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement