LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 01-08-2007, 10:49 AM   #1
daYz
Member
 
Registered: Nov 2003
Distribution: Ubuntu
Posts: 164

Rep: Reputation: 30
Hoto run a for loop in C that depends on the program's first argument?


Hi,

I want to run a for loop, in C, that loops when the first expression of the for loop has an integer value that is less than the integer value given as the first argument to the program.

I have tried it like this:

Code:
for ( i = 0; i < argv[1]; i++)
But the loop is infinite this way.

So what is a proper way of doing this?

Thanks for the help.
 
Old 01-08-2007, 11:29 AM   #2
traene
Member
 
Registered: Jan 2005
Distribution: Archlinux, Debian, Centos
Posts: 222

Rep: Reputation: 35
Are you trying to give an argument to your programme, which lets you give the number of loops, the programme should do? You can try something like:
Code:
    int loops=atoi(argv[1]);
    for ( i = 0; i < loops; i++)
However, you should check given parameters, and may provide a default value.
 
Old 01-08-2007, 11:43 AM   #3
daYz
Member
 
Registered: Nov 2003
Distribution: Ubuntu
Posts: 164

Original Poster
Rep: Reputation: 30
Hi traene,

Yes, it is exactly what I'm trying. I am not yet familiar with C++ and atoi seems to be part of that language. Can you show me how the same can be achieved by just using C?

Thanks
 
Old 01-08-2007, 11:55 AM   #4
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
atoi is a part of C. try "man atoi" to see what you need to #include
 
Old 01-08-2007, 11:59 AM   #5
daYz
Member
 
Registered: Nov 2003
Distribution: Ubuntu
Posts: 164

Original Poster
Rep: Reputation: 30
Ok, I will take a look at atoi.

Thanks guys.
 
  


Reply



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
temporily suspending the execution of loop to allow other method to run linuxmandrake Programming 1 04-16-2006 07:56 PM
Loop ends unexpectedly after one run! trex_dk Programming 2 11-11-2005 03:49 AM
HOTO load kde in slackware 10 Creeps Linux - Newbie 4 09-13-2004 02:19 AM
How do I run program's with WINE? kaber Linux - Software 3 09-09-2004 03:41 PM
Bad Argument '53' IPtables doesn't run and has no bash debug lines? piratebiter Linux - Security 3 09-02-2003 10:18 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:02 PM.

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
Open Source Consulting | Domain Registration