"No newline at end of file"???

This is a discussion on "No newline at end of file"??? within the C++ Programming forums, part of the General Programming Boards category; I'm still on chapter 6 (gotta wait until payday to get a new book...) and I'm doing the exercises. The ...

  1. #1
    Utter insanity
    Join Date
    Jan 2004
    Posts
    19

    "No newline at end of file"???

    I'm still on chapter 6 (gotta wait until payday to get a new book...) and I'm doing the exercises. The first 4 have you creating a class Employee with three data members, methods to access them, ect... I get the warning "1 E:\Cpp\Source\e61a5.cpp:60 [Warning] no newline at end of file " when I try to compile. I would prefer to treat warnings as errors, since I know it may cause something I don't want, and later on down the line I don't want to be digging through 3 million lines looking for a bug that occured only because I ignored a warning in an earleir build... I'll post the code if y'all want; it's somewhat long, and otherwise error free. Thanks again.
    "I merely took the energy it takes to pout and wrote some blues." - Duke Ellington

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Just taking a wild stab at it here...maybe you need a newline at the end of your file e61a5.cpp. Then again, I've never used your compiler.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Hello,

    I believe that warning exists to avoid empty source files and make sure the file contains at least a newline at the end of the file.
    Code:
    newline not last character in file
    	Type: Warning
    	Every non-empty source file and header must consist of complete lines.
    	This diagnostic warns that the last line of a file did not end with a newline.
    You can simply fix this warning by adding a new line at the end of your source file(s) and header file(s).


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  4. #4
    Utter insanity
    Join Date
    Jan 2004
    Posts
    19
    I tried putting an additional newline at the end of main; but not outside of it. Maybe that'll fix it. Thanks.

    This compiler is supposedly ASCI standard; but then, so is my book; and I've ran over enough screwups (that I can see; and fix somewhat) that I'm beginning to think SAM's dosn't have an editorial dept. I'd reinstall borland, but my CD-RW is overheating. I'm afraid it will eat the disc.

    edit: I didn't see stack's post before I posted. There is a newline character at the end of the last line of main. I added another one next to it and it did nothing. I also added a seperate cout statement at the end of main, with nothing but a \n in it; didn't help. adding a cout statement with just the newline char to the class just caused an error. This is the code as of the last adjustment; which removed the error and added several \n chars to the end of the thing.

    Code:
    #include <iostream>
    using namespace std;
    
    class Employee
    {
          public:
                  //Using default constructor/destructor
                  //public data
                  int ageX;
                  int yearX;
                  int SalX;
                  //Accesor methods
                  int getAge()const   { return ageX;}
                  int getYears()const { return yearX; }
                  int getSal()const   { return SalX; }
                  //Data entry methods
                  void setAge(int age) { ageX = age; }
                  void setYears(int yearsOfService) { yearX = yearsOfService; }
                  void setSal(int Salary) { SalX = Salary; }
                  //Rounding employees' salary
                  int getRoundedSal()const  { return (Salary+500) / 1000; }
          private:
                  //private data
                  int age;
                  int yearsOfService;
                  int Salary;
                 
    };
    
    int main()
    {
        //Employees
        Employee Frank;
        Employee Joe;
        //Employees' age
        Frank.setAge (21);
        Joe.setAge (19);
        //Employees' years of service
        Frank.setYears (3);
        Joe.setYears (2);
        //Employees' salary setting
        Frank.setSal (24762);
        Joe.setSal (23944);
        //Employees' Salary records
        cout << "Frank Hardy\n";
        cout << "Frank is " << Frank.getAge() << " and has been working here ";
        cout << Frank.getYears() << " years.\n";
        cout << "Frank makes " << Frank.getSal() << " dollars; which is ";
        cout << Frank.getRoundedSal() << " rounded off.\n";
        cout << "\n\n";
        
        cout << "Joe Hardy\n";
        cout << "Joe is " << Joe.getAge() << " and has been working here ";
        cout << Joe.getYears() << " years.\n";
        cout << "Joe makes " << Joe.getSal() << " dollars; which is ";
        cout << Joe.getRoundedSal() << " rounded off.\n\n";
        cout << "\n\n\n\n\n";
        return 0;
    }
    Last edited by domhnall4h; 12-30-2004 at 11:47 AM.
    "I merely took the energy it takes to pout and wrote some blues." - Duke Ellington

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    You don't need a newline in the output. You need a newline in your actual code. Go to the } that ends main() and hit Enter

  6. #6
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Hello,

    By adding a new line, I was referring to the carriage return. Like pressing the enter key at the last line of your file. For instance:

    // Wrong
    [code begin]
    int main() {

    }
    [code ends here]

    // Right
    [code begin]
    int main() {

    }

    [code ends here]

    Simply hit the return key at the last line of your file. In this case, I'm guess the last line of your file ends with the }. Rather it should end with a blank line, a new line.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  7. #7
    Utter insanity
    Join Date
    Jan 2004
    Posts
    19
    "1 E:\Cpp\Source\e61a5.cpp:64 [Warning] no newline at end of file"

    That's with the extra line at the end of main. I'm beginning to believe this is a bug within the compiler's current version. this is the first prog I'll have compiled since I updated it. I'm going to check against some of the older progs that did compile fine previously, and see if it pops up.

    The closing brace for main is on line 59. the file ends on line 68.

    Thanks y'all.
    "I merely took the energy it takes to pout and wrote some blues." - Duke Ellington

  8. #8
    Registered User
    Join Date
    Dec 2004
    Posts
    73
    If you're using Dev-C++, I have always gotten the newline warning. My programs ran just fine with it, so I don't think there is anything to worry about.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  2. how to obtain first character of every other word
    By archie in forum C++ Programming
    Replies: 8
    Last Post: 02-18-2002, 01:58 PM
  3. Collision with quads?
    By SyntaxBubble in forum Game Programming
    Replies: 6
    Last Post: 01-18-2002, 06:17 PM
  4. Character in Array to end for loop.
    By mattz in forum C Programming
    Replies: 6
    Last Post: 12-04-2001, 11:05 AM
  5. fgets and a bothersome newline char
    By ivandn in forum Linux Programming
    Replies: 1
    Last Post: 11-14-2001, 01:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21