Why need to write \n and " in same line?

I am getting an error message when I type this code
#include

int main()

{

std::cout << "Hello\n

";

std::cout << “Goodbye\n”;

}

Error :
typeoperator<<(basic_ostream<_CharT, _Traits>& __os,
^~~~~~~~
/usr/include/c++/7/bits/basic_string.h:6284:5: note: template argument deduction/substitution failed:
pattern.cpp:8:8: note: ‘std::ostream {aka std::basic_ostream}’ is not derived from ‘const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’
std::cout << “Goodbye\n”;
^~~~

Can someone tell what is the mistake in the code.

Formatting counts.

int main()
{
    std::cout << "Hello\n";
    std::cout << "Goodbye\n";
}

This will run perfectly. Some languages are very open to brute formatting, others not so. At least it makes for cleaner more readable code, and will help when you need to transition to other languages. For future reference you can format your text here too by clicking </> before pasting the code. It helps to troubleshoot errors better.

His main code works perfectly, but to know the real problem, you must study the error well. The error says that there is a problem with the ostream, which is the c++ standard library. Because you did not include the library properly, all your code becomes void. However, there is an easy fix. Just add the ostream library to your file like this:

#include <iostream>

Put this on the beginning of your code and it will work perfectly. I also recommend specifying the name space of your code so you don’t have to be typing std:: all the time.
Here is your code but in a perfected format:

#include <iostream>
using namespace std;
int main() {
  cout << "Hello\n";
  cout << "Goodbye\n";
  
  return 0;
}

Hope this helped!

1 Like

Sure, but this was what I was referring to.


Screen Shot 2020-07-21 at 9.31.54 AM

Screen Shot 2020-07-21 at 9.30.26 AM


Sublime also throws an error

Yeah, you are right. But I still feel that the main problem in his code was the library, as I read through the error thoroughly, and I am an experienced C/C++ debugger. I have dealt with these kind of errors many times in the past so I knew what it was immediately I saw it.

1 Like