Link to home
Start Free TrialLog in
Avatar of grfik
grfik

asked on

Command line arguments

I am trying to use command line arguments to input a string(or just a array of char)
this is the code i am using..

#include <cstdio>
int main(int argc, char** argv)
{
char* word;
sscanf(argv[1], "%s", &word);

...}

my program crashes when i try to access WORD and when i use COUT statements to see what WORD contains.. it would output weird characters.

could you tell me how to correctly input commmand line arguments..
Avatar of Axter
Axter
Flag of United States of America image

word needs to have memory assigned to it.
Example:
int main(int argc, char** argv)
{
char word[32];
sscanf(argv[1], "%s", &word);
You should also check that argc is at least equal to two.
Example:
int main(int argc, char* argv[])
{
     char word[32];
     if (argc > 1) sscanf(argv[1], "%s", &word);
     
     printf("argv[1] = %s\n", word);

     system("pause");
     return 0;
}

Avatar of grfik
grfik

ASKER

this is my main() function

int main(int argc, char** argv)
{
   if(argc < 2)
   {
      cout << "Must enter a word as a command line argument\n";
      return 0;
   }

   char w[90];

   Stack<char> s;
   Queue<char> q;

   sscanf(argv[1], "%s", &w);

   /*
   int i = 0;
   while(w[i] != '\n')
   {
      s.push(w[i]);
      q.add(w[i]);
      i++;
   }
   */

   char word;
   char ans = 'n';
   do
   {
      Stack<char> s;
      Queue<char> q;

      if(ans != 'n' && ans != 'N')
      {
         cout << "Enter a word: ";
         cin.get(word);

         while (word != '\n')
         {
            s.push(word);
            q.add(word);
            cin.get(word);
         }
      }


      bool check = true;
      try
      {
         while ( ! q.isEmpty( ) )
            if(s.pop() != q.remove())
            {
               check = false;
               break;
            }
      }
      catch (QueueEmpty qe)
      {
         cout << qe.getMessage();
      }

      if(check)
         cout << "The word is a palindrome\n";
      else
         cout << "The word is not a palindrome\n";

      cout << "Again?(y/n): ";
      cin >> ans;
      cin.ignore(10000, '\n');
   }while (ans != 'n' && ans != 'N');

   return 0;
}

i commented a part so the program executes..

as you can see i attempt to access individual characters from the command line argument..
but since the char is initialized at a array of size 90
"char w[90]"

so i cant test for the end of the word..
there is no null character :(

The thing is... you may not need to use sscanf at all (I have never liked that function).

argv[1] is the first word, argv[2] is the second word etc.

For instance, if the command line is:

     MyProg.Exe now is the time for all good beans

then argc is 9
and
argv[1] is the address of "now"
argv[2] is the address of "is"
argv[3] is the address of "the"
argv[4] is the address of "time"
argv[5] is the address of "for"
argv[6] is the address of "all"
argv[7] is the address of "good"
argv[8] is the address of "beans"

If you do use sscanf, you really should pass in the address of the destination buffer:

   sscanf(argv[1], "%s", word );
or
   sscanf(argv[1], "%s", &word[0] );

(rather than &word) even though the compiler seems to accept either form in this case.  It's just a good habit.

-- Dan
Avatar of grfik

ASKER

the problem is then that i have to specify a size for array of characters WORD
for some reason i cant test for the null character..
the rest of the array gets filled with random characters which causes alot of problems
ASKER CERTIFIED SOLUTION
Avatar of DanRollins
DanRollins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If your real problem is with accessing commandline parameter (the part which you commented out) herez the fix.

/*
  int i = 0;
  while(w[i] != '\0')
  {
     s.push(w[i]);
     q.add(w[i]);
     i++;
  }
*/

Also if you recoin the word from letters make sure you manually insert String terminator ('\0') at the end.
You could simply use the strings.

if (argc > 1)
{
  // process command line options
  for (int i=1; i <= argc; i++)
  {
    // process i parameter
    do_something (argv[i]);
  }
}
#include <stdio.h>
#include <iostream.h>

int isPalindrome(char *str){
  char *pbeg = str, *pend = str + strlen(str) - 1;

  while(pbeg < pend){
    if(*pbeg != *pend) return 0; //false
    pbeg--;
    pend--;
  }
  return 1; //true
}

void main(int argc, char* argv[]){
  if (argc < 2) cout << "must enter a word at least" << endl;
  else {
    for(int i = 0; i < argc; i++){
      cout << argv[i] << " is " << isPalindrome(argv[i]) ? "" : "not " << "a palindrome" << endl;
    }
  }
}

I didn't compile this actually, but I think it should work. Hope this helps.
Oops. In isPalindrome, pbeg has to be incremented of course. See what happens if you don't proofread your code? ;-)
Avatar of grfik

ASKER

ooooopsss
i was checking for \n all this time while i should've been checking for \0
thx dan
and the way i actually ended up implementing the code was..
   int i = 0;
   while (argv[1][i] != '\0')
   {
      s.push(argv[1][i]);
      q.add(argv[1][i]);
      i++;
   }
sry if i worded the question so it seemed more complicated then it should be.. :/

thank you all
They don't call me eagle-eye Dan fer nuthin!  Good luck -- Dan