I am trying to parse main(argc, argv[]); and i need to look at individual
characters with in the strings pointed to by *argv[]. I though of a string
as an array of chars. This builds fine but causes an error in execution.

So what is a simple and straight forward way. Lets say I want to look at
each character in a string pointed to by argv[c] and/or compare the each
character or compare strings.

I am sure this is quite simple and I want to keep it simple if possible.

I am missing something here. Thanks in advance for suggestions.

// ArgParse.cpp : Defines the entry point for the console application.
// How can you refer to a character in a string? This does not work !!!

#include "stdafx.h"

#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
int c = argc;

while (c) {
	printf("arg %i - |%s|\n",c,argv[c]);
	if (argv[c][0] == '-') {				// Look at char in pos 0 ='-'? //
		printf("|%c|\n", argv[c][1]);		// then show what it is //
	}
		c--;
}

-eof-

73
-Grace
NNNN
z

Recommended Answers

All 4 Replies

>>c--;

The initial value of c is too large. The value of argc is the number of strings in the array. So it counts from 0 to argc-1. You should make the loop as you would when using any other array

for(int i = 0; i < argc; i++)
{
   // blabla
}

If you would rather use a while loop, then

int c = argc-1;
while( c )
{

}

Here we go again - I am looking for simple answers here I think without copying
the string from argv[] to an array allocated with malloc or some such madness.
There must be a very easy way to do this. I simply want to look at each argument
and if the character following a - or + is one of the switches I will set a
flag to use the next argument in a particular way.

This is something like what I am trying to do:

// ArgParse.cpp : Defines the entry point for the console application.
// How can you refer to a character in a string? This does not work !!!

#include "stdafx.h"

#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <stdlib.h>

#define STRINGSIZE 128

int main(int argc, char *argv[])
{
	char ch;
	char *str[STRINGSIZE];
	int c = argc;

while (c) {
	printf("arg %i - |%s|\n",c,argv[c]);
	str = argv[c];
#25-->	ch = str[0];
	if (ch == '-') {			// Look at char in pos 0 ='-'? //
		ch = str[1];
		printf("|%c|\n", ch);		// then show what it is //
	}
		c--;
}

system("PAUSE");
return 0;
}

Now when I compile I get the following error from Visual Studio 2008 c++:

"There is no context in which this conversion is possible
1>c:\users\grace\documents\visual studio 2008\xorcryt\argparse\argparse\argparse.cpp(25) : error C2440:"

'=' : cannot convert from 'char *' to 'char'

I am accustomed to assembler and c++ along with Visual studio are new
to me. Okay, maybe that defines better what I am trying to accomplish. Any
help for this newbie would be appreciated. As I said I think I could
come up with something far more difficult than this needs to be.

Thanks again!

73
-Grace
NNNN
z

why are you trying to assign an array of pointers (str) to a single string? Also you failed to heed my warning about argc. If you want to do anything useful you have to start from argv[1] to argv[argc-1], not the other way around. Western languages read from left to right, not the other way around, and that's the way they are represented in argv.

// ArgParse.cpp : Defines the entry point for the console application.
// How can you refer to a character in a string? This does not work !!!

#include "stdafx.h"

#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <stdlib.h>

#define STRINGSIZE 128

int main(int argc, char *argv[])
{
	char ch;
	char *str;
	int c = 1;

while (c < argc) {
	printf("arg %i - |%s|\n",c,argv[c]);
	if (argv[c][0] == '-') {
		printf("|%c|\n", argv[c][0]); // then show what it is 
             // assume the option string immediately follows the - symbol
             if( stramp( &argv[c][1], "Hello" )
             {  
                 printf("%s\n", &argv[c][1]);
             }
	}
	c++;
}

system("PAUSE");
return 0;
}

why are you trying to assign an array of pointers (str) to a single string? Also you failed to heed my warning about argc. If you want to do anything useful you have to start from argv[1] to argv[argc-1], not the other way around. Western languages read from left to right, not the other way around, and that's the way they are represented in argv.....

.............
....
...          // assume the option string immediately follows the - symbol
             if( strcmp( &argv[c][1], "Hello" )
             {  
                 printf("%s\n", &argv[c][1]);
             }
	}
	c++;
}
......
..

Okay! After some thought I see how this can be useful...

A belated but hearty thank You!

Solved!

73
-Grace
NNNN
z

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.