Sign in to follow this  
TraderJack

Compiling Problem

Recommended Posts

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


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


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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this