Computer Science Canada

question

Author:  Geminias [ Fri Sep 23, 2005 5:12 pm ]
Post subject:  question

compiler: mingw

when i compile this source code i get a "warning: no newline at end of file"

the prog still works, but i got this warning and was wondering what it means exactly.

code:
#include <iostream>
using namespace std;

int division (int x, int z=2)
{
        int r;
        r= x/z;
        return (r);
}
int main ()
{
        std::cout<< division (10)<<std::endl;
        std::cout<< "division using both arguments"<<division (20,5);
        return 0;
}

Author:  Mazer [ Fri Sep 23, 2005 5:46 pm ]
Post subject: 

Why do you declare that you're using the standard namespace and then go ahead and use "std::" in front of cout? I'm no compiler-error expert, but maybe is't just some crazy problem because of that? (at the very least the code might make a bit more sense).

Author:  [Gandalf] [ Fri Sep 23, 2005 5:50 pm ]
Post subject: 

Works fine for me.

You should always indent, use proper whitespace, the point of "using namespace std" is so that you don't need to use std::, and your function could be written better as:

code:
int division (int x, int z = 2)
{
    return (x/z);
}

Author:  1of42 [ Fri Sep 23, 2005 5:51 pm ]
Post subject: 

It means what it says. There is no newline at the end of the file. Put one there, and it will compile without warnings.

Author:  Geminias [ Fri Sep 23, 2005 6:50 pm ]
Post subject: 

lol.. i didn't really know what "use namespace std;" really did, i was just having trouble compiling and i saw this other program that had that so i decided i would just copy it in there LOL.

good to know anyways!

(ps. i do indent and organize my code neatly but when i copied/paste it messed it up)

you say include a newline at the end, like what do you mean just type "newline" at the end lol?

Author:  rizzix [ Fri Sep 23, 2005 6:53 pm ]
Post subject: 

yes you do.. just that you used [quote] instead of [code] or [syntax="cpp"]

Author:  wtd [ Fri Sep 23, 2005 6:54 pm ]
Post subject: 

C and C++ are meant to work on many different hardware platforms, running many different operating systems. Many of these operating systems don't consider a text file properly formatted unless it ends in a newline.

This is why GCC (G++ is part of GCC) warns about the lack of a newline.

Author:  rizzix [ Fri Sep 23, 2005 8:06 pm ]
Post subject: 

wtd wrote:
Many of these operating systems don't consider a text file properly formatted unless it ends in a newline..
huh?

Author:  wtd [ Fri Sep 23, 2005 8:09 pm ]
Post subject: 

I believe that VMS is such a system. The IO system gets all confused if plain text files don't end in a newline.

Author:  rizzix [ Fri Sep 23, 2005 8:11 pm ]
Post subject: 

oh. weird.

Author:  Monstrosity_ [ Fri Sep 30, 2005 4:06 pm ]
Post subject:  Re: question

Geminias wrote:

when i compile this source code i get a "warning: no newline at end of file" the prog still works, but i got this warning and was wondering what it means exactly.

I dont use much c++, I use c instead. But I do know they have some of the same "rules" in the standards. Somewhere in section 2 should state that having no newline or having a newline preceded by a backslash character at the end of your source file results in undefined behaviour.


: