#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main() {
cout<<"Welcome to "C++"";
cout<<"Welcome to \"C++\"";
return 0;
}

- 555,201
- 31
- 458
- 770

- 1
- 1
-
2A) you should show the error message. Regardless, you have nested double-quotes in your string, which is not valid. – jkb Jun 24 '22 at 23:48
-
1All questions here should have all relevant information ***in the question itself as plain text***. Links can stop working at any time making questions meaningless. Code, data, or errors shown as images cannot be copy/pasted; or edited or compiled for further research and investigation. Can you [edit] this question, removing and replacing all links and images with all relevant information as plain text? All code must meet all requirements of a [mre]. You'll find many other questions here, with a [mre], in plain text. Please use them as an example for how your question should look. – Sam Varshavchik Jun 24 '22 at 23:53
1 Answers
When you do "Welcome to "C++""
, the quotes confuse the compiler. It sees "Welcome to "
, which is the string part, and then it sees C++
(not as part of the string) which confuses it.
Instead, do this:
cout << "Welcome to \"C++\"";
That escapes the double-quote characters. You escape a character by putting a \
before it (a backslash, typically above the Enter key on your keyboard). This means to do something special. In this case, it just means "don't end the string, treat the double-quote as part of it". But it can have other meanings depending on what you are escaping. For example, \n
makes a newline. I explain a bit more in the next paragraph.
Warning: don't go putting a \
(backslash a.k.a. whack) before every character in your string. Depending on what character you put the \
before, it may do something different. For example, just doing \"
makes a double-quotation-mark. Doing \n
makes a newline. See this other answer for a list of them.
Side note: although it isn't required in C/C++, I suggest (consistently) indenting your code for readability

- 555,201
- 31
- 458
- 770

- 518
- 3
- 9
- 21
-
Another option is to use a [raw string literal](https://en.cppreference.com/w/cpp/language/string_literal) instead, then you don't have to escape characters, eg: `cout << R"(Welcome to "C++")";` Everything between the `()` is treated as-is as the string's content. – Remy Lebeau Jun 25 '22 at 00:15
-
@RemyLebeau I'm not familiar with that (I almost only use C, not C++), but if you posted that as an answer, I'd upvote it though :) – cocomac Jun 25 '22 at 00:17