程序即使编译也没有输出 [英] no output for the program even when it compiles

查看:95
本文介绍了程序即使编译也没有输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了以下程序,并在Windows 7上使用wxDev C ++ 7.3.0.6对其进行了编译,但是即使编译顺利,我也没有得到想要的输出(打印输入的最长行).

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.

#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?

推荐答案

您没有指定出什么问题,但是我猜想程序永远不会停止并打印输入的最长字符串.

您将程序设置为在getline返回0时停止读取行,但是,如果查看getline,您会发现它始终将回车存储在其中.这意味着对于空字符串,长度为1而不是0.
因此,如果您将
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
while( (len=getline(line, MAXLINE))>0)

替换为

while( (len=getline(line, MAXLINE))>1)

,您应该会得到预期的结果.

you should get your expected result.


它确实输出一些内容.您还期望更多吗?

最后唯一输出什么的呼叫是printf.您可能很容易注意到它,因为此应用程序紧随其后.

另一个问题:getline总是返回长度> 0,即使仅按Enter键也是如此.我认为如果用户按下Enter键,您想退出循环;现在,除非用户用Ctrl + C结束循环,否则循环将无限执行,但是不会输出程序结果.也许,返回i − 1而不是i,这样,长度将是字符串长度,不包括以''\n''''\0''结尾的字符串,这对于您的目的是很好的.

好了,在每次调用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


添加#include <windows.h>,因此我们可以使用pause

在主要:
add #include <windows.h>, so we can use pause

in main:
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



和修改后的getline():



and a revised getline():

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.


这篇关于程序即使编译也没有输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆