-3

My code is a basic HelloWorld but fails to compile when I use cout<<endl.

I'm using Microsoft visual studio fresh download and created a console application for my first test project.

    // Test1ConsoleApplication.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <string>
#include <iostream>
//#include <ostream>


using namespace std;


int main()
{
    string s = "hello world!!";
    cout << "lets see: " << s << endl;
    return 0;
}

It generates a

"C1001" at line 1.

Replacing "endl" with ""\n"" works though.

wscourge
  • 10,657
  • 14
  • 59
  • 80
  • 2
    The example given compiles just fine. I suspect there is an error in stdafx.h – Jive Dadson Jan 20 '18 at 06:56
  • @MayorJellies - Hello again! If you've found an answer here useful, please don't forget to accept one and vote it up. In doing so, you both get positive reputation and it helps future visitors to your question determine which answer is the most appropriate/helpful. If you've got any question about this, check out this link: [how-does-accepting-an-answer-work](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – Brien Foss Mar 20 '18 at 23:43

3 Answers3

1

You don't need the precompiled header #include <stdafx.h> so you can safely get rid of it. Also get rid of using namespace std; because it pollutes the global namespace. Try something like this. There's no reason it shouldn't work.

#include <string>
#include <iostream>

using std::string;
using std::cout;
using std::endl;

int main()
{
    string s = "hello world!!";
    cout << "lets see: " << s << endl;
    return 0;
}

In Visual Studio you can disable use of the precompiled header in the project settings.

Justin Randall
  • 2,243
  • 2
  • 16
  • 23
0

I do not see what the problem is. Both options compile and execute for me. RexTester cppOnline

// Test1ConsoleApplication.cpp : Defines the entry point for the console application.
//
//#include "stdafx.h"
#include <string>
#include <iostream>
//#include <ostream>


using namespace std;


int main()
{
    string s = "hello world!!";
    cout << "lets see: " << s << endl;
    cout << "lets see: " << s << "\n";
    return 0;
}
Brien Foss
  • 3,336
  • 3
  • 21
  • 31
0

So idk what was causing the error but it was fixed after pasting imports to the "stdafx.h" header file and then delete them...