Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 29th, 2005, 10:50 AM   #1
Insomniac
Programmer
 
Insomniac's Avatar
 
Join Date: Aug 2004
Location: Cloud #9
Posts: 47
Rep Power: 0 Insomniac is on a distinguished road
What is the difference between... (hello world-noob)

std::cout and cout?

Im using Orielly's book and he's using Borland, but Im using DevCpp. Borland won't properly download for some reason... and I like DevCpp.

'std::cout' doesn't work in Dev, and when I change it to simply 'cout', it also doesn't work.

Here's the one the book wants me to use:

  #include <iostream>
 int main() 
 {
     std::cout << "Hello World\n";
     return (o);
 }

DevCpp highlights the std::cout line and says down below "'cout' is not a member of 'std'".

Here is one from Dev itself and as you can see, it likes 'cout':

 #include <iostream>
 
 using namespace std;
 
 int main (int argc, char *argv[])
 { 
     char quit;  
 
     quit = '\0';
     while (quit != 'q')
     {
         cout << "Hello ! This is a console app." << endl;
         cout << "To create a console, go to Project Options and select" << endl;
         cout << "\'Win32 Console\'." << endl;
         cout << "Press q to quit " << endl;
         cin >> quit;
     }
 
     return 0;
 }

I welcome your insight.

Last edited by Insomniac; May 30th, 2005 at 3:47 AM.
Insomniac is offline   Reply With Quote
Old May 29th, 2005, 10:54 AM   #2
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6 bl00dninja is on a distinguished road
C++ has something called namespaces, and cout belongs to the std library. so you have to either say std::cout explicitly every time you use it, or you have to put the statement

using namespace std;

at the top of your code. you'll see the double-colon used when you get into classes and such. i know, that problem was a bitch for me at one time as well.

and as for it coming out bad we have tags for code as such


we also have quote tags too
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja is offline   Reply With Quote
Old May 29th, 2005, 11:24 AM   #3
Insomniac
Programmer
 
Insomniac's Avatar
 
Join Date: Aug 2004
Location: Cloud #9
Posts: 47
Rep Power: 0 Insomniac is on a distinguished road
thank you ninja,

edit: Got it.
__________________
From an IBM Thinkpad T43 - 14.1" SXGA+ - ATI 64 MB X300 - Sonoma 760 - 2 GB RAM - 80 GB HD 5400 - IBM ABG II - FC3, Ubuntu & XPee
DevC++, and Macromedia's - Dreamweaver & Flash Pro and a little Adobe Illustrator & Photoshop
Sleep? Sleep is for the weak.:cool:


Last edited by Insomniac; May 29th, 2005 at 3:20 PM.
Insomniac is offline   Reply With Quote
Old May 29th, 2005, 3:17 PM   #4
Insomniac
Programmer
 
Insomniac's Avatar
 
Join Date: Aug 2004
Location: Cloud #9
Posts: 47
Rep Power: 0 Insomniac is on a distinguished road
I hate to ask this because I once found the answer to it.. and now I can't after doing a search.

After compiling and executing the program the window doesn't stay open. Any suggestions? I tried the system("PAUSE"); line.. but it did nothing that I could see.

 #include <iostream>
 
 using namespace std;
 
 int main() 
 {
     cout << "Hello World\n";
     return 0;
 }
__________________
From an IBM Thinkpad T43 - 14.1" SXGA+ - ATI 64 MB X300 - Sonoma 760 - 2 GB RAM - 80 GB HD 5400 - IBM ABG II - FC3, Ubuntu & XPee
DevC++, and Macromedia's - Dreamweaver & Flash Pro and a little Adobe Illustrator & Photoshop
Sleep? Sleep is for the weak.:cool:


Last edited by Insomniac; May 30th, 2005 at 3:49 AM.
Insomniac is offline   Reply With Quote
Old May 29th, 2005, 4:18 PM   #5
Chris Weimer
Programmer
 
Join Date: May 2005
Posts: 34
Rep Power: 0 Chris Weimer is on a distinguished road
it's because you have to include <cstdlib> for system("PAUSE"); also you can do cin.get();
Chris Weimer is offline   Reply With Quote
Old May 29th, 2005, 5:31 PM   #6
Insomniac
Programmer
 
Insomniac's Avatar
 
Join Date: Aug 2004
Location: Cloud #9
Posts: 47
Rep Power: 0 Insomniac is on a distinguished road
Thanks Chris,

where do I put that? I tried cin.get(); in two different places that I thought were right:

 #include <iostream>
 
 using namespace std;
 
 int main() 
 {
     cout << "Hello World\n"; //prints "Hello World" on the screen
     return 0;
 }
 cin.get();

AND

 #include <iostream>
 
 using namespace std;
 
 int main() 
 {
     cout << "Hello World\n"; //prints "Hello World" on the screen
     return 0;
     cin.get();
 }


The book makes no reference to that line at all. :o
__________________
From an IBM Thinkpad T43 - 14.1" SXGA+ - ATI 64 MB X300 - Sonoma 760 - 2 GB RAM - 80 GB HD 5400 - IBM ABG II - FC3, Ubuntu & XPee
DevC++, and Macromedia's - Dreamweaver & Flash Pro and a little Adobe Illustrator & Photoshop
Sleep? Sleep is for the weak.:cool:


Last edited by Insomniac; May 30th, 2005 at 3:50 AM.
Insomniac is offline   Reply With Quote
Old May 29th, 2005, 5:35 PM   #7
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 6 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
the second one should be correct and it probably makes no refernce to it as well they dont want to over complicate things.
__________________
"Put your hand on a hot stove for a minute, and it seems like an hour. Sit with a pretty girl for an hour, and it seems like a minute. THAT'S relativity."

- Albert Einstein
Berto is offline   Reply With Quote
Old May 29th, 2005, 5:40 PM   #8
Insomniac
Programmer
 
Insomniac's Avatar
 
Join Date: Aug 2004
Location: Cloud #9
Posts: 47
Rep Power: 0 Insomniac is on a distinguished road
Quote:
Originally Posted by Berto
the second one should be correct and it probably makes no refernce to it as well they dont want to over complicate things.
Thank you Berto, but unfortunately, neither keep the window open
__________________
From an IBM Thinkpad T43 - 14.1" SXGA+ - ATI 64 MB X300 - Sonoma 760 - 2 GB RAM - 80 GB HD 5400 - IBM ABG II - FC3, Ubuntu & XPee
DevC++, and Macromedia's - Dreamweaver & Flash Pro and a little Adobe Illustrator & Photoshop
Sleep? Sleep is for the weak.:cool:

Insomniac is offline   Reply With Quote
Old May 29th, 2005, 7:04 PM   #9
Insomniac
Programmer
 
Insomniac's Avatar
 
Join Date: Aug 2004
Location: Cloud #9
Posts: 47
Rep Power: 0 Insomniac is on a distinguished road
With a little extra searching I found what works.

getchar();

immediately following the cout statement. Yea!

Ok, now to the understanding part.. what is this line saying exactly? 'get character' and somehow it waits for user input?
__________________
From an IBM Thinkpad T43 - 14.1" SXGA+ - ATI 64 MB X300 - Sonoma 760 - 2 GB RAM - 80 GB HD 5400 - IBM ABG II - FC3, Ubuntu & XPee
DevC++, and Macromedia's - Dreamweaver & Flash Pro and a little Adobe Illustrator & Photoshop
Sleep? Sleep is for the weak.:cool:


Last edited by Insomniac; May 30th, 2005 at 3:52 AM.
Insomniac is offline   Reply With Quote
Old May 29th, 2005, 7:16 PM   #10
Kami
Newbie
 
Join Date: Jan 2005
Posts: 21
Rep Power: 0 Kami is on a distinguished road
Quote:
Originally Posted by Insomniac
Thanks Chris,

where do I put that? I tried cin.get(); in two different places that I thought were right:

#include <iostream>

using namespace std;

int main()
{
cout << "Hello World\n"; //prints "Hello World" on the screen
return 0;
}
cin.get();

AND

#include <iostream>

using namespace std;

int main()
{
cout << "Hello World\n"; //prints "Hello World" on the screen
return 0;
cin.get();
}


The book makes no reference to that line at all.

Actually, neither of those are correct. As soon as the code encounters the return, the function exits. cin.get() is never executed. The correct code would be as follows:

int main()
{
cout << "Hello World\n"; //prints "Hello World" on the screen
cin.get(); //Wait for user input
return 0; //Exits funciton
}
Kami is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:35 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC