Author |
Learning some basic C++ |
Ignorance Grand Admiral
Joined: October 27, 2012 Posts: 85
| Posted: 2013-02-19 19:36  
I am just reading a book of C++ and creating some simple programs with C++.
I'm working with variables right now and I looked online for code to make the computer wait for user input before exiting the program, because when I ran the program it instantly quit before I could even see if it worked. I got something, but it doesn't seem to be working. I figure a game that's programmed with C++ is a good place to ask for help.
Is this right?:
#include
#include
using namespace std;
int main()
{
char letter; letter='A';
int number; number=100;
float decimal=7.5;
double pi= 3.14159;
bool isTrue=false
;cout<< "char letter: " << letter << endl;
cout<< "int number: " << number << endl;
cout<< "float decimal: " << decimal << endl;
cout<< "double pi: " << pi << endl;
cout<< "bool isTrue: " << isTrue << endl;
return 0;
}
void wait()
{
std::cout << "Press ENTER to continue...";
std::cin.ignore( std::numeric_limits ::max(), ' ' );
}
_________________ Lt. Commander Data: \"In the game of poker, there is a moment when a player must decide if an opponent is being deceptive, or actually holds a winning hand.\"
|
Null Pointer Grand Admiral Templar Knights
Joined: April 10, 2010 Posts: 148
| Posted: 2013-02-19 21:21  
You could just use
"cin.get()" instead of that wait function.
It does the same thing ^^
Also, you need to call the "void wait()".
add
wait();
before
return 0; [ This Message was edited by: Boolean Argument on 2013-02-19 21:23 ]
_________________
|
Pantheon Marshal Palestar
Joined: May 29, 2001 Posts: 1789
| Posted: 2013-02-19 22:24  
What Boolean said.
What you've done is declared two functions : main() and wait(). main() is called by default when the program is run, but you've only just declared wait(). To use wait() stick it before the return 0; part at the bottom of your main() function.
This can be handy if you want to use that same thing over and over again in other parts of a problem. So instead of having to write similar code all the time, you can simply use wait(). If you really wanted to get fancy, you could pass an arguement into wait() as the string (wait("Press Enter to...")).
Honestly, for a single wait, I'd probably just use a single cin.get(). [ This Message was edited by: Pantheon on 2013-02-19 22:25 ]
_________________
|
Ignorance Grand Admiral
Joined: October 27, 2012 Posts: 85
| Posted: 2013-02-20 06:30  
So I just put the 'wait()' right above the 'return 0;' ?
_________________ Lt. Commander Data: \"In the game of poker, there is a moment when a player must decide if an opponent is being deceptive, or actually holds a winning hand.\"
|
Fattierob Vice Admiral
Joined: April 25, 2003 Posts: 4059
| Posted: 2013-02-20 06:32  
When I was learning C++ I used this website to help me with some of the syntax and library calls. It has good tutorials too.
Also if you're "using namespace std;" you don't need to type std::cout or std::cin. You're in the namespace "std" so you just type cout or cin.
_________________
|
Ignorance Grand Admiral
Joined: October 27, 2012 Posts: 85
| Posted: 2013-02-20 06:34  
Quote:
|
On 2013-02-20 06:32, Fattierob wrote:
When I was learning C++ I used this website to help me with some of the syntax and library calls. It has good tutorials too.
Also if you're "using namespace std;" you don't need to type std::cout or std::cin. You're in the namespace "std" so you just type cout or cin.
|
|
Yeah I copied down the code just to see if it would work. I guess I was too lazy to get rid of that extra. ^^
_________________ Lt. Commander Data: \"In the game of poker, there is a moment when a player must decide if an opponent is being deceptive, or actually holds a winning hand.\"
|
| Dark Hiigaran | Chief Marshal
Joined: July 07, 2007 Posts: 426 From: Slovenia (Europe)
| Posted: 2013-02-20 07:34  
good video tutorials for c++
_________________
|
Faustus Marshal Palestar
Joined: May 29, 2001 Posts: 2748 From: Austin, Texas
| Posted: 2013-02-20 07:38  
Quote:
|
On 2013-02-20 06:32, Fattierob wrote:
When I was learning C++ I used this website to help me with some of the syntax and library calls. It has good tutorials too.
Also if you're "using namespace std;" you don't need to type std::cout or std::cin. You're in the namespace "std" so you just type cout or cin.
|
|
I would recommend to steer clear of the "using" keyword, it ends up causing more problems that it's worth in the end.
_________________
|
DiepLuc Chief Marshal
Joined: March 23, 2010 Posts: 1187
| Posted: 2013-02-20 13:21  
C is everywhere. Where is D?
_________________
|
Faustus Marshal Palestar
Joined: May 29, 2001 Posts: 2748 From: Austin, Texas
| Posted: 2013-02-20 13:39  
Quote:
|
On 2013-02-20 13:21, DiepLuc wrote:
C is everywhere. Where is D?
|
|
Here..
http://dlang.org/
_________________
|
Fattierob Vice Admiral
Joined: April 25, 2003 Posts: 4059
| Posted: 2013-02-20 14:34  
So when are we upgrading the code base to D?
[ This Message was edited by: Fattierob on 2013-02-20 14:42 ]
_________________
|
| Dark Hiigaran | Chief Marshal
Joined: July 07, 2007 Posts: 426 From: Slovenia (Europe)
| Posted: 2013-02-20 15:10  
lolcode is the best language
_________________
|
Null Pointer Grand Admiral Templar Knights
Joined: April 10, 2010 Posts: 148
| Posted: 2013-02-20 17:23  
TheNewBoston is how I learned C++ and Java.
_________________
|
Ignorance Grand Admiral
Joined: October 27, 2012 Posts: 85
| Posted: 2013-02-20 20:39  
So uhm. How do I PM someone? Haven't learned because I never needed to. o.O
_________________ Lt. Commander Data: \"In the game of poker, there is a moment when a player must decide if an opponent is being deceptive, or actually holds a winning hand.\"
|
Walrus of Apathy Admiral Templar Knights
Joined: August 07, 2005 Posts: 466 From: Dorans Basement
| Posted: 2013-02-20 21:07  
Very top of the page, right below the Reply button there's a button to go to your private messages. From there you can compose one.
_________________
|