![]() |
| | #1 |
| Registered User Join Date: Feb 2008 Location: Yokohama
Posts: 48
| K&R solution? Code: for (i = 0; i < lim -1 && (c = getchar()) != '\n' && c != EOF; ++i) s[i] = c; Code: i = 0;
while (i < lim -1)
if (c = getchar() != '\n')
if (c != EOF)
++i;
s[i] = c;
Last edited by deadhippo; 05-08-2008 at 02:31 AM. |
| deadhippo is offline | |
| | #2 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| I'm assuming yours is the second one, in which case you are missing the condition to break the loop when you get a end-of-line or end-of-file. -- Mats
__________________ Compilers can produce warnings - make the compiler programmers happy: Use them! Please don't PM me for help - and no, I don't do help over instant messengers. |
| matsp is offline | |
| | #3 |
| Registered User Join Date: Apr 2008
Posts: 278
| I guess the 1st solution is the correct one and the second is your attempt? Clearly they don't do the same thing. The first one fill the array with characters read from the input stream until the array is full or there's nothing more to read or the last character was a new line. The second solution fill the array and everything should be fine until the EOF or '\n'... in which case, if the array is not full yet, we should fall into an infinite loop as the index is no longer incremented and the current item in the array is overwritten by EOF or '\n'. The particular case in which the last character read is also the last index of the array should work fine however... |
| root4 is offline | |
| | #4 |
| Registered User Join Date: Feb 2008 Location: Yokohama
Posts: 48
| Thanks. Back to the drawing board. |
| deadhippo is offline | |
| | #5 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| Code: if ( (c = getchar()) != '\n' ) Just thought I'd mention. Carry on!
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #6 |
| Registered User Join Date: Feb 2008 Location: Yokohama
Posts: 48
| Ok, I have another idea. Code: i = 0;
loop = 1;
while (loop == 1)
if (i >= lim-1)
loop = 0;
else if ((c = getchar()) == '\n')
loop = 0;
else if (c == EOF)
loop = 0;
else
{
s[i] = c;
++i;
}
Thanks Elysia, I had been reading that that was good practice too. |
| deadhippo is offline | |
| | #7 | |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Quote:
-- Mats
__________________ Compilers can produce warnings - make the compiler programmers happy: Use them! Please don't PM me for help - and no, I don't do help over instant messengers. | |
| matsp is offline | |
| | #8 | |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Quote:
Code: i = 0;
while (i < lim-1)
{
if ((c = getchar()) == '\n' || c == EOF)
break;
s[i] = c;
++i;
}
Mats
__________________ Compilers can produce warnings - make the compiler programmers happy: Use them! Please don't PM me for help - and no, I don't do help over instant messengers. | |
| matsp is offline | |
| | #9 |
| Registered User Join Date: Feb 2008 Location: Yokohama
Posts: 48
| Thanks Mats, but according to the exercise we can't use || or &&. |
| deadhippo is offline | |
| | #10 | |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Quote:
-- Mats
__________________ Compilers can produce warnings - make the compiler programmers happy: Use them! Please don't PM me for help - and no, I don't do help over instant messengers. | |
| matsp is offline | |
| | #11 |
| Registered User Join Date: Feb 2008 Location: Yokohama
Posts: 48
| Thanks. I'll try that. |
| deadhippo is offline | |
| | #12 |
| -AppearingOnThis.......... Join Date: May 2005 Location: Netherlands
Posts: 44
| why even bother splitting it up into if/elseif cases. just use the fact that conditionals such as == and != represent a boolean value. ie. something like Code: while((cond1) + (cond2) == 2) ... |
| SirNot is offline | |
| | #13 | |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Quote:
-- Mats
__________________ Compilers can produce warnings - make the compiler programmers happy: Use them! Please don't PM me for help - and no, I don't do help over instant messengers. | |
| matsp is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| 'Solution' and 'Project' usage in VC++ | C+/- | C++ Programming | 2 | 01-13-2007 09:50 AM |
| anyone know the solution? | heeroyung | C Programming | 15 | 09-30-2005 06:46 AM |
| My Unix/Linux SECURITY SOLUTION - pls read | bjdea1 | Linux Programming | 3 | 04-11-2004 09:28 PM |
| Solution - Workspace --> are they the same | actionbasti | C++ Programming | 2 | 09-14-2003 04:05 PM |
| Problem and Solution | DavidP | Tech Board | 3 | 08-18-2003 02:23 AM |