View Full Version : C++ question, preferred usage single quotes vs. double quotes
Corey
07-24-2004, 11:48 PM
I notice in my Learning C++ book it has some examples which say:
cout << someVar << "\n";
But also some which say:
cout << someVar << '\n';
They both work, but if someone can tell me which one is the preferred real world usage I would appreciate it. No sense in forming bad habits if I can avoid it. Thanks. :)
Corey Milner
Creative Director, Indigo Rose Software (http://www.indigorose.com)
Brett
07-26-2004, 09:33 AM
I rarely see single quotes used for strings. I have never used one myself (in C++ - I do use them in PHP sometimes for some reason.)
Overall, I would say that "double quotes" are the standard.
As far as I understand it (and I may be wrong or just thinking about C) but single quotes surround a character and double quotes surround a string.
char ch = 'c';
char* ch = "My String";
I'm guessing that in the example that you used either a character or a string could be used.
Corey
08-15-2004, 07:27 PM
That's *exactly* what I was asking. Confusing in a sense, well not confusing really but just odd. Other than that everything seems to be identical to PHP so far, but that one thing is an anomaly. Ah well, time constraints have diverted my C++ thing indefinitely anyhow. Thanks Mark! :)
Corey Milner
Creative Director, Indigo Rose Software (http://www.indigorose.com)
Well then, I guess to finish this toppic off...personally I always use the character method (single quotes) if I am only using one character. But if I need a whole string (which is more common anyways) I use the double quotes.
So, for a CString example:
CString strMark = "Hello everyone";
int nPos = strMark.Find('o');
nPos = strMark.Find("eve");
Lorne
08-19-2004, 01:06 PM
Note that in C/C++ a character and a string are two different things.
'a' is a single byte, with no null character at the end.
"a" is a string, consisting of two bytes, 'a' followed by '\0' which terminates the string.
Not that confusing if you just knew C/C++, but since we all know so many friggin' similar languages, a bit of confusion is understandable. :)
Corey
08-22-2004, 06:36 AM
Here's a photo which contains both a string and a character. You're right, they are different! At a glance, I'm guessing not too many folks quote this character either:
http://twinetour.com.hosting.domaindirect.com/original/mn/images/state_thumb_06.jpg
Corey Milner
Creative Director, Indigo Rose Software (http://www.indigorose.com)
Georges
08-22-2004, 07:32 AM
Strings like that can only be quoted by people with really good character.
"Quote" by Georges
hey I have a question about reading a file which has a string in double quotes and writing that into a file within double quotes
txt file to read is
"1 91 2" 49 2
"1 922 34" 69 8
ifstream myfile1;
myfile1.open("test.txt");
I want to read the ones in double quaotes as strings and the rest as integers
and then I want to write them to another file
like this
" 1 91 2" 49 2
how do I write this after opening a file
ofstream myfile;
myfile.open("test1.txt");
Lorne
11-20-2006, 10:21 AM
You'll need to "parse" the file, which basically means to go through it (for the most part one character at a time) and interpret the data.
In this case, you'll loop through the data looking for quotes and spaces. When you find a quote, grab everything up to the next quote as a string. When you aren't "inside" quotes like that, grab everything between spaces as a number. (You'll need to convert the number from a string to the equivalent number.)
vBulletin® v3.7.3, Copyright ©2000-2009, Jelsoft Enterprises Ltd.