LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 03-02-2014, 02:03 AM   #1
Aska123
Member
 
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.
Code:
//sim.cc

#include<stdlib.h> 
#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",
delay);
 }
then I compiled it as follows
g++ -o sim sim.cc

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

where is the error?

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

Rep: Reputation: 311Reputation: 311Reputation: 311Reputation: 311
Hi,
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, 07:26 AM   #3
Aska123
Member
 
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
 
  


Reply


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 02:18 PM
command line argument in c vipin_jss Linux - Newbie 6 05-11-2009 06:47 PM
bash script 'for each command line argument' true_atlantis Linux - Newbie 3 01-28-2009 01:51 PM
Perl command line argument noir911 Programming 8 01-15-2008 04:59 AM
Redirecting output to a command-line argument of another command madiyaan Linux - Newbie 1 02-19-2005 04:35 PM


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

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