Simple Script causing error

This is a discussion on Simple Script causing error within the C++ Programming forums, part of the General Programming Boards category; Hey everyone. I just started learning C++ and I'm reading the tutorial here at cprogramming. I tried to use the ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    22

    Simple Script causing error

    Hey everyone. I just started learning C++ and I'm reading the tutorial here at cprogramming. I tried to use the script in one of the examples. Here is the source:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      cout<<"HEY, you, I'm alive! Oh, and Hello World!\n";
      cin.get();
    
      return 1;
    }
    When I try to Compile this I get an error that says '[Warning] no newline at end of file'. I am using Dev-C++. What is wrong with this?

  2. #2
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Some compilers prefer that your source files end with a newline character.
    Last edited by LuckY; 12-22-2004 at 04:07 PM. Reason: Misstated

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    22
    Ha that fixed it. Thanks LuckY .

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Really? And is this standard behaviour, or just pointless thoughtless laziness on the part of the compiler writers?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    22
    Hmm I just tried another one of the examples and I'm getting the same error. Here's the source.
    Code:
    #include <iostream>	
    
    using namespace std;
    		
    int main()                            // Most important part of the program!
    {
      int age;                            // Need a variable...
      
      cout<<"Please input your age: ";    // Asks for age
      cin>> age;                          // The input is put in age
      cin.ignore();                       // Throw away enter
      if ( age < 100 ) {                  // If the age is less than 100
         cout<<"You are pretty young!\n"; // Just to show you it works...
      }
      else if ( age == 100 ) {            // I use else just to show an example 
         cout<<"You are old\n";           // Just to show you it works...
      }
      else {
        cout<<"You are really old\n";     // Executed if no other statement is
      }
      cin.get();
    }
    I tried adding a few more lines at the end, but I still get the error. [Warning]no newline at end of file.

  6. #6
    Registered User
    Join Date
    Apr 2004
    Posts
    22
    Hmm I found the problem. It seems you can only add one new line at the end of the program . I was trying to add a couple, and that messes things up.

  7. #7
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Quote Originally Posted by Hunter2
    Really? And is this standard behaviour, or just pointless thoughtless laziness on the part of the compiler writers?
    As far as I'm aware it is not a part of the C++ standard, if that's what you're asking. My guess is that it is included simply to warn programmers of potential problems.

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

    LuckY has a good point.
    Code:
    newline not last character in file
    	Type: Warning -- Options: all
    	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.
    I believe that warning exists to avoid empty source files and make sure the file contains at least a newline.


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

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    >> My guess is that it is included simply to warn programmers of potential problems.

    or maybe the (unsophisticated) lexical scanner on some compilers expect it so that last line of code gets a line number associated with it?



    ITSA
    Socket Library!

  10. #10
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    the newline thing is supposedly a UNIX convention. the latest build of dev C++ for some reason seems to want to incorporate it - as if i really wanted all those redundant CLI msges in my IDE - I i did i would have just used MINGW.
    Its annoying if nothing else. plus someimes your build prduces a "gmon.out" file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 03:09 AM
  3. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  4. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 03:49 AM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM

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