Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Jun 27th, 2006, 8:05 AM   #21
ivan
Professional Programmer
 
ivan's Avatar
 
Join Date: Sep 2005
Location: serbia & montenegro
Posts: 484
Rep Power: 4 ivan is on a distinguished road
Ok, I will try to guess what you think...

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

I suppose that here you are trying to check if argc contains 1 argument. What would happen if the user entered more than two arguments? So, the correct way would be:

if(argc == 1)
{ ----}

Now, in the if - statement, do you want to check that i is equal to 4? If iso, if( i == 4 ) would work just fine.

I hope that helps.
ivan is offline   Reply With Quote
Old Jun 27th, 2006, 8:24 AM   #22
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Squirrel, please go back to using code tags. Ivan, some of us think he want to set this value with a command line argument, as when he invokes the program. Others think he wants to set the value with a command line argument when he invokes the COMPILER to compile the program. He hasn't addressed this issue. He has been given solutions if the first assumption is correct. He hasn't given any really definitive code snippets or expressed his needs clearly. It's all ambiguous. Good responses are difficult to come up with under the circumstances.
__________________
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 27th, 2006, 8:27 AM   #23
squirrel
Newbie
 
Join Date: Jun 2006
Posts: 11
Rep Power: 0 squirrel is on a distinguished road
No u got me wrong. May be i explained it wrong.
I have a program which has a if construct:

fread(data,sizeof(data),1,fp_myfile);
if(data[0]=='8')&&(data[1]=='2'))
{
memcpy(&i,data+17,sixeof(int));

if(i == 4) // the value of "i" is not always same, so need to change the value of "i" eachtime i run the program
{
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)
{ 
printf("Error at command line");
printf("Enter value if 'i' ");
}

i=atoi(argv[1]);

Now my problem is how do i change the if construct??
fread(data,sizeof(data),1,fp_myfile);
if(data[0]=='8')&&(data[1]=='2'))
{
memcpy(&i,data+17,sixeof(int));
if(i == ??) // what do I give here, like how do i change the if construct, bcoz the value is taken from command line, its a bit confusing?? 
{
some commands;
}
else
{
some statements;
}
}

Hope the problem is understood.

thanks
Squirrel
squirrel is offline   Reply With Quote
Old Jun 27th, 2006, 8:28 AM   #24
squirrel
Newbie
 
Join Date: Jun 2006
Posts: 11
Rep Power: 0 squirrel is on a distinguished road
DaWei -> Iam sorry for posting such unclear questions. But i guess I will learn it soon, as iam new in the forums.
squirrel is offline   Reply With Quote
Old Jun 27th, 2006, 8:32 AM   #25
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Maybe understood. Let's presume you are now setting the value of i from the command line. Now you want to branch depending upon that value. If you don't know how your branch condition relates to the value, how the hell can we? Perhaps you could post a chart. If i is in the range blah:bleah, do this; otherwise, do that.
__________________
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 27th, 2006, 8:50 AM   #26
squirrel
Newbie
 
Join Date: Jun 2006
Posts: 11
Rep Power: 0 squirrel is on a distinguished road
Thankx for the help
squirrel is offline   Reply With Quote
Old Jun 27th, 2006, 9:37 AM   #27
stevengs
Professional Programmer
 
stevengs's Avatar
 
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4 stevengs is on a distinguished road
Quote:
Originally Posted by squirrel
DaWei -> Iam sorry for posting such unclear questions. But i guess I will learn it soon, as iam new in the forums.
The ability to convey an idea has nothing to do with being new to a forum.
Quote:
Originally Posted by squirrel
int i;
---
---

if(argc!=2)
{ 
printf("Error at command line");
printf("Enter value if 'i' ");
}

i=atoi(argv[1]);
You seem to wish to acquire input from the command line in the event that the program was called without (enough) arguments (but that is really just a wild guess). Input from the commandline in the middle of the program has nothing to do with the 'arg' variable (unless you explicitly put it in there). There are tools with varying degrees of portability (and robustness) available for getting input from the command line -> check out the stdio library.

The use of 'if' is particularly simple, but without some idea of the logic behind the program, no one will be able to help you much. For starters what does "i" represent, what do you intend to decide based apon it and what will actions will be taken? How many valid values of your particular "i" are there?
__________________
-Steven
"Is this a piece of your brain?" - Basil Fawlty

Last edited by stevengs; Jun 27th, 2006 at 9:59 AM.
stevengs is offline   Reply With Quote
Old Jun 27th, 2006, 10:44 AM   #28
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 5 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
It's as simple as changing your question marks to an actual number that you want i to be compared to...
__________________

tempest 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 11:37 AM.

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