| View previous topic :: View next topic |
| Author |
Message |
wunder312355 Grandmaster Cheater
Reputation: -1
Joined: 14 May 2007 Posts: 568
|
Posted: Sun Jan 20, 2008 3:05 pm Post subject: Quick C++ Question |
|
|
In a win32 console, the hello world application just runs real quick, just swiftly displaying "hello world" text.
| Code: |
#include <iostream>
int main()
{
std::cout << "Hello World!" << std::endl;
} |
How do i make that say "press any key to continue" before it ends?
Visual studio 2005
Ok i figured out to use
|
|
| Back to top |
|
 |
NINTENDO Grandmaster Cheater Supreme
Reputation: 0
Joined: 02 Nov 2007 Posts: 1371
|
Posted: Sun Jan 20, 2008 3:30 pm Post subject: |
|
|
Do i have to use sleep in c#?
_________________
Intel over amd yes. |
|
| Back to top |
|
 |
MegaForum Grandmaster Cheater
Reputation: 0
Joined: 20 Aug 2007 Posts: 558
|
Posted: Sun Jan 20, 2008 3:32 pm Post subject: |
|
|
name space std;
return 0;
|
|
| Back to top |
|
 |
FullyAwesome I post too much
Reputation: 0
Joined: 05 Apr 2007 Posts: 4438 Location: Land Down Under
|
Posted: Sun Jan 20, 2008 4:19 pm Post subject: |
|
|
or if you run it straight from a cmd prompt, you'll see it come up and then it will take you to the next line C:\ or w.e. you'll still see your "hello world" message above, if you know what i mean.
_________________
|
|
| Back to top |
|
 |
Noz3001 I'm a spammer
Reputation: 26
Joined: 29 May 2006 Posts: 6220 Location: /dev/null
|
Posted: Sun Jan 20, 2008 4:29 pm Post subject: |
|
|
| Code: | printf("\n\nPress any key to continue...\n");
_getch();
return 0; |
|
|
| Back to top |
|
 |
lurc Grandmaster Cheater Supreme
Reputation: 2
Joined: 13 Nov 2006 Posts: 1900
|
Posted: Sun Jan 20, 2008 4:35 pm Post subject: |
|
|
| noz3001 wrote: | | Code: | printf("\n\nPress any key to continue...\n");
_getch();
return 0; |
|
dont forget
_________________
|
|
| Back to top |
|
 |
Noz3001 I'm a spammer
Reputation: 26
Joined: 29 May 2006 Posts: 6220 Location: /dev/null
|
Posted: Sun Jan 20, 2008 5:43 pm Post subject: |
|
|
| I wanted him to look that part up himself..
|
|
| Back to top |
|
 |
samuri25404 Grandmaster Cheater
Reputation: 7
Joined: 04 May 2007 Posts: 955 Location: Why do you care?
|
Posted: Sun Jan 20, 2008 6:41 pm Post subject: |
|
|
He'd have most likely just said something along the lines of
"Your code doesn't work!"
_________________
|
|
| Back to top |
|
 |
wunder312355 Grandmaster Cheater
Reputation: -1
Joined: 14 May 2007 Posts: 568
|
Posted: Sun Jan 20, 2008 8:50 pm Post subject: |
|
|
| samuri25404 wrote: | He'd have most likely just said something along the lines of
"Your code doesn't work!" |
Im sorry, but i probably would have. That part just really confuses me. How do you know which files to include? Probably something to do with msdn right?
|
|
| Back to top |
|
 |
samuri25404 Grandmaster Cheater
Reputation: 7
Joined: 04 May 2007 Posts: 955 Location: Why do you care?
|
Posted: Sun Jan 20, 2008 11:16 pm Post subject: |
|
|
Don't worry, I most likely would have too. :P
You just kinda know, I guess. Microsoft grouped different things into different header files for a reason, though if you ever get confused, you just just Google it
_________________
|
|
| Back to top |
|
 |
Noz3001 I'm a spammer
Reputation: 26
Joined: 29 May 2006 Posts: 6220 Location: /dev/null
|
Posted: Mon Jan 21, 2008 2:30 am Post subject: |
|
|
| thefun25 wrote: | | samuri25404 wrote: | He'd have most likely just said something along the lines of
"Your code doesn't work!" |
Im sorry, but i probably would have. That part just really confuses me. How do you know which files to include? Probably something to do with msdn right? |
http://msdn2.microsoft.com/en-us/library/aa297934.aspx
Look, it tells you right there which headers to include.
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Mon Jan 21, 2008 10:42 am Post subject: |
|
|
You shouldn't use System("pause"); for pausing your application if that is going to be the only System() call you make. This is due to creating a huge overhead for doing something basic.
Check out this article:
http://www.gidnetwork.com/b-61.html
For substitutes, you can use _getch(); or just use the iostream method of:
| Code: | std::cin.ignore();
std::cin.sync(); |
| Code: | #include <iostream>
int main()
{
std::cout << "Hello world!" << std::endl;
std::cout << "Press any key to continue..." << std::endl;
std::cin.ignore();
std::cin.sync();
return 0;
} |
| MegaForum wrote: | | name space std; |
Incorrect syntax, and this isn't needed. The correct method would be:
| Code: | | using namespace std; |
Again, not needed though. And this wont cause the program to pause, it just signifies you want to use the std namespace so you do not need to make calls to the std functions inside iostream using std:: at the front.
A quick read if you want to know more about namespaces:
http://cplusplus.com/doc/tutorial/namespaces.html
_________________
- Retired. |
|
| Back to top |
|
 |
MegaForum Grandmaster Cheater
Reputation: 0
Joined: 20 Aug 2007 Posts: 558
|
Posted: Mon Jan 21, 2008 7:39 pm Post subject: |
|
|
| Wiccaan wrote: | You shouldn't use System("pause"); for pausing your application if that is going to be the only System() call you make. This is due to creating a huge overhead for doing something basic.
Check out this article:
http://www.gidnetwork.com/b-61.html
For substitutes, you can use _getch(); or just use the iostream method of:
| Code: | std::cin.ignore();
std::cin.sync(); |
| Code: | #include <iostream>
int main()
{
std::cout << "Hello world!" << std::endl;
std::cout << "Press any key to continue..." << std::endl;
std::cin.ignore();
std::cin.sync();
return 0;
} |
| MegaForum wrote: | | name space std; |
Incorrect syntax, and this isn't needed. The correct method would be:
| Code: | | using namespace std; |
Again, not needed though. And this wont cause the program to pause, it just signifies you want to use the std namespace so you do not need to make calls to the std functions inside iostream using std:: at the front.
A quick read if you want to know more about namespaces:
http://cplusplus.com/doc/tutorial/namespaces.html |
You wouldn't need st:: for ea. line if you just do
using namespace std; at the beginning right?
|
|
| Back to top |
|
 |
lurc Grandmaster Cheater Supreme
Reputation: 2
Joined: 13 Nov 2006 Posts: 1900
|
Posted: Mon Jan 21, 2008 8:38 pm Post subject: |
|
|
including the entire namespace is pointless and is bad form
if your to lazy to add std:: to the beggining of all your cout's, cin's and endl's then just add this:
| Code: | using std::cout;
using std::cin;
using std::endl; |
you can add that instead because those are usually the only 3 things u use from the std namespace.
_________________
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Tue Jan 22, 2008 5:59 am Post subject: |
|
|
| MegaForum wrote: | You wouldn't need st:: for ea. line if you just do
using namespace std; at the beginning right? |
Yes, if you include the namespace, you wont need the std:: in front of the functions and such, but my main reason for the response I quoted you with was because the code you gave was both written wrong and did not pertain to the question at hand.
_________________
- Retired. |
|
| Back to top |
|
 |
|