“文件末尾没有换行符”警告,即使在换行之后

发布于 2024-11-25 15:52:44 字数 1414 浏览 2 评论 0原文

最近我一直在尝试学习C++,直到今天一切都很顺利。我正在尝试制作一个非常简单的应用程序,基本上只是要求用户提供一个数字,然后显示该数字的阶乘。

当我尝试在 Cygwin 中编译文件(g++ Factorial.cpp -o fact)时,我收到以下警告“警告:文件末尾没有换行符”。按照警告的话,我在文件末尾添加了一个换行符并再次尝试......结果完全相同。无论我尝试一个换行符还是二十个换行符,它似乎都无法编译。

我在下面附加了简单且尚未完成的文件(除非想象它末尾有一个空行。这个菜鸟无法让它显示在代码视图中):

   #include <iostream>
   #include <string>

    using namespace std;

    int getInput();
    void displayFactorial(int);

    int main()
    {
        int number = getInput();
        displayFactorial(number);
        return 0;

    }



    int getInput()
    {

        int userNumber;
        bool okNumber;

        while(okNumber == false){
            cout << "Please eneter a number in the range of 1-10";
            cin >> userNumber;
            if(userNumber >= 1 and userNumber <=10){
                okNumber = true;
            }
            else{
                cout << "Incorrect number" << endl;
            }
        }
        return userNumber;
    }

        void displayFactorial(int number){
            string displayString = number + "! =";
            int total;

            for(int i = 0; i<=number; i++){
                //displayString += "
            }

            cout << displayString;

        }

// File ended the line above this (with a new line. This line added by Martin so it shows)

如果不是,任何想法可能会导致该警告换行符?

I've been trying to learn C++ lately, and all has been going well until today. I'm trying to make a very simple application that basically just asks a user for a number and then displays the factorial of that number.

When I try to compile the file in Cygwin (g++ factorial.cpp -o fact), I get the following warning "warning: no newline at end of file". Following the words of the warning, I added a newline at the end of the file and tried it again...with the exact same result. Whether I try one newline or twenty, it never seems to compile.

I attached the simple, and still unfinished, file below (Except imagine there is a blank line at the end of it. This noob can't get it show up in code view):

   #include <iostream>
   #include <string>

    using namespace std;

    int getInput();
    void displayFactorial(int);

    int main()
    {
        int number = getInput();
        displayFactorial(number);
        return 0;

    }



    int getInput()
    {

        int userNumber;
        bool okNumber;

        while(okNumber == false){
            cout << "Please eneter a number in the range of 1-10";
            cin >> userNumber;
            if(userNumber >= 1 and userNumber <=10){
                okNumber = true;
            }
            else{
                cout << "Incorrect number" << endl;
            }
        }
        return userNumber;
    }

        void displayFactorial(int number){
            string displayString = number + "! =";
            int total;

            for(int i = 0; i<=number; i++){
                //displayString += "
            }

            cout << displayString;

        }

// File ended the line above this (with a new line. This line added by Martin so it shows)

Any idea what could cause that warning if it's not the newline?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

森林迷了鹿 2024-12-02 15:52:44

首先,警告不会阻止程序编译。
第二:你确定编译了你正在编辑的文件吗?
我在大学举办过一些教程,大多数初学者都犯了这个错误。
如果您可以肯定地说您正在编译正在编辑的文件,那么这可能是由不同的换行设置引起的,但我认为这极不可能。

您使用哪种编辑器/IDE?

First of all a warning does not prevent a program from compiling.
Second: Are you sure you compile the file you are editing?
I held some tutorials at my university and most of the beginners made this mistake.
If you can definitely say you are compiling the file you are editing, then it could be caused by different new line settings but I consider this highly improbable.

Which editor/IDE do you use?

一个人的夜不怕黑 2024-12-02 15:52:44

您所要做的就是等待新的 C++0x 标准。源文件末尾不再需要换行符。 (这花了40年?)

All you have to do is wait for the new C++0x standard. A newline is no longer required at the end of the source file. (And this took 40 years?)

谷夏 2024-12-02 15:52:44

大多数基于 Unix 的文本编辑器都会自动添加尾随换行符,但显然
Notepad++(我从未使用过)没有。

文件不需要以空行结尾(这将表示为两个换行符
连续字符),只是一个正确终止的字符。

查看 Notepad++ 是否有一个配置选项,告诉它自动附加换行符
保存文件时,或者至少询问您是否要添加一个。

很好的解释 - 我遇到了同样的问题,只需使用写字板而不是记事本即可解决它:)

Most Unix-based text editors will add a trailing newline automatically, but apparently
Notepad++ (which I've never used) doesn't.

The file doesn't need to end with a blank line (which would be represented as two newline
characters in a row), just a properly terminated one.

See if Notepad++ has a configuration option that tells it to automatically append a newline
when saving a file, or at least to ask whether you want to add one.

Great explanation - I had the same problem and solved it simply by using WordPad instead of Notepad :)

从来不烧饼 2024-12-02 15:52:44

该问题与您的 C++ 代码无关;我几乎可以向您保证,程序中未初始化的布尔值不会导致问题。是源文件本身的问题。 “Hello, world”程序会产生相同的警告。

文本文件(包括 C++ 源文件)由多行字符组成,其中每行均以行尾标记终止。在 Unix 中,因此在 Cygwin(使用默认设置)中,行结束标记是单个 '\n' (换行符、换行符)字符。 (Windows 使用 CR-LF 对 ("\r\n")。

文本文件的最后一行可能以换行符结尾,但是g++ 更喜欢它被正确终止。

大多数基于 Unix 的文本编辑器会自动添加尾随换行符,但显然 Notepad++(我从未使用过)不需要

以空白结尾 。 行(其中将表示为一行中的两个换行符),只是一个正确终止的字符。

看看 Notepad++ 是否有一个配置选项,告诉它在保存文件时自动附加换行符,或者至少询问您是否要添加换行符。

(文件末尾缺少换行符可能是导致您在发布问题时粘贴代码时出现问题的原因。)

The problem has nothing to do with your C++ code; I can almost guarantee you that the uninitialized boolean in your program didn't cause the issue. It's a problem with the source file itself. A "Hello, world" program would have produced the same warning.

A text file, including a C++ source file, consists of lines of characters, where each line is terminated by an end-of-line marker. In Unix, and therefore in Cygwin (with the default settings), the end-of-line marker is a single '\n' (newline, linefeed) character. (Windows uses CR-LF pair ("\r\n").

It's possible for the very last line of a text file not to end with a newline character, but g++ prefers it to be terminated properly.

Most Unix-based text editors will add a trailing newline automatically, but apparently Notepad++ (which I've never used) doesn't.

The file doesn't need to end with a blank line (which would be represented as two newline characters in a row), just a properly terminated one.

See if Notepad++ has a configuration option that tells it to automatically append a newline when saving a file, or at least to ask whether you want to add one.

(The missing newline at the end of the file is probably what caused your problems pasting the code when you posted the question.)

玩心态 2024-12-02 15:52:44

也许是某个看不见的角色出了问题?我复制粘贴您的代码并在 Cygwin 下使用以下 g++ 进行编译,没有任何警告/错误:

$ g++ --version
g++ (GCC) 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)

Maybe it is some problem with some invisible character? I copy-paste your code and gets compiled without any warnings/errors, under Cygwin with the following g++:

$ g++ --version
g++ (GCC) 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)
善良天后 2024-12-02 15:52:44

最终问题是没有将布尔变量初始化为 false。但是,我不确定是否实际上是初始化问题,或者只是如果您只是在文件末尾附加空格,文本编辑器可能实际上不会保存文件。对于那些想知道的人来说,这在 Notepad++ 中发生在我身上。

It ended up that not initializing a boolean variable to false was the issue. However, I'm not sure whether it was actually the initialization that was the problem, or just the fact that the text editor might not actually save a file if you just append whitespace to the end of it. For those wondering, this was happening to me in Notepad++.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文