place for code

maybe this is a stupid question, but I have started microsoft visual C++ today (c++ itself also for the first time) and i dont know where to put the code. i'm making the program hello world, but it doesnt work it asks if i want to 'build' the programm and i dont know what it means. i named the program hello and i put the code in the place where it says: hello.cpp is this the right place if so, what am i doing wrong

this is the code:

// hello.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])

{

return 0;

}

#include <iostream.h>

int main()

{

cout << "Hello World!\n";

return 0;

}

is this the right code everything before #include <iostream.h> was already there.



Answer this question

place for code

  • Stan Bibbs

    Try to be a little more descriptive. How is the window titles where you see "hello.cpp failed" Is it a window or just the status bar Do open the Output window. Use the view menu if you cannot find it anywhere open already. Copy and paste all the text of the output window here after you did a build which failed.

    VC++ can compile pure C++ code from your samples - just don't mix their code with code from the VC++ project wizards. If you have an example in your book and created a wizard based project just remove all the code of the wizard and only place the code of your book sample in the main cpp file. If you get compiler errors than your book likely uses none standard C++ and you need to fix that with the help of the compiler errors which should be more elaborate than "some.cpp failed". Just find the right place where the errors are printed to.

    --
    SvenC


  • barkingdog

    Buy a book that's targeted to VC++. The questions you're asking are outside the scope of this forum.



  • MrSock

    Where do you see "hello.cpp failed" VC has a "Error List" window and an "Output" window. Normally below your source code window. If not you should find them from the View menu. Do these windows contain more detailed error information

    --
    SvenC


  • li4li4tsai4

    hmm. all i get to see is: hello.cpp failed. i clicked on build programm. is that right thats what your supposed to do right i mean nobody ever told me what to do, all i have is a book that is oriented to normal c++ and a vc++ compiler. i dont understand anything of the compiler.
  • Sunil_Kumar_63c25f

    duck thing wrote:

    Buy a book that's targeted to VC++. The questions you're asking are outside the scope of this forum.

    but the language C++ is the same as VC++ right the only difference is the editor, right or is this wrong if so i guess ill have to trade the book or get a C++ compiler.


  • xyligun

    yea, i know i need to be more specific, but i can't get to the computer thhhat has c++ and i'm not allowed to download it on this one, so i'll have to wait a little bit.


  • kevlar623

    No, no - VC++ is a good choice in my eyes. It gets more and more standard compliant, so with new books teaching C++ according to the ISO standard those sample should work with VC++.

    What error do you get after the last modifications of your sample code Just paste your code and the error messages which should not be too much for a Hello World app. Then we figure out the problems and might explain some things which are unclear to you.

    --
    SvenC


  • Fusion54

    well the book: the .h. he said that was only for old compilers and new ones dont need it. so i tried to do it wothoutthat too but it didn't work.

    are you saying I need to get a C++ compiler instead of a VC++ compiler because someone told me it doesnt matter if u have c++ or vc++. that it only matters what the compiler itself looks like and how u work with it and the code doesnt get affected by it. is this not true

     

    edit: btw the book is from 2001. is this old for a programming book i don't know this since i know NOTHING <<<<<< of programming languages. (except that u can use them to make games or to put the tekst: hello world on a screen)


  • Eric van Feggelen

    Well if you start a VC++ project with the wizard it will create minimal code for you which includes the main function. As Ovidiu pointed out _tmain is a macro which expands to main or wmain to support multi byte or unicode builds from one source.

    This is really VC++ specific, so your book might not care about that. You just have to know that these unicode/multi byte extensions exit in VC++ and that you sometimes need to "adjust" code to work in VC++ instead of just copying it over.

    Your book seems a bit pragmatic sometimes, e.g. with the .h stuff. Older compilers with old STL versions might have been delivered with headers like that but current versions should not have that any more.

    --
    SvenC


  • Joe Albahari

    below the source code window.
  • Cute_Celina

    No, ther is not the right code.
    First, to clarify a little:
    _tmain is a macro that expands in the preprocessor phase to main or wmain which are entry points for non-UNICODE or UNICODE builds, respectively (see main: Program Startup in MSDN).

    You don't need to add another main function beside already existing _tmain.

    So, your code have to look like this one:

    //hello.cpp : Defines the entry point for the console application.
    //
    #include "stdafx.h"
    #include <iostream>
    
    int _tmain(int argc, _TCHAR* argv[])
    
    {
      std::cout << "Hello World!\n";
      return 0;
    }

    (EDIT) ... and indeed, as SvenC already pointed, you have to include the standard header <iostream> and use std namespace.



  • r3n

    Many books are written long time ago,
       many authors are old,
          then show you obsolete/deprecated things.

    Don't trust 100% in everything you read!
    Do not copy/paste everything you find!
    Just also think yourself!


  • S&amp;#233;bastien Nunes

    Put the code in _tmain and remove main or the other way around. Have only one of those functions

    #include <iostream> // no .h at the end

    Either use

    using namespace std;

    or fully qualify cout which is part of std:

    std::cout << "Hello World1\n";

    --
    SvenC


  • MLyons10

    but i have bought this book and it says i have to put this in:

    #include <iostream.h>

    int main()

    {

    cout << "Hello World!\n";

    return 0;

    }

     

    and the .h only if without the .h it doesnt work

    and since the other stuff was alreadt there, can't i just remove that i dont know what its for and the book didnt say anything about that. it said the code should look like the above code.


  • place for code