C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 12-11-2006, 07:46 AM   #1
Registered User
 
Join Date: Dec 2006
Posts: 2
Help Please, Beginner Question, rewrite loop

Hello, I'm really new to C programming, just started about a week and a half ago.

I worked with Visual Basic 6.0 last year, but i've forgotten alot since then. It appears my previous experience isn't much of a help with the higher languages.

But to the question.

How would I write a loop equivalent to the FOR loop below without using && or ||?

It's from the ANSI C book by Brian Kernighhan, Dennis Ritchie.

Code:
#include <stdio.h>

/* easy version, with && and || */

main()
{
int i;
int lim;
int c; 
int s[100];

      for (i=0; i<lim-1 && (c=getchar()) != '\n' && c != EOF; ++i)
        s[i] = c;
        
}
office888 is offline   Reply With Quote
Old 12-11-2006, 07:48 AM   #2
Registered User
 
Join Date: Dec 2006
Posts: 2
FYI, I'm pretty much learning on my own entirely (no teacher). I'd like to have a jump start before I hit college. I'm going to work through most of the C book, then move onto a reputable book for C++.
office888 is offline   Reply With Quote
Old 12-11-2006, 08:16 AM   #3
Registered User
 
Join Date: Nov 2006
Posts: 65
Use while and your code does not work, think about what your using and comparing.
__________________
You rant and rave about it, but at the end of the day, it doesn't matter if people use it as long as you don't see.
People are free to read the arguments, but if the only way for you to discover gravity is by jumping off a cliff, then that is what you're going to have to experience for yourself.
Eventually, this "fast and loose" approach of yours will bite you one too many times, then you'll figure out the correct way to do things. - Salem
KoG Metalgod is offline   Reply With Quote
Old 12-11-2006, 09:50 AM   #4
Registered User
 
Join Date: Nov 2006
Posts: 176
Code:
int i = 0
while (i < lim-1)
{
    c = getchar
    if c == '\n' break
    if c == EOF break
    s[i] = c
    i++
}
can't just copy and paste this, but here an outline of how it could be done
sl4nted is offline   Reply With Quote
Old 12-11-2006, 10:07 AM   #5
Registered User
 
Join Date: Apr 2006
Posts: 1,323
You also need to initilize lim.
__________________
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
King Mir is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
For loop question JuzMe C++ Programming 11 04-20-2009 08:39 AM
Loop question kwood965 C Programming 6 10-29-2008 11:12 PM
Same old beginner question... Sharmz C Programming 15 08-04-2008 11:48 AM
for loop question cdalten C Programming 4 03-20-2006 09:42 AM
Beginner on Win32 apps, lame question. Templario C Programming 3 11-06-2002 08:39 PM


All times are GMT -6. The time now is 11:32 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22