Help - Search - Members - Calendar
Full Version: Sorry I Didn't Have Anyother To Ask
BleepingComputer.com > Software > Programming
   
sakaman
hi, some might find this stupid but all i want is an answer,

(language C)
I want to write this loop

for(i = 0; i<lim-1 && (c = getchar())!= '\n' && c != EOF; ++i)
s[i] = c;

without using && or ||


so my answer is:

for(i = 0 ; i<lim -1; ++i)
while ((c = getchar()) != '\n')
while( c!= EOF)
s[i] = c;

is my answer right? is there anyother solution?
groovicus
There are many ways to program... does the code that you wrote work?
sakaman
well actually it doesn't work mad.gif sad.gif wacko.gif

so i am asking
if those two code phrase are equal ?

and if the second is wrong how is it right?

thanks for your help laugh.gif
groovicus
Can I se the exact code that you are using? Use the cpde tags so thathe formatting is preserved, and then maybe I will be able to se what is happening.
sakaman
well actually it doesn't work mad.gif sad.gif wacko.gif

so i am asking
if those two code phrase are equal ?

and if the second is wrong how is it right?

thanks for your help laugh.gif
sakaman
#include <stdio.h>
#define MAXLINE 1000

int getline(char line[], int maxline);
void copy(char to[], char from[]);


main()
{ int len; /*lenght of the string i just gave*/
int max ; /*the length of the biggest string*/

char line[MAXLINE]; /*save string i just gave */
char longest[MAXLINE]; /*save the biggest string*/

max = 0;
while ((len = getline(line, MAXLINE))> 0)
if (len> max)
{ max = len;
copy(longest, line);
}
if (max>0)
printf("%s", longest);
return 0;
}

/* getline : gets string length*/

int getline(char s[], int lim)
{ int c, i;

for ( i=0; i<lim-1 ; ++i)
while ((c = getchar()) !='\n')
while(c!=EOF)
s[i] = c;
if (c == '\n')
{ s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
/*copy : copies to 'from' st 'to' , 'to' is big enough*/
void copy(char to[], char from[])
{ int i;

i =0;
while((to[i] = from[i]) != '\0')
++i;
}
the prgram is suposed to take a numbers of string and return the biggest of all of them
when i give it EOF (with ctrl-d)
sorry form the messy comments but there were in greek

the program didn't work after change the code (with the answer with no && and ||)
actually the the ctrl-d it doesn't work
sakaman
i didn't get it how will i preserv the formating sad.gif
groovicus
By using the code tags. When you paste code, you surround it with tags that look like this:[ code][ /code]

Does that code even compile?
sakaman
CODE
#include <stdio.h>
#define MAXLINE 1000
int getline(char line[], int maxline);
void copy(char to[], char from[]);


main()
{           int len;                
        int max;      
        char line[MAXLINE];    
        char longest[MAXLINE];  
        max = 0;
        while ((len = getline(line, MAXLINE))> 0)
                if (len> max)
                {       max = len;
                        copy(longest, line);
                }
        if (max>0)                    
                printf("%s", longest);
        return 0;
}


int getline(char s[], int lim)
{int c, i;
for ( i=0; i<lim-1; ++i)
                while ((c = getchar()) !='\n')
                        while(c!=EOF)
                                s[i] = c;
        if (c == '\n')
        {       s[i] = c;
                ++i;
        }
        s[i] = '\0';
        return i;
}

void copy(char to[], char from[])
{       int i;

        i =0;
        while((to[i] = from[i]) != '\0')
                ++i;
}


a man of it compiles


sorry i didn't get again tongue.gif
groovicus
That code is a mess, but I think for your issue:
CODE
int getline(char s[], int lim){
   int c, i;

   for ( i=0; i<lim-1; ++i){
      if ((c = getchar()) != '\n'){
         if(c!=EOF){
            s[i] = c;
         }
      }

      if (c == '\n') {
         s[i] = c;
         ++i;
      }  
         s[i] = '\0';
      }
      return i;
}


EDIT: Ignore this completely. I am soo tired at the moment that I am going crosseyed trying to keep the nestings straight.
sakaman
thanks man i will try it
thumbup.gif thumbup.gif clapping.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.