I want to be able to activate an easter egg in this program, but passing and reading these arguments is making me pull my hair out. Any ideas?

#include <cstdlib>
#include <iostream>

using namespace std;

//(cc) 2008 Nathan Barndt
//Attribution-Share Alike 3.0 United States License

void easteregg()
{
     //easteregg code here
     cout<<"Congrats. You've found the easter egg";
}

int main(int argc, char *argv[])
{
    if (argv[1] == "/e") easteregg();
    system("TITLE I Am Lost");
    system("COLOR F0");
    cout<<"\a";
    cout<<endl <<endl <<endl <<endl <<endl <<endl <<endl <<endl <<endl;
    cout<<"\t"<<"\t"<<"\t";
    cout<<"If you are seeing this message,";
    cout<<endl<<"\t"<<"\t"<<"\t";
    cout<<"I am lost. Please return me to:";
    cout<<endl<<"\t"<<"\t"<<"\t";
    cout<<"       xxxxxxxxxxxxxxxxx    ";
    cout<<endl<<endl<<endl<<"\t"<<"\t"<<"\t";
    system("PAUSE");
    return 0;
}

The line

if (argv[1] == "/e")

is invalid since you are compairing a char pointer to a string. To compare strings, use strcmp().

Also, if the user doesn't enter the easteregg argument, you don't want to check argv[1], so first check argc to check the number of arguments.

I want to be able to activate an easter egg in this program, but passing and reading these arguments is making me pull my hair out. Any ideas?

int main(int argc, char *argv[])
{
    if (argv[1] == "/e") easteregg();

Close, but as the previous poster mentioned, test to make sure argv[1] exists by checking argc, and use strcmp. You can change the line above to:

if (argc > 1 && strcmp (argv[1], "/e") == 0) easteregg();

For C++, I usually just forget the c-string stuff:

#include <ciso646>    // operator synonyms
#include <iostream>
#include <limits>     // numeric_limits
#include <string>
#include <vector>
#include <windows.h>  // to play with the console (system() is evil)
using namespace std;

void easteregg()
{
    ...
}

int main( int argc, char** argv )
{
    vector <string> args( argv, argv+argc );

    if ((args.size() > 1) and (args[ 1 ] == "/e")) easteregg();

    SetConsoleTitle( "I am lost" );
    SetConsoleTextAttribute( 0xF0 );
    cout << "\a";
    cout << string( 9, '\n' );
    ...

    cout << "Press ENTER to continue...";
    cin.ignore( numeric_limits <streamsize> ::max(), '\n' );

    return 0;
}

Enjoy!

thanks for the help, all of you. I really appreciate it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.