-2

I am trying to teach myself C++ with an old book I've had lying around and have multiple errors trying to run the initial HelloWorld program as written in community visual studio 2017. Here is the code.

#include <iostream.h>

int main()
{
    cout << "Hello World!\n";
    return 0;
}

When I try to run the program, I get the following errors:

C++ errors

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
Dez M
  • 57
  • 5

3 Answers3

5

I assume you have a book about Turbo C++? Ditch it and try one of a good C++ books instead.

A well written HelloWorld in C++ looks like this:

#include <iostream>

int main()
{
    std::cout << "Hello world!" << std::endl;
    return 0; //optional
}
Yksisarvinen
  • 18,008
  • 2
  • 24
  • 52
  • 1
    endl is also optional, and discouraged (by e.g. C++ Core Guidelines) – Cubbi Sep 28 '18 at 17:58
  • It's "Teach Yourself C++ in 21 Days", published 1997. I knew it was old, but it was something I already owned. Guess i'll be ponying up for one of those new books. – Dez M Sep 29 '18 at 17:39
  • @DezM C++ standard was created in 1998, so yeah, I'd strongly recommend a newer book to learn. You won't learn C++ from pre-standard books. – Yksisarvinen Oct 01 '18 at 11:43
  • @Cubbi I agree, but it's readablity vs. performance here. Flushing stream is not much of a deal in such short program, and `std::endl` is easier to read than "\n" IMO. We could omit it fully as well. – Yksisarvinen Oct 01 '18 at 11:47
-2

There are two ways to do it. I wrote code in VS2017, 1.

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
    cout << "Hello World!\n";
    return 0;
}

2.

#include "stdafx.h"
#include <iostream>

int main()
{
    std::cout << "Hello World!\n";
    return 0;
}

for your reference https://en.cppreference.com/w/cpp/io/cout I highly recommend you https://www.amazon.com/Programming-Principles-Practice-Using-C/dp/0321543726

Praveer Kumar
  • 912
  • 1
  • 12
  • 25
-3

Use as "< iostream>".You can also use search functionality to check if similar question is asked :)

paypaytr
  • 199
  • 1
  • 1
  • 12