| View previous topic :: View next topic |
| Author |
Message |
Acim Grandmaster Cheater Supreme
Reputation: 0
Joined: 04 Jun 2007 Posts: 1948 Location: If anyone has a GMS DK and they don't need it I'll have it!!
|
Posted: Fri Oct 05, 2007 2:34 pm Post subject: New to C++ |
|
|
Yeah... I'm new to C++. So I've used a compiler (Bloodshed Dev-C++) and for the program I wrote:
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello World!";
return 0;
}
I also tried int main( void ) and stuff like that, but when it runs it immediatly closes. In BAT programming "pause" makes it stay and not close until the exit button is hit, is there any C++ command that does that? _________________
I'm alive and well, but I quit CEF for a while. Legitly playing since Novemberish 07. Starting hacking October 06. |
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Fri Oct 05, 2007 2:50 pm Post subject: |
|
|
__getch();
edit: i think you need to include conio for it. |
|
| Back to top |
|
 |
zart Master Cheater
Reputation: 0
Joined: 20 Aug 2007 Posts: 351 Location: russia
|
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Fri Oct 05, 2007 3:13 pm Post subject: |
|
|
| Code: | std::cin.ignore();
std::cin.sync(); |
|
|
| Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Fri Oct 05, 2007 3:14 pm Post subject: |
|
|
I posted this in the RS section when he asked for it there, I am just reposting here just incase anybody else was wondering:
| Mychilli wrote: | | Code: | #include <iostream> // provides std::cout
int main()
{
std::cout << "Hello, world!\n";
return 0;
} |
lol spoilers |
Using a namespace is like globally defining the class you use, so std::cout was the exact same thing he was doing.
You are actually doing everything correctly (aside form the fact it is in C++). What the program does it output Hello, world! then immediately closes. What you want is a way to either prolong the closing or wait till the user hits a key.
Prolong:
| Code: | #include <iostream>
using namespace std;
int main ()
{
cout << "Hello World! xD";
Sleep(1000); //The program waits for 1000ms (1 second) before continuing.
return 0;
} |
Wait for input:
| Code: | #include <iostream>
using namespace std;
int main ()
{
cout << "Hello World! xD";
cin.get(); //Wait for keypress.
return 0;
} |
|
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Fri Oct 05, 2007 3:25 pm Post subject: |
|
|
| Flyte wrote: | Wait for input:
| Code: | #include <iostream>
using namespace std;
int main ()
{
cout << "Hello World! xD";
cin.get(); //Wait for keypress.
return 0;
} |
|
| Code: | #include <iostream>
int main(int argc, char* argv[])
{
std::cout << "Hai World!";
std::cin.ignore();
std::cin.sync();
return EXIT_SUCCESS;
} |
|
|
| Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Fri Oct 05, 2007 3:28 pm Post subject: |
|
|
| Flyte wrote: | | x0r wrote: | I didn't know iostream provided it's own Sleep function
|
My bad, too many C programming habits.
You know what I was getting at though. |
|
|
| Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Fri Oct 05, 2007 4:36 pm Post subject: |
|
|
Lol, so many people having trouble with this. You can't put return 0. Do this.
| Code: |
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello World!\n";
cin.get();
}
|
_________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
| Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Fri Oct 05, 2007 4:38 pm Post subject: |
|
|
| oib111 wrote: | Lol, so many people having trouble with this. You can't put return 0. Do this.
| Code: |
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello World!\n";
cin.get();
}
|
|
NO! |
|
| Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Fri Oct 05, 2007 4:56 pm Post subject: |
|
|
Fine remove the namespace, its not good programming practice right  _________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
| Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Fri Oct 05, 2007 4:58 pm Post subject: |
|
|
| oib111 wrote: | Fine remove the namespace, its not good programming practice right  |
Ahahaha, that isn't why I said no.
Always have a return in main! |
|
| Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Fri Oct 05, 2007 5:12 pm Post subject: |
|
|
Lol. Or void it. But you rly shouldn't void it. _________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
| Back to top |
|
 |
Noz3001 I'm a spammer
Reputation: 26
Joined: 29 May 2006 Posts: 6220 Location: /dev/null
|
Posted: Fri Oct 05, 2007 5:38 pm Post subject: |
|
|
| oib111 wrote: | | Lol. Or void it. But you rly shouldn't void it. |
Or double it or char it or rape it. namespace std; isn't bad programming! |
|
| Back to top |
|
 |
appalsap Moderator
Reputation: 0
Joined: 27 Apr 2006 Posts: 6753 Location: Pakistan
|
Posted: Fri Oct 05, 2007 5:43 pm Post subject: |
|
|
| noz3001 wrote: | | Or double it or char it or rape it. namespace std; isn't bad programming! |
It's bad beginner programming, especially if you don't know what it means or what namespaces are. What if there was a function inside the std namespace called MessageBoxA? Since you dumped the contents of std into the current namespace, when you use MessageBoxA, you use the std one, not the windows one (well maybe it wouldn't if the parameters were different, but namespaces should be used to solve naming conflicts, not cause them!) |
|
| Back to top |
|
 |
Acim Grandmaster Cheater Supreme
Reputation: 0
Joined: 04 Jun 2007 Posts: 1948 Location: If anyone has a GMS DK and they don't need it I'll have it!!
|
|
| Back to top |
|
 |
|