![]() |
| | #1 |
| Registered User Join Date: Dec 2006
Posts: 2
| Help Please, Beginner Question, rewrite loop 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 | |
| | #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 | |
| | #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 | |
| | #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++
}
|
| sl4nted is offline | |
| | #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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |