C++ << std::end1;

Discussion in 'Web Design & Programming' started by Waffle, May 28, 2006.

  1. Waffle

    Waffle Alpha Geek

    Likes Received:
    38
    Trophy Points:
    0
    Starting to learn C++, seems like a fairly good learning curve.

    Anyway, ran in to an error when using code from a book:

    PHP:
    #include <iostream>

    int main()
    {
      
    std::cout << "Hello world!\n"// new line
      
    std::cout << "this is on a new line.." 
      
    return 0;
    }

    that works fine :eek:

    then i read about using std::end1; -

    PHP:
    #include <iostream>

    int main()
    {
      
    std::cout << "Hello world!" << std::end1;
      
    std::cout << "this is on a new line.." << std::end1;
      
    std::cout << "and another";
      return 
    0;
    }
    this returns an error message:

    E2316 'end1' is not a member of 'std' in function main() at line xx


    Suggestions why?
     
  2. Addis

    Addis The King

    Likes Received:
    91
    Trophy Points:
    48
    Waffle, the endline function is spelt with an l at the end, not a 1.

    So the statement would be:
    Code:
    cout << "testing testing" << endl;
    cout << "New line this is";
    cin.get(); //waits for user input
    
    Also, you should try putting the statement
    Code:
    using namespace std;
    above your int main() function, after the include headers so that you don't have to use the std:: bit before using the cout and cin objects.
     
  3. Waffle

    Waffle Alpha Geek

    Likes Received:
    38
    Trophy Points:
    0
    An L? I'll be damned. Looked just like a one I didn't give it a second thought.

    I'll give that statement a shot as well - thanks :)
     
  4. Karanislove

    Karanislove It's D Grav80 Of Luv

    Likes Received:
    0
    Trophy Points:
    36
    In these types of functions, you can further reduce your lines... because you are not returning any value so instead of using...int main()...you can use void main() and your program will not look for a return value and you can remove that return 0 at the end.....
    using "getch();" command will help in getting out of your program.....just use it in your function at the end.....
     
  5. Addis

    Addis The King

    Likes Received:
    91
    Trophy Points:
    48
    Karanislove, using
    PHP:
    void main()
    is not recommended. ANSI C++ states that main must return an integer. With C++ being a very fast compiled language, theres no need to do that. Many compilers like gcc will give you a warning for not returning a value. In java, using void main is also illegal.
     
  6. Karanislove

    Karanislove It's D Grav80 Of Luv

    Likes Received:
    0
    Trophy Points:
    36
    I never had any problem with this function using Visual Studio 2003.net
     
  7. Addis

    Addis The King

    Likes Received:
    91
    Trophy Points:
    48
    Thats because the VC++ compiler allows it, but if you want to build portable applications, its better to use standard ANSI C++ code.
     
  8. vol7ron

    vol7ron Guest

    I like void main() as well.

    its endl instead of end1 because the L stands for line. Also you should include the library extension. For instance <iostream.h>

    Why are you learning C++? It's dated. Move on to C#.
     
  9. Addis

    Addis The King

    Likes Received:
    91
    Trophy Points:
    48
    There's absolutely no point in using void main(), if you want to be a good developer, stick to standards when possible, especially something as trivial as that. VC might allow it, but gcc in Linux won't. And to prove it, here's the output of using iostream.h, with void main and no return statement on gcc4.
    Code:
    In file included from /usr/lib/gcc/i586-mandriva-linux-gnu/4.0.1/../../../../include/c++/4.0.1/backward/iostream.h:31,
                     from voidmain.c:1:
    /usr/lib/gcc/i586-mandriva-linux-gnu/4.0.1/../../../../include/c++/4.0.1/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated.
    voidmain.c:5: error: '::main' must return 'int'
    
    The reason you don't put .h after the iostream is because iostream is for C++, not C. All C++ STL's have just the format #include <name>. To include C headers, you leave off the .h and prefix the header with "c". E.g. #include <cstdio>
    C++ is definitely not dead, it may be older than C# but it still has many many uses. KDE is primarily written in C++.

    Why use the Unix security system when its dated and Windows is newer?
     
  10. harrack52

    harrack52 Supreme Geek

    Likes Received:
    5
    Trophy Points:
    0
    You can also use \n inside your quotation marks (the "new line" character), it makes the code easier to read and faster to write.

    \t for tabulations, as an FYI.

    I understand c++ is much more powerful, but with Java you get the API for free, which is quite useful. After learning it a bit moving to c++ will be easier.
     
  11. achi

    achi Guest

    ha ha!I am very stupid for this small mistake.
    thanks for sharing.
     

Share This Page