Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 23rd, 2006, 9:26 AM   #11
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
IF loop of n2??
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jun 23rd, 2006, 9:26 AM   #12
Little Mac
Newbie
 
Little Mac's Avatar
 
Join Date: Apr 2006
Posts: 4
Rep Power: 0 Little Mac is on a distinguished road
What InfoGeek was saying was that you could use...

sscanf(argv[1],"%d",&n2);

...to put the contents passed into argv[1] into the int n2 (&n2 means the address of n2). argv[1] is the first argument passed into your program (stored as a char *). sscanf can convert it to an integer (that's the "%d" part, the format part). The situation looks like this.

/command/prompt> your-program-name first-argument

or

/command/prompt> check_K_source 41388

If you insert this into your code at some point prior to your first use of the variable n2, then the number you've entered as your first argument will be used for the value of n2.

What DaWei was saying is that it's good programming style to add an error check around the call to sscanf. This way, your program doesn't explode if you forget to enter any arguments. Your program already contains severl similar checks.
__________________
"Help! Doc!!"

"Join the Nintendo Fun Club today! Mac."
Little Mac is offline   Reply With Quote
Old Jun 23rd, 2006, 9:30 AM   #13
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
It isn't good programming style, it's good programming practice. It's REQUIRED programming practice if I hire someone. If the user enters a non-numeric for that argument, the stream will fail. It won't recover on its own; the coder has to test for success and clear the failure if it occurs (or terminate the program or some other appropriate action). If the failure is not cleared, all subsequent stream operations will fail and the coder can be looking in the wrong place for the problem. One surely wouldn't want to put such code in a life-support system.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jun 23rd, 2006, 9:53 AM   #14
squirrel
Newbie
 
Join Date: Jun 2006
Posts: 11
Rep Power: 0 squirrel is on a distinguished road
Thankx for all the help
squirrel is offline   Reply With Quote
Old Jun 23rd, 2006, 9:56 AM   #15
squirrel
Newbie
 
Join Date: Jun 2006
Posts: 11
Rep Power: 0 squirrel is on a distinguished road
wat should be given here at this loop,

                      if (n2 == 41388) 
			{
				printf("\t<Sin> 1stNode [%d] 1stLink [%d] 2ndNode [%d] 2ndLink [%d] 3rdNode [%d]\n",n1,l1,n2,l2,n3);
			}
			else
			{
				fwrite(data,128,1,fp_out);
			}

squirrel
squirrel is offline   Reply With Quote
Old Jun 23rd, 2006, 10:20 AM   #16
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
I'm sorry, but your question is entirely unclear. What do you WANT there? (It isn't a loop, it's an if construct. If there's a loop involved, it isn't within the visible context.)
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jun 23rd, 2006, 11:01 AM   #17
Little Mac
Newbie
 
Little Mac's Avatar
 
Join Date: Apr 2006
Posts: 4
Rep Power: 0 Little Mac is on a distinguished road
Squirrel man...we want to help, but we don't want to trace through the difficult-to-interpret code you keep posting, especially without any explaination on your part as to what it does.

Look back at my original post. If this code you've highlighted is in fact the FIRST use of the variable n2 since it was declared, then what you want will probably be something like this.

if (sscanf(argv[1], "%d", &n2) != 1) // transfer first input argument to n2
{
printf("Error transfering user-input\n");
exit(0);
}
if (n2 == 41388) // compare that value against the literal 41388
{
printf("\t<Sin> 1stNode [%d] 1stLink [%d] 2ndNode [%d] 2ndLink [%d] 3rdNode [%d]\n",n1,l1,n2,l2,n3);
}
else
{
fwrite(data,128,1,fp_out);
}

Quote:
Originally Posted by DaWei
It isn't good programming style, it's good programming practice.
You say tom-A-to, I say tom-ah-to. I'm sure we mean the same thing.
__________________
"Help! Doc!!"

"Join the Nintendo Fun Club today! Mac."
Little Mac is offline   Reply With Quote
Old Jun 23rd, 2006, 11:31 AM   #18
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Sometimes things are synonyms, sometimes not. It isn't a biggie unless the miscommunication costs some mean guy a bunch of bucks.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jun 23rd, 2006, 4:47 PM   #19
mackenga
Professional Programmer
 
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 317
Rep Power: 4 mackenga is on a distinguished road
It looked like the original question was about compiling with an argument to set these values rather than passing an argument to the program. In case you're still interested in that, it is possible; use a symbol in your program like:

x = SOMEVALUE + 6;

and don't bother defining anywhere what SOMEVALUE is. Then pass your compiler -DSOMEVALUE=22 (say). I believe this works with gcc; there'll be something similar with most compilers. In case I have the specifics wrong, what you want to do is define a constant as when using:

#define NAME "value"

or similar, but do it from the command line. Most compilers should let you specify such constants with arguments as well as in files.

'Scuse the rushed response. I want to go watch an episode of The X Files so I'm not paying much attention to this answer to be honest...
__________________
"I'm not a genius. Why do I have to suffer?"
mackenga is offline   Reply With Quote
Old Jun 27th, 2006, 7:25 AM   #20
squirrel
Newbie
 
Join Date: Jun 2006
Posts: 11
Rep Power: 0 squirrel is on a distinguished road
Thanks for all the help.

My problem was simple,
I have a program which has a if construct:
if(i == 4)
{
some commands;
}
else
{
some statements;
}

This program is hardcoded, and i need to open the program and change the value of "i" always.

I modified the program so that "i" can be taken as command line argument. I added some code lines as follows:

int i;
---
---

if(argc!=2)
{ ----}

i=atoi(argv[1]);

Now my problem is how do i change the if construct??

if(i == ??)


squirrel
squirrel is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:25 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC