![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#12 |
|
Newbie
Join Date: Apr 2006
Posts: 4
Rep Power: 0
![]() |
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." |
|
|
|
|
|
#13 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#14 |
|
Newbie
Join Date: Jun 2006
Posts: 11
Rep Power: 0
![]() |
Thankx for all the help
|
|
|
|
|
|
#15 |
|
Newbie
Join Date: Jun 2006
Posts: 11
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#16 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#17 | |
|
Newbie
Join Date: Apr 2006
Posts: 4
Rep Power: 0
![]() |
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:
I'm sure we mean the same thing.
__________________
"Help! Doc!!" "Join the Nintendo Fun Club today! Mac." |
|
|
|
|
|
|
#18 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#19 |
|
Professional Programmer
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 317
Rep Power: 4
![]() |
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?" |
|
|
|
|
|
#20 |
|
Newbie
Join Date: Jun 2006
Posts: 11
Rep Power: 0
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|