Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 03-02-2014, 03:03 AM   #1
Registered: Apr 2013
Posts: 197

Rep: Reputation: Disabled
command line argument result is always coming 0

Hello friends

I have written a program to calculate delay between nodes using link.

#include <iostream>
#include <fstream>
#include <math.h>
#include <cstdio>

 int main(int argc, char* argv[]) {
 float delay = 0, d_i = atof(argv[0]);
 int i, num_nodes = atoi(argv[1]);
 for(i = 1; i < num_nodes; i++)
 delay += d_i;
 printf("Overall Packet Delay is %2.1f seconds\n",
then I compiled it as follows
g++ -o sim

when i run $./sim 2 5
it gives packet delay is 0.0

where is the error?

please reply
Old 03-02-2014, 03:55 AM   #2
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
In C++, argv[0] contains the name of the program. The first argument is argv[1].
1 members found this post helpful.
Old 03-02-2014, 08:26 AM   #3
Registered: Apr 2013
Posts: 197

Original Poster
Rep: Reputation: Disabled
Thanks millgates
problem is solved.

I changed argv[0] to argv[1] and argv[1] to argv[2].

now when I run ./delay 2 5
output is 8.0


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
uniform result whois command line anilcliff Linux - Newbie 2 09-12-2011 03:18 PM
command line argument in c vipin_jss Linux - Newbie 6 05-11-2009 07:47 PM
bash script 'for each command line argument' true_atlantis Linux - Newbie 3 01-28-2009 02:51 PM
Perl command line argument noir911 Programming 8 01-15-2008 05:59 AM
Redirecting output to a command-line argument of another command madiyaan Linux - Newbie 1 02-19-2005 05:35 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:01 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration