Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I wrote the following program and compiled it using wxDev C++ 7.3.0.6 on windows 7, but I did not get the desired output(to print the longest line entered) even though it compiles without a hitch.
C++
#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 && (c=getchar())!= EOF && c!='\n'; ++i)
        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;
}

Please help me out?
Posted
Updated 28-Oct-11 5:44am
v4
Comments
Sergey Alexandrovich Kryukov 28-Oct-11 11:40am    
Why do you thing that successful compilation should mean any desired results during run-time? :-)
And what's "desired output"?
--SA

You did not specify what goes wrong, but my guess it that the program never stops and prints the longest string entered.

You set the program to stop reading lines when getline returns 0, however if you look at your getline you will see that it always stores the enter in it. This means that for an empty string the length is 1 and not 0.
So if you replace
C++
while( (len=getline(line, MAXLINE))>0)
with
C++
while( (len=getline(line, MAXLINE))>1)
you should get your expected result.
 
Share this answer
 
Comments
kr_harsha 28-Oct-11 11:46am    
the program does not give any output.
i entered this on the screen-

hey<enter>
kumar<enter>
^Z<ctrl+z>

after this the window closes...
no output at all..
Sergey Alexandrovich Kryukov 28-Oct-11 11:59am    
Not true. It does output. Please see my solution, I explained what you can do.

André solution gets my 5 -- this is another variant of the same thing.
Just output some prompt before every call to getline.
--SA
It does output something. Did you expect some more?

The only call which outputs anything is printf at the end. You could easily fail to notice it, because this the application exists immediately after it.

Another problem: getline always return length > 0, even if one just presses Enter. I think you wanted to exit the loop if the user presses Enter; right now the loop executes infinitely unless the user end it with Ctrl+C, but then the result of the program is not output. Perhaps, return i − 1 instead of i, in this way, the length will be the string length not counting ending '\n' and '\0', which will be just fine for your purposes.

Well, and output prompt for the user before every call to getline; and you will be all right.

—SA
 
Share this answer
 
v4
Comments
Andrew Brock 28-Oct-11 12:00pm    
Almost.
"return i − 1 instead of i − 1", think you meant just "i" on that last one.
Also, it isn't the '\0' that it is counting in getline(), it is the '\n' that it is adding on.
Sergey Alexandrovich Kryukov 28-Oct-11 12:22pm    
Whatever... thanks for correcting me anyway...
First text mistake was fixed, will fix another one...
--SA
kr_harsha 28-Oct-11 12:02pm    
i tried i-1; still doen't give any output...
Sergey Alexandrovich Kryukov 28-Oct-11 12:25pm    
Please, do yourself a favor, read again, this time thoroughly: 1) there is output at the end already; 2) no more output for you until you add some; I already advised you to do so and explained where.

Good luck, call again.
--SA
add #include <windows.h>, so we can use pause

in main:
C++
if(max>0)
        printf("%s\n", longest); //Add a \n in here
   system("PAUSE"); //This will allow you yo see the results before closing the window on you. Credit to SA for picking up on this


and a revised getline():
C++
int getline(char s[], int lim)
{
    int c, i;
 
    for(i=0; i < lim-1 && (c=getchar())!= EOF && c!='\n'; ++i)
        s[i]=c;
    //comment out this
    /*if(c=='\n')
    {
        s[i]=c;
        ++i;
    }*/
 
    s[i]='\0';
 
    return i;
}


This will allow you to enter a series of words, when you enter a blank word (just hit enter) it will break and print the longest work you entered.
 
Share this answer
 
v5
Comments
kr_harsha 28-Oct-11 12:19pm    
yup that worked..
thanks..
Sergey Alexandrovich Kryukov 28-Oct-11 12:26pm    
Correct, a 5.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900