473,467 Members | 1,342 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

command line argument

25 New Member
I made a program to be able to change the value of four variables (will be shown in the program) at the command line arguments.

int main (int argc, char *argv[])

{

double minx = -1.0;
double maxx = 1.0;
double miny = -1.0;
double maxy = 1.0;
int i;

for (i = 1;i < argc;i++)
{
if (strcmp(argv[i],"-range") == 0)
{
minx = atof(argv[++i]);
maxx = atof(argv[++i]);
miny = atof(argv[++i]);
maxy = atof(argv[++i]);
}
}
this code will work after I compile with cc compiler. Let's say the filename is range.c

cc range.c
a.out -range 1.0 2.0 3.0 4.0

But how do I stop the program (show error message) if less than four values entered, for example

cc range.c
a.out -range 1.0 2.0 3.0

Someone help me please
thank you
Sep 12 '06 #1
2 9755
Banfa
9,065 Recognized Expert Moderator Expert
Expand|Select|Wrap|Line Numbers
  1. #include "stdlib.h"
  2.  
  3. ...
  4.  
  5. if (argc < 5)
  6. {
  7.   // may be output an error message here
  8.   exit(EXIT_FAILURE);
  9. }
  10.  
Sep 12 '06 #2
tyreld
144 New Member
Expand|Select|Wrap|Line Numbers
  1. #include "stdlib.h"
  2.  
  3. ...
  4.  
  5. if (argc < 5)
  6. {
  7.   // may be output an error message here
  8.   exit(EXIT_FAILURE);
  9. }
  10.  

You still have to take into account that the program name is counted as one of the arguments. So, in this example you would actually be expecting no less than 6 arguments. The first being the program name. The second being the -range option, and the remaining 4 being the values.

Expand|Select|Wrap|Line Numbers
  1. if (argc < 6)
  2. {
  3.   // error message
  4.   exit(1);
  5. }
  6.  
Sep 12 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Joe | last post by:
I'm using Python 2.4 on Windows XP SP2. I'm trying to receive a command line argument that is a newline (\n) Here is the command line to use sample.py "\n" Here is a sample.py script
7
by: Bernd Danberg | last post by:
Hi, I have a real strange problem with the command line arguments given to the main-function and together with using std::string: #include <string> int _tmain(int argc, _TCHAR* argv) {...
2
by: SunRise | last post by:
Hi I am creating a C Program , to extract only-Printable-characters from a file ( any type of file) and display them. OS: Windows-XP Ple help me to fix the Errors & Warnings and explain...
3
by: Shailesh Humbad | last post by:
I want to set a command-line argument for a Windows service that I programmed in VB.Net. This ought to be available from the service installer classes, but I can not find any such property. What...
1
by: amirmira | last post by:
I would like to set command line arguments to a service at install time. I need to do this because I need to get information from different registry locations depending on my command line argument....
34
by: Roman Mashak | last post by:
Hello, All! I'm implementing simple CLI (flat model, no tree-style menu etc.). Command line looks like this: <command> <param1> <param2> ... <paramN> (where N=1..4) And idea is pretty simple: ...
6
by: evantri | last post by:
I am developing a program that prompt the input from the command line argument. For example, this is the lists of command line arguments that I want to include: -fish -cat -dog -animal All...
6
by: =?Utf-8?B?Rm9ycmVzdCBIZWxsZXI=?= | last post by:
The only code that is helpful is this: fp = New IO.StreamReader(filename, New System.Text.UnicodeEncoding(True, False), False) If I pass a command line argument to my program that is not from...
51
by: Ojas | last post by:
Hi!, I just out of curiosity want to know how top detect the client side application under which the script is getting run. I mean to ask the how to know whether the script is running under...
2
by: =?Utf-8?B?UmFtb24gR2VuZQ==?= | last post by:
Hello: I am building a console application and I am having an issue with the command line arguments. A couple of my arguments are paths and they are usually enclosed in double quotes. If one of...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.