1

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?

Martin York
  • 257,169
  • 86
  • 333
  • 562
Luke
  • 648
  • 7
  • 15
  • Not sure what causes the warning, but you don't initialize `okNumber` to 'false' before you (try to) enter the loop, so the `while` loop may never run. Oh, and perhaps it means you don't do `<< endl` at the end of `displayFactorial()` – Rudy Velthuis Jul 24 '11 at 21:26
  • Did you save the file before recompiling it? – Tamer Shlash Jul 24 '11 at 21:29
  • @Rudy Velthuis: Missing `<< endl` can't be the reason. see [This](http://stackoverflow.com/questions/72271/no-newline-at-end-of-file-compiler-warning). Besides what if I used `\n` instead? – Tamer Shlash Jul 24 '11 at 21:37
  • @Mr.T: I was wildly guessing. I don't have the Cygwin compiler installed here. – Rudy Velthuis Jul 24 '11 at 21:44
  • FWIW, you have a comment `//displayString += "`. Is that a comment in the function too, or is it code? Looks like a non-terminated string literal. – Rudy Velthuis Jul 24 '11 at 21:47
  • Wow, initializing okNumber actually fixed the issue. I really didn't think it would do anything, but it's working like a charm now. Thanks, Rudy! Maybe my text editor didn't actually save the file when I just added whitespace, but did after I added text. Not sure. (On a side note, is there a way that I can mark a comment as the accepted answer?) – Luke Jul 24 '11 at 22:26
  • @Luke: You can only accept answers as answers. Just post an answer with the solution yourself and then mark it as the answer. Maybe your editor does strip trailing whitespace as unneeded. As I already asked: Which editor do you use? – Nobody moving away from SE Jul 25 '11 at 09:23
  • Exactly *how* did you add a newline to the end of the file? – Keith Thompson Aug 02 '11 at 00:45
  • You literally need the last character in the file to be a \n (new line char). It's easy to not see some extra space after it. Use some text editor that shows all characters, including blank spaces and tabs and do make sure it ends with a \n – Daniel J. Jul 05 '23 at 16:30

6 Answers6

2

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 :)

Community
  • 1
  • 1
Holger
  • 21
  • 1
2

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?

2

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

TonyK
  • 16,761
  • 4
  • 37
  • 72
  • It's actually a big deal when dealing with header files. But most compilers have been able to work fine without the extra blank line for a decade or more now. – Martin York Jul 24 '11 at 23:46
1

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.)

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
0

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)
gfour
  • 959
  • 6
  • 9
  • The thing is, this kind of problem can easily disappear when transmitting source code over the net... whether or not his source code has a trailing newline, you can easily copy the copy here in a way that includes or excludes a newline... –  Jul 24 '11 at 21:30
0

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++.

Luke
  • 648
  • 7
  • 15