Parsing command line arguments in C

  • Thread starter Thread starter TheSourceCode
  • Start date Start date
  • Tags Tags
    Line
AI Thread Summary
The discussion focuses on a C programming assignment that requires parsing command line arguments to display a user's first name, last name, and optionally their age. The original code crashes when arguments are not formatted correctly, particularly when the age argument is not provided. Valid examples of command line inputs include "program.exe John Doe -age 43" and "program.exe -age 56 Jane Doe." The importance of checking array bounds before accessing elements is emphasized to prevent errors. The user successfully resolved the issue after receiving feedback on their code.
TheSourceCode
Messages
14
Reaction score
0
Here is the given assignment:

Write a code that would
parse the command line
and display the user’s first /
last name and (if given)
his/her age. The ‘-age’ key,
if present, could be the first
or, alternatively, the third
argument and indicates that
the following parameter is
age, as in the example
shown. If the command
line arguments are not
valid, an error message to
that effect should be
displayed. (You may use
string functions as needed.)

My program works as long as the user is inputting arguments with the -age key but crashes if any other time. Could I get a hint as to what is wrong?

Code:
#include <stdio.h>
int main(int argc, char *argv[]){ 
  if(argc < 3 || argc > 5 || argc == 4)
    printf("Invalid command line arguements.\n");
  if(!strcmp(argv[2], "-age") || !strcmp(argv[4], "-age"))
    printf("Invalid command line arguements.\n");
  if(argc == 3){
    printf("Your first name is: %s\n", argv[1]);
    printf("Your last name is: %s\n", argv[2]);
  }   
  if(!strcmp(argv[1], "-age")){
    printf("Your first name is: %s\n", argv[3]);
    printf("Your last name is: %s\n", argv[4]);
    printf("Your age is: %s\n", argv[2]);
  }
  if(!strcmp(argv[3], "-age")){
    printf("Your first name is: %s\n", argv[1]);
    printf("Your last name is: %s\n", argv[2]);
    printf("Your age is: %s\n", argv[4]);
  }
  system("PAUSE");	
  return 0;
}
 
Physics news on Phys.org
Can you provide an example valid arguments? It said the age argument could be first or third, but in your program, it looks like it's the third or fifth...so I'm a little lost.
 
"if(argc < 3 || argc > 5 || argc == 4)"

You said age if given, so that means age input is not necessarily expected right?
 
Sorry, let me clarify a little. Valid input would look something like this:

Code:
program.exe John Doe -age 43
or
Code:
program.exe -age 56 Jane Doe
or
Code:
program.exe Bob Smith
Where program.exe is argument 0 and so on...
 
If the age is not suppled, your program would attempt to read or write past the end of the argv array. Read your source code and attempt to find out where.

This is something that is very important to avoid. Every time you access an array element that might not exist, check the array size (or bounds) first!
 
Thanks for the feedback everyone, I got it to work! I really should have noticed the issue before.
 
Thread 'Have I solved this structural engineering equation correctly?'
Hi all, I have a structural engineering book from 1979. I am trying to follow it as best as I can. I have come to a formula that calculates the rotations in radians at the rigid joint that requires an iterative procedure. This equation comes in the form of: $$ x_i = \frac {Q_ih_i + Q_{i+1}h_{i+1}}{4K} + \frac {C}{K}x_{i-1} + \frac {C}{K}x_{i+1} $$ Where: ## Q ## is the horizontal storey shear ## h ## is the storey height ## K = (6G_i + C_i + C_{i+1}) ## ## G = \frac {I_g}{h} ## ## C...

Similar threads

Replies
8
Views
2K
Replies
10
Views
2K
Replies
2
Views
5K
Replies
3
Views
1K
Replies
4
Views
1K
Replies
2
Views
2K
Replies
1
Views
10K
Back
Top