|
#1
|
||||
|
||||
|
C++ question, preferred usage single quotes vs. double quotes
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 |
|
#2
|
||||
|
||||
|
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. |
|
#3
|
||||
|
||||
|
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.
Code:
char ch = 'c'; char* ch = "My String";
__________________
MSI Factory The Next Generation Intelligent Setup Builder |
|
#4
|
||||
|
||||
|
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 |
|
#5
|
||||
|
||||
|
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: Code:
CString strMark = "Hello everyone";
int nPos = strMark.Find('o');
nPos = strMark.Find("eve");
__________________
MSI Factory The Next Generation Intelligent Setup Builder |
|
#6
|
||||
|
||||
|
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.
__________________
--[[ Indigo Rose Software Developer ]] |
|
#7
|
||||
|
||||
|
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:
Corey Milner Creative Director, Indigo Rose Software |
|
#8
|
||||
|
||||
|
Strings like that can only be quoted by people with really good character.
"Quote" by Georges |
|
#9
|
|||
|
|||
|
c++ double quotes
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"); |
|
#10
|
||||
|
||||
|
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.)
__________________
--[[ Indigo Rose Software Developer ]] |
![]() |
«
Previous Thread
|
Next Thread
»
| Thread Tools | |
| Display Modes | |
|
|
All times are GMT -6. The time now is 06:10 PM.









Linear Mode
