I've been trying to learn C++ for about a year now and came across this excellent tutorial. I can't leave anything alone so I changed some single quotes to double quotes and have the following code:

#include <iostream>

void printTwice(char phil) {
	std::cout << phil << phil << std::endl;
}

int main() {
	std::cout << "First line." << std::endl;
	printTwice('a');
	printTwice("a");
	std::cout << "Last line." << std::endl;
	return 0;
}

...which gives me (under Fedora 13 GNU/Linux and the latest GCC):

[jw0@localhost ~]$ g++ -o program.exe program.cpp
program.cpp: In function ‘int main()’:
program.cpp:10: error: invalid conversion from ‘const char*’ to ‘char’
program.cpp:10: error:   initializing argument 1 of ‘void printTwice(char)’

If I remove printTwice("a") then it works fine. I did some Googling for the differences between const char* and char but I couldn't extrapolate from the various forum posts why single quotes work and double ones don't. I would be really grateful if someone could let me know - I can carry on the tutorial without it but it would be interesting to find out.

Thanks in advance for any help/tips/pointers :)

A character surrounded by single quotes is a single character value; a pair of double quotes indicates that its enclosed data is a string literal, which is represented in memory as a contiguous sequence of characters (a.k.a an array of characters)

Strings and single characters are like apples and oranges - for historical reasons to do with the way the 'C' language works, raw string data involves an additional invisible 'nul' character, which represents end-of-sequence. The difference in memory between 'a' and "a" is that "a" is actually two characters.

On an unrelated note, I had a brief look at the tutorial you said you're learning from; Is that your only learning resource? There are an awful lot of mistakes/errors on the page - it doesn't look like a particularly good tutorial to me - be aware that the things its teaching you are likely to be bad in C++ (or in some cases they may be wrong altogether).

Look above at bench post.

A character surrounded by single quotes is a single character value; a pair of double quotes indicates that its enclosed data is a string literal, which is represented in memory as a contiguous sequence of characters (a.k.a an array of characters)

Strings and single characters are like apples and oranges - for historical reasons to do with the way the 'C' language works, raw string data involves an additional invisible 'nul' character, which represents end-of-sequence. The difference in memory between 'a' and "a" is that "a" is actually two characters.

On an unrelated note, I had a brief look at the tutorial you said you're learning from; Is that your only learning resource? There are an awful lot of mistakes/errors on the page - it doesn't look like a particularly good tutorial to me - be aware that the things its teaching you are likely to be bad in C++ (or in some cases they may be wrong altogether).

Thanks for the reply, bench. I am always on the lookout for other C++ resources to learn from and have so far used Jesse Liberty's C++ in 21 Days, Herb Schildt's C++ Complete Reference and a few other web resources. I had a suspicion this tutorial was flawed as it didn't mention namespaces at all, nor why they chose doubles over floats (I found that out myself). I also didn't hear great feedback about Schildt's work and was unsure of Liberty's.

Are there any books/resources you can recommend to learn C++? I have a basic understanding of variables and functions (gained from learning Object Oriented PHP) but I come unstuck on namespaces, objects, templates, structs, types and pointers. I came across this list on DaniWeb, but I don't know how accurate those resources are.

Thanks again.

That list is probably about the best advice on book recommendations that you're going to find; the first book on the list, Accelerated C++, is widely recognised as being the one of the best C++ beginner books available, particularly if you're familiar with "the fundamentals" (i.e. loops/functions/etc) from another language

You might be interested in this link too: http://www.parashift.com/c++-faq-lite/how-to-learn-cpp.html

That list is probably about the best advice on book recommendations that you're going to find; the first book on the list, Accelerated C++, is widely recognised as being the one of the best C++ beginner books available, particularly if you're familiar with "the fundamentals" (i.e. loops/functions/etc) from another language

You might be interested in this link too: http://www.parashift.com/c++-faq-lite/how-to-learn-cpp.html

Thanks :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.