• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 263
  • Last Modified:

argument passing not working properly

I am new to c. Something in my arguments is not making mush sense. I have the following:
int main (int argc, char *argv[])
.
.
n=1;
while(argv[n]>""){
i=atoi(argv[n]);
printf("%i",i);
.
.
I execute by giving program name followed by 1 or 2. when 1, it prints 1, when 2, nothing happens. Can someone provide some help? Thanks
0
elikhater
Asked:
elikhater
1 Solution
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi elikhater,

The argument passing is fine.  The problem is in your test.  Also, you re-use the index variable.  Very dangerous thing to do.

Try starting with this:

  int n;

  for (n = 1; n < argc; n++)
  {
    printf("%s\n",argv[n]);
  }

Then you can expand it to:

  int n;
  int i;
  for (n = 1; n < argc; n++)
  {
    i = atoi (argv[n]);
    printf ("%d\n", i);
  }



Good Luck!
Kent
0
 
elikhaterAuthor Commented:
Kent, still did not work. Works fine for 1 but not 2.
0
 
mactep13Commented:
try the following:

int n=1;
while(argv[n] != "")
{
int i=atoi(argv[n++]);
printf("%i",i);
}
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Kent OlsenData Warehouse Architect / DBACommented:
Hi elikhater,

It's probably in the way that you're setting up the program.  Those snippets are fine.

Here's an entire program based on the second snippet.  Give it a try.

Running it as :

Test 1 2 3

Gives:

1
2
3

Kent



#include <stdlib.h>
#include <stdio.h>

int main(int argc, char* argv[])
{
  int n;
  int i;
  for (n = 1; n < argc; n++)
  {
    i = atoi (argv[n]);
    printf ("%d\n", i);
  }

  getchar ();
  return (0);
}
0
 
elikhaterAuthor Commented:
Kent,

Please clarify the following:

What is the purpose of getchar()? Aren't all the arguments stored in the argv pointer?
0
 
brettmjohnsonCommented:
> What is the purpose of getchar()?

On Windows systems, clickable console apps bring up a console window,
execute and the console window disappears before you can see the output.
Windows programmers often add a call to getchar() at the end of their
console programs.  This blocks, waiting for input on stdin, so that you can
see the output.  Press any key an the window disappears.  It is not necessary
if you use Unix/Linux or run the program from an MS-DOS command window.

Although common in the Windows world, it is a horrible kludge that cripples
the use of pipes to chain multiple programs together.

0
 
elikhaterAuthor Commented:
kdo,

figured it out and it was relating to not adding \n to my printf statements. Thanks for your efforts in any case.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now