| View previous topic :: View next topic |
| Author |
Message |
deleted19776 I post too much
Reputation: 11
Joined: 29 Apr 2007 Posts: 3838
|
Posted: Tue Sep 23, 2008 8:15 pm Post subject: [C++] How to make it freeze on cmd window? |
|
|
I've apparently did try messing with variables, it compiled successfully, but i need it to "pause" on the cmd window.
Source:
| Code: | // operating with variables
#include <iostream>
using namespace std;
int main ()
{
// declaring variables:
int a, b;
int result;
// process:
a = 5;
b = 2;
a = a + 1;
result = a - b;
// print out the result:
cout << result;
// terminate the program:
return 0;
} |
I was messing with the variables, i'm just starting, but how do you make it like freeze on saying "4"?
I double clicked and compiled the .exe, but it just shows me a cmd window for quite a little while.
My dad wants me to learn C++ by next year, so i really need you guys's help.
Exe attached, even if you don't need it, lol.
|
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Tue Sep 23, 2008 8:16 pm Post subject: |
|
|
cin.sync()
cin.ignore()
|
|
| Back to top |
|
 |
manc Grandmaster Cheater
Reputation: 1
Joined: 16 Jun 2006 Posts: 551
|
Posted: Tue Sep 23, 2008 8:22 pm Post subject: |
|
|
| slovach wrote: | cin.sync()
cin.ignore() |
Agreed. Use this. No doubt, other people will come in this thread telling you to use system("PAUSE") and such, but the code slovach method uses the least amount of resources (and doesnt add extra lines saying "Press any key to continue...)
_________________
|
|
| Back to top |
|
 |
deleted19776 I post too much
Reputation: 11
Joined: 29 Apr 2007 Posts: 3838
|
Posted: Tue Sep 23, 2008 8:23 pm Post subject: |
|
|
| slovach wrote: | cin.sync()
cin.ignore() |
Thank you!!!
And i won't make the common mistake of not putting ";" at the end of lines .
|
|
| Back to top |
|
 |
Heartless I post too much
Reputation: 0
Joined: 03 Dec 2006 Posts: 2436
|
Posted: Wed Sep 24, 2008 3:36 pm Post subject: |
|
|
I use getchar(), one line less trying I don't care if it takes more resource or what ever.
_________________
What dosen't kill you, usually does the second time. |
|
| Back to top |
|
 |
Fredd Expert Cheater
Reputation: 0
Joined: 18 Mar 2008 Posts: 240 Location: Somewhere past the rainbow, past the gold.....
|
Posted: Wed Sep 24, 2008 8:31 pm Post subject: |
|
|
I asked this same question, and got something complicated no one would explain. Explain this so I know what the hell it's doing to keep it open?
Also, waxxup, you didn't need to declare "results"- in the cout you can just do "cout a+b;" or so. (wrong I know.)
[/code]
_________________
Last edited by Fredd on Wed Sep 24, 2008 8:42 pm; edited 1 time in total |
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Wed Sep 24, 2008 8:37 pm Post subject: |
|
|
| Fredd wrote: | | I asked this same question, and got something complicated no one would explain. Explain this so I know what the hell it's doing to keep it open? |
sync basically discards unread characters from the input buffer.
ignore ignores a part of input.
|
|
| Back to top |
|
 |
Fredd Expert Cheater
Reputation: 0
Joined: 18 Mar 2008 Posts: 240 Location: Somewhere past the rainbow, past the gold.....
|
Posted: Wed Sep 24, 2008 8:48 pm Post subject: |
|
|
I still don't get how that makes it stay open...
_________________
|
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Wed Sep 24, 2008 9:11 pm Post subject: |
|
|
| Fredd wrote: | | I still don't get how that makes it stay open... |
because it needs to be executed before the program can continue. look at my other post, and there is your answer.
|
|
| Back to top |
|
 |
manc Grandmaster Cheater
Reputation: 1
Joined: 16 Jun 2006 Posts: 551
|
Posted: Wed Sep 24, 2008 10:18 pm Post subject: |
|
|
| HornyAZNBoy wrote: | | I use getchar(), one line less trying I don't care if it takes more resource or what ever. |
Doesnt that require an extra #include though?
_________________
|
|
| Back to top |
|
 |
Fredd Expert Cheater
Reputation: 0
Joined: 18 Mar 2008 Posts: 240 Location: Somewhere past the rainbow, past the gold.....
|
Posted: Sat Sep 27, 2008 6:16 pm Post subject: |
|
|
| slovach wrote: | | Fredd wrote: | | I still don't get how that makes it stay open... |
because it needs to be executed before the program can continue. look at my other post, and there is your answer. |
That other post, in idiot terms please?
_________________
|
|
| Back to top |
|
 |
GMZorita Grandmaster Cheater Supreme
Reputation: 0
Joined: 21 Mar 2007 Posts: 1361
|
Posted: Sun Sep 28, 2008 5:57 am Post subject: |
|
|
_________________
Gone |
|
| Back to top |
|
 |
DeletedUser14087 I post too much
Reputation: 2
Joined: 21 Jun 2006 Posts: 3069
|
Posted: Sun Sep 28, 2008 6:07 am Post subject: |
|
|
| I use Sleep() and a infinite loop or a condition loop.
|
|
| Back to top |
|
 |
scuba111223 Master Cheater
Reputation: 0
Joined: 10 Sep 2008 Posts: 275
|
Posted: Sun Sep 28, 2008 7:32 am Post subject: |
|
|
| im pretty new to c++ but can you use cin.get(); ?
|
|
| Back to top |
|
 |
Noz3001 I'm a spammer
Reputation: 26
Joined: 29 May 2006 Posts: 6220 Location: /dev/null
|
Posted: Sun Sep 28, 2008 9:31 am Post subject: |
|
|
Are you mad!?
We ALL know that this is a lot more efficient and is used in professional programs.
|
|
| Back to top |
|
 |
|