Indigo Rose Software

Go Back   Indigo Rose Software Forums > Indigo Rose Software > Developer's Den

Reply
 
Thread Tools Display Modes
  #1  
Old 07-24-2004
Corey's Avatar
Corey Corey is offline
Registered User
 
Join Date: Aug 2002
Posts: 9,746
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
Reply With Quote
  #2  
Old 07-26-2004
Brett's Avatar
Brett Brett is offline
Indigo Rose Staff Member
 
Join Date: Jan 2000
Posts: 2,001
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.
Reply With Quote
  #3  
Old 08-15-2004
Mark's Avatar
Mark Mark is offline
Indigo Rose Staff Member
 
Join Date: Jun 2000
Location: Indigo Rose Software
Posts: 1,773
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";
I'm guessing that in the example that you used either a character or a string could be used.
__________________
MSI Factory The Next Generation Intelligent Setup Builder
Reply With Quote
  #4  
Old 08-15-2004
Corey's Avatar
Corey Corey is offline
Registered User
 
Join Date: Aug 2002
Posts: 9,746
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
Reply With Quote
  #5  
Old 08-18-2004
Mark's Avatar
Mark Mark is offline
Indigo Rose Staff Member
 
Join Date: Jun 2000
Location: Indigo Rose Software
Posts: 1,773
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
Reply With Quote
  #6  
Old 08-19-2004
Lorne's Avatar
Lorne Lorne is offline
Indigo Rose Staff Member
 
Join Date: Feb 2001
Location: Indigo Rose Software
Posts: 2,588
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 ]]
Reply With Quote
  #7  
Old 08-22-2004
Corey's Avatar
Corey Corey is offline
Registered User
 
Join Date: Aug 2002
Posts: 9,746
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
Reply With Quote
  #8  
Old 08-22-2004
Georges's Avatar
Georges Georges is offline
Indigo Rose Customer
 
Join Date: Aug 2004
Location: Gent, Belgium
Posts: 74
Strings like that can only be quoted by people with really good character.

"Quote" by Georges
Reply With Quote
  #9  
Old 11-18-2006
jetb jetb is offline
Forum Member
 
Join Date: Nov 2006
Posts: 2
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");
Reply With Quote
  #10  
Old 11-20-2006
Lorne's Avatar
Lorne Lorne is offline
Indigo Rose Staff Member
 
Join Date: Feb 2001
Location: Indigo Rose Software
Posts: 2,588
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 ]]
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



All times are GMT -6. The time now is 06:10 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Copyright © 2000 - 2009 Indigo Rose Corporation. All rights reserved.
Indigo Rose Software