Jump to content
  • Advertisement
Sign in to follow this  
Gallivan

Strings/Nesting Comments

This topic is 4347 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Two quick (I assume) questions that I have come across. 1. Is there any benefit to nesting comments? As in: /* //Comment //Comment */ 2. What is the most common usage of strings?

Share this post


Link to post
Share on other sites
Advertisement
To be honest I can't think of any situation in which i'd want to nest comments, but it also doesn't make sense.
Anything after the comment marks is ignored by the compiler, comments are only for yourself to make your code easier to read. Therefore, unless you think it improves the readability of your code, you can use the style you showed. ( I think it doesn't improve readability though :P ).

Some comment styles I often use:

// Just a quick comment for above a variable or something

// -------------------------------------
// Name: Function name here
// Info: Function description here
// Return value: return value here.
// -------------------------------------

/* short comment between code */

/*
* This style could be used for commenting the start of a file.
* Filename:
* Author:
* Date:
*
*/


//
// Or this, I like it better than those asterisks...
//




As you can see "nesting" would only make it less readable.

As to your second question, as soon as you are starting to work with text in your applications the string class is going to be of use. Technically you could handle text just with character arrays but std::string is not only so much easier, it's also much safer than using character arrays. Therefore, I recommend you use std::string as often as possible when you need to store text ;)

Share this post


Link to post
Share on other sites
You can use /* */ to temporarily remove code from being compiled, for testing usually. In c++ you can use
#if 0
// stuff you want commented
#endif

the #if way is better because nested /**/ comments dont work, the compiler will only catch the indicated code:

/*
int x;
/*
int y;
*/

int z; // oh no, this is included
*/
//<--- compiler will complain about this one, its not syntactically correct
As its quite common to have /* */ comments littered throughout code, it can make commenting out large blocks a pain.


Its hard to think of many useful programs where strings aren't used...

Share this post


Link to post
Share on other sites
Alright, I though as much as far as the comments went, but I was reading a site in which it said you could. I guess you "can" do anything. :P

But in terms of strings, (I don't really understand what a character array is), you are saying that if I want to say Hi once, that I should just use COUT, but if I want to say it a hundred times, use strings?

Share this post


Link to post
Share on other sites
Quote:
Original post by Gallivan
1. Is there any benefit to nesting comments?

As in:

/* //Comment
//Comment
*/


Usually when you see this it is because someone "commented out" a piece of code that contained comments itself. This is a common way to remove a chunk of code while debugging.

Share this post


Link to post
Share on other sites
Quote:
Original post by Gallivan
But in terms of strings, (I don't really understand what a character array is), you are saying that if I want to say Hi once, that I should just use COUT, but if I want to say it a hundred times, use strings?


No, a string (or a character array) is simply an array of numbers where each number is the ASCII (or UNICODE) code of a character. C-style strings are null-terminated, so they contain an additional number zero at the end.

cout << "This is a string" << endl;

If you wanted to print that out a hundred times you have to use a for loop:

for(int i=0; i<100; i++)
cout << "This is a string" << endl;

Share this post


Link to post
Share on other sites
Ok thanks to everyone above for their EXTREMELY speedy responses. But I am stilling having a bit of confusion about where and when to use strings? From what I have read, which I am sure has not covered even the most basic uses of strings, they are only used in text?

Here is an example from the site I am reading through:

// my first string
#include <iostream>
#include <string>
using namespace std;

int main ()
{
string mystring;
mystring = "This is the initial string content";
cout << mystring << endl;
mystring = "This is a different string content";
cout << mystring << endl;
return 0;
}

And on a different note, how do I incorporate that into a small white box? and <code> don't seem to work.

Share this post


Link to post
Share on other sites
Strings by definition are text, so that's what they are used for: displaying text on the screen or capturing text from the keyboard. In C++ whenever you see an std::string variable, you can bet that the variable contains some sort of text.

EDIT: I guess what you are really asking is why do we need to use std::string when we can just use literal strings directly? For instance, these two pieces of code do exactly the same thing:

cout << "Hi" << end; //print out the literal string

string hi = "Hi"; //save the literal string so we can use it later
cout << hi << endl; //print out the std::string object

The reason why you would want to use an std::string object is in case you don't know in advance what kind of text should be in the string. For example:

string message = getMessage(); //this function returns the text that needs to be printed out (perhaps the text comes from an instant messaging application)
cout << message << endl; //this prints out whatever was returned from the above function

[Edited by - deathkrush on September 20, 2006 6:37:36 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Gallivan
But I am stilling having a bit of confusion about where and when to use strings?


strings can be used in forms where a person's name, address, or phone number are required; places where a numeric variable could not hold their possible values. a good way to figure out when to use a string rather than numeric data types is if your program isn't going to need to arithmetically modify or test the data. data such as phone numbers or ssn, should not be stored in numeric data types, even though they have mostly, or entirely all numbers.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!