about //cout

Hi

I have one questions about //cout.
What //cout is defference from cout?
Please let me know.

Thanks
Yoshi
Hi Yoshi,

Where did you get "//cout" from?

In C++ "//" is always used to introduce a comment. So I think it more than likely that you're looking at a piece of code which has been "commented out".

When might you see this? Well, some programmers often include cout statements to provide checks when developing code. For example;

1
2
3
4
5
6
7
8
9
    
    // don't worry about what this code does (it hasn't been error checked!)          
    // just worry about the principle behind comments and the use of "//"
    for (int i=0; i < ARRAYSIZE; i++) {
        array1 [i] = rand()%6 + 1;        // fill array with random elements
        cout << "\n" << array1[i];        // print array elements
    }



In this example the cout statement merely serves to let the programmer see what's going on inside the program. When the cout statement is no longer needed, it can be deleted or "commented out" as follows;

1
2
3
4
5
6
7
8
9

    // now "cout" has been "commented out" and the system will ignore 
    // the entire line following "//"
    for (int i=0; i<10; i++) {
        array1 [i] = rand()%6 + 1;        // fill array with random elements          
        //cout << "\n" << array1[i];        // print array elements
    }



Don't be confused by the fact that there are now TWO sets of "//" in the line containing the cout command. The compiler just ignores the second set and treats the entire line like a comment.

Does that help any?

PS: a debugger is a better way to see what's going on inside your programs. If you have access to one, you should learn to use it.
Last edited on
Hi muzhogg ,

Thanks your reply.
I was wondering as below coding.

int main(){
int integer;
integer = 0;
cout << "Enter your integer";
cin >> integer;
//cout >> "0";
if ( integer==0 )
{
cout<<"integer is zero."<<endl;
}

Again, thanks your explanatioin, I love your comments.
If you do, Please let me know, or anyone Please leave your comments.
I wish if I could be a better learner.

Thanks
Yoshi
Yoshi-san,

//cout >> "0";

This line would be treated as a comment. The compiler will ignore it.

If you change it to
cout >> "0";
it would become an incorrect line. You must then change it to
cout << "0";

Hotaru
Hi Yoshi,

As Hotaru said, "cout >> "0";" would be an incorrect line -- it's simply not possible to read data into a constant from the input stream.

My guess would be that whoever wrote the code put in the "//" in order to avoid compiler errors. Try running it without the "//" and you'll see!
Hotaru san and Muzhogg san,

Thanks your comments.
I talked last night with my friends about it.
I could figure out how "//" means, it just use leaving comments, even if we want to write for the line code, right?

I need to do next assignment until tommrrow morning!
ha ha.

See you!

Thanks
Yoshi
Topic archived. No new replies allowed.