-
arguments to main
So I'm trying to learn about arguments to main. The idea here was to start the program with it's name and then type in your first name. So in this case a.out Ryan. This is the output of the program, followed by the code. For some reason it is missing a section of the program.
The value of name_counter variable is 1
The value of name_counter variable is 2
The value of name_counter variable is 3
The value of name_counter variable is 4
This line printed, did it output if name was short/long first?
Code:
#include <stdio.h>
int main (int argc, char** argv)
{
/* VARIABLE DECLARATIONS */
int i = 0, name_counter = 0;
/* PROCESS FIRST NAME */
do
{
if ( argv[1][i] != '\0' )
{
name_counter++;
i++;
printf("The value of name_counter variable is %i\n", name_counter); /* debugging line */
}
else if ( argv[1][i] == '\0' )
{
printf("The program iterated thru the string and hit the '\0'"); /* debugging line */
if ( name_counter <= 3 )
printf("That's a short first name!");
else if ( name_counter >= 3 )
printf("That's a long first name!");
else
printf("There is an error in the 'else if ( argv[1][i] == '\0' ) routine'");
}
else
printf("The outer if/else in the name counting routine is not functioning properly.");
}
while ( argv[1][i] != '\0' );
printf("This line printed, did it output if name was short/long first?\n\n"); /* debugging line */
return 0;
} /* end main */
-
Simply put, argv is an array of pointers to strings. As you may, or may not know, you can get the length of a string with strlen() from string.h, i.e.:
Code:
#include <string.h>
#include <stdio.h>
int main(int argc, char * argv[])
{
char * firstname = NULL;
size_t length = 0;
/* sanity checking */
if(argc < 2)
{
printf("Usage: %s <firstname>\n", argv[0]);
return 1;
}
/* ease of reading ;) */
firstname = argv[1];
length = strlen(firstname);
if(length <= 3)
{
printf("That's a short firstname!\n");
/* > 3 */
}else{
printf("That's a long firstname!\n");
}
return 0;
}
You need to brush up on your logic:
Code:
if ( name_counter <= 3 )
printf("That's a short first name!");
else if ( name_counter >= 3 )
printf("That's a long first name!");
else
printf("There is an error in the 'else if ( argv[1][i] == '\0' ) routine'");
That last "else" can NEVER execute.
-
The "while (argv[1][i] != '\0')" part guarantees that your short/long first name will never happen, since your loop must necessarily break when argv[1] hits the \0 character.
-
The most civilized method is to use the industry standard getopt() calls:
Example of Getopt - The GNU C Library
Code:
// looks like this:
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int
main (int argc, char **argv)
{
int aflag = 0;
int bflag = 0;
char *cvalue = NULL;
int index;
int c;
opterr = 0;
while ((c = getopt (argc, argv, "abc:")) != -1)
switch (c)
{
case 'a':
aflag = 1;
break;
case 'b':
bflag = 1;
break;
case 'c':
cvalue = optarg;
break;
case '?':
if (optopt == 'c')
fprintf (stderr, "Option -%c requires an argument.\n", optopt);
else if (isprint (optopt))
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
else
fprintf (stderr,
"Unknown option character `\\x%x'.\n",
optopt);
return 1;
default:
abort ();
}
printf ("aflag = %d, bflag = %d, cvalue = %s\n",
aflag, bflag, cvalue);
for (index = optind; index < argc; index++)
printf ("Non-option argument %s\n", argv[index]);
return 0;
}
-
I don't see how getopt() helps at all in this circumstance, he's not trying to parse options. The use of command-line arguments does not automatically warrant the use of getopt()...