command line/terminal arguments are driving me bananas
according to most of my google searchs this should work
by the way i am using g++/gcc version 4.6.0

#include <iostream>
#include <string>
using namespace std;


int main(int argc, char **argv){
if(argc==0){
cout<<"Use -l or -ls!\n";
}
if(string(argv[1])== "-l"){
//code for lua interpreter
}
else if(string(argv[1]) == "-ls"){
//code for interpreting lua script
}

lua_close();
return 0;
}

instead of ./nameofprogram printing "Use -l or -ls!"
it prints nothing but the switches work just fine.
what gives ? i will post full code if necessary but i do believe the rest is functioning properly as my only problem exists with argc == 0 not doing what it should(or rather what i believe it should).
thanks in advance DW always helps me figure out whats going wrong.

Recommended Answers

All 8 Replies

The test should be if(argc == 1) . argc always is at least 1, as the first argument always is the command that invoked the program.

just tryed that no dice mate :( . ill post the full code maybe that would help?

#include <iostream>'
#include <stdio.h>
#include <string.h>
#include <lua.hpp>
using namespace std;

int main(int argc,char ** argv){
char buff[256];
int error;

/*
Here I am adding command line switches this may or may not break the interpreter
*/

lua_State * l;
l = lua_open();
luaL_openlibs(l);

for (int i=1; i<argc; i++){


if(string(argv[i]) == "-ls"){
luaL_dofile(l,argv[2]);
}

else if(string(argv[i]) == "-l"){
cout<<"Lua>> ";
while(fgets(buff,sizeof(buff),stdin) != NULL){
error = luaL_loadbuffer(l,buff,strlen(buff),"line") || lua_pcall(l,0,0,0);
cout<<"Lua>> ";
if(error){
fprintf(stderr,"%s\n",lua_tostring(l,-1));
lua_pop(l,1);
}//if(error) end
}//while() end
}//else if end

if(argc==1){
cout<<"Either use the -l or -ls arguments\n";
}

}//end of for statement

lua_close(l);
return 0;

}

Err... What is if(argc==1) { cout<<"Either use the -l or -ls arguments\n"; } doing down there? It should be the first thing inside main. In fact, it should be like this:

int main(int argc, char ** argv)
{
    if(argc == 1)
    {
        cout << "Either use the -l or -ls arguments\n";

        cout << "(hit enter to quit...)"; cin.get();

        return 0; // terminate the program
    }

    //...
}

What exactly is the problem again? What do you expect the program to do? What does it do?

commented: thanks man that saved me a lot of trouble. +2

so it has to be the first statement then ?

also the program is just a test to see if i could embed a lua interpreter in C++ which every thing works except that one if statement i really wouldn't being asking such a little question like this if it hadn't hit a nerve in my skull ;P
everything works fine except that one little if statement. ill try placing it at the beginning if that doesn't work ill file a bug report for gcc/g++ although previous implementations never gave me this weird behavior.

ok i fixed it by adding the if statement to the top right under int error;

apparently you can't nest that conditional for some odd reason i'm now wondering if that is because of the c++0x standard or if its just a bug. its a simple test it shouldn't do that if i were to compile the same code under 4.3 gcc/g++ it would compile just fine.

Doesn't anyone know how to write a simple test to figure out something that they don't quite understand?

#include <iostream>
using namespace std;
int main(int ac, char *av[])
{
  int i;
  cout << "Arg count = " << ac << endl;
  for (i=0; i<ac; i++)
  {
    cout << "param " << i << "  " << av[i] << endl;
  }
  return 0;
}

Learn to write these test programs. It will save you hours of pulling your hair out and waiting for someone to answer forum questions.

commented: thanks for the info +2

i could have done that but that wouldn't have help solved my problem i would have missed the fact that the if(argc == 1) has to be at the top but thanks :p

The statement you seem to have problem with checks whether there are sufficient command line arguments.
If there aren't, it terminates the program. Are you sure you do provide these arguments when you run the program?

If you're not sure you do and you're not sure how to do it, you can always get what you need from the user:

//...

#include <string>

using namespace std;

int main()
{
    //...

    string arg;

    cout << "enter argument (-l or -ls) > ";

    getline(cin, arg);

    if (arg == "-ls")
    {
        cout << "enter filename > ";

        getline(cin, arg);

        luaL_dofile(l, arg.c_str());
    }
    else if (arg == "-l")
    {
        //...
    }
    else
    {
        cout << "invalid arguments..." << endl;
    }

    //...

    return 0;
}

WaltP has a point though...

getting the arguments isn't a problem for me i understand that its just that with 4.3 gcc/g++ i seem to have been spoiled by its slight lack of enforcement or maybe it was an oversight i don't really know. i was just having trouble with checking to see if there werent any arguments and printing the message to the screen anyhow its fixed now ;P thanks DW once again my favorite IT community!

also ive replied about your optimization trouble!

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.