Beginning C++, question about tutorial

Unsuccessful on an online tutorial for the very beginning of C++

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 2270 Views - Last Post: 27 April 2010 - 05:09 PM Rate Topic: -----

#1 Teaturtle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 25-April 10

Beginning C++, question about tutorial

Posted 25 April 2010 - 11:28 PM

Hello. I had been browsing for something useful to learn in starting game developing when I, of course, came across C++. http://www.cprogramm...om/code_blocks/ is a site I had gone to, did each step, installed the CodeBlocks.

Hit f9 to compile Project1:

#include <iostream>

int main()
{
std;cout <<Hello world!<< std::endl;
return 0;
}



Now, the last step on that tutorial shows a black box with the text inside. However when I hit f9, a flicker comes up on my screen and then disappears. Does it matter that I cannot see the black box, type in it, etc? What exactly did it do? I'm sorry, this post probably looks like a heap of garbage, but I'm quite confused. I've searched, trying to find exactly how C++ is used, whether or not this is an issue with my vista, or... I don't know. If anyone can point me in the right direction, I'd be very grateful.

This post has been edited by JackOfAllTrades: 26 April 2010 - 04:33 AM
Reason for edit:: Added code tags.


Is This A Good Question/Topic? 0
  • +

Replies To: Beginning C++, question about tutorial

#2 damund01  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 34
  • Joined: 20-January 10

Re: Beginning C++, question about tutorial

Posted 26 April 2010 - 12:05 AM

View PostTeaturtle, on 25 April 2010 - 11:28 PM, said:

Hello. I had been browsing for something useful to learn in starting game developing when I, of course, came across C++. http://www.cprogramm...om/code_blocks/ is a site I had gone to, did each step, installed the CodeBlocks.

Hit f9 to compile Project1:

#include <iostream>

int main()
{
std;cout <<Hello world!<< std::endl;
return 0;
}



Now, the last step on that tutorial shows a black box with the text inside. However when I hit f9, a flicker comes up on my screen and then disappears. Does it matter that I cannot see the black box, type in it, etc? What exactly did it do? I'm sorry, this post probably looks like a heap of garbage, but I'm quite confused. I've searched, trying to find exactly how C++ is used, whether or not this is an issue with my vista, or... I don't know. If anyone can point me in the right direction, I'd be very grateful.



If you add a
system("pause");

in to main before your return you will be able to view your project. So your code would look like this
int main()
{
std::cout <<"Hello world!"<< std::endl;

system("pause");

return 0;
}



or you could do something like:

using namespace std;

int main()

{
cout <<"Hello world!"<< endl;

system("pause");

return 0;
}


The system pause just makes the console ask you to press enter to continue through the code.

However when you get a lot of namespaces in use this can get cumbersome.

This post has been edited by damund01: 26 April 2010 - 08:49 AM

Was This Post Helpful? 1
  • +
  • -

#3 keithgarry  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 63
  • Joined: 26-August 09

Re: Beginning C++, question about tutorial

Posted 26 April 2010 - 02:28 AM

Hi. Goodluck with your computer programming journey :)

Here is what your code should look like.

#include <iostream>

int main()
{
	std::cout << "Hello World!" << std::endl;
	std::cin.get();	

	return 0;
}




At first glance there is a lot going on and over time you will learn the ins and outs of everything.

You can think of
std::
being a folder that holds code. It also holds special names that the code refers to. When you become more experienced you will eventually make your own programming 'folders'. The actual term is called a namespace.


In layman's terms
cout
is a programming object that 'talks' with the console. "console output". You can think of cout as a noun or an object, and << as a verb. In this case the cout object is speaking "Hello World!" using the std (standard) library of code. endl is another object that speaks with the console. I'm pretty sure it means end line. It serves as a return or enter key.


std::cin.get()
is sort of like the opposite of cout. It gets input from the client (in this case it is the person at the keyboard). console in.

Here is a link explaining why you should not use system("PAUSE") among other practices to avoid.

What kind of games do you want to program? :)

This post has been edited by keithgarry: 26 April 2010 - 02:30 AM

Was This Post Helpful? 2
  • +
  • -

#4 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1112
  • View blog
  • Posts: 4,619
  • Joined: 09-June 09

Re: Beginning C++, question about tutorial

Posted 26 April 2010 - 09:17 AM

let me advice you not to use
system("pause");


since it is like breaking out a chainsaw to cut a piece of paper.

To pause exectution put a
cin.ignore(); //ignore istream buffer
cin.get(); //get a character from user




As a beginner i will advice you not to use
using namespace std;



since you probably have no idea what a "namespace" is.
Was This Post Helpful? 2
  • +
  • -

#5 damund01  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 34
  • Joined: 20-January 10

Re: Beginning C++, question about tutorial

Posted 26 April 2010 - 06:05 PM

View PostTeaturtle, on 25 April 2010 - 11:28 PM, said:

Hello. I had been browsing for something useful to learn in starting game developing when I, of course, came across C++. http://www.cprogramm...om/code_blocks/ is a site I had gone to, did each step, installed the CodeBlocks.

Hit f9 to compile Project1:

#include <iostream>

int main()
{
std;cout <<Hello world!<< std::endl;
return 0;
}



Now, the last step on that tutorial shows a black box with the text inside. However when I hit f9, a flicker comes up on my screen and then disappears. Does it matter that I cannot see the black box, type in it, etc? What exactly did it do? I'm sorry, this post probably looks like a heap of garbage, but I'm quite confused. I've searched, trying to find exactly how C++ is used, whether or not this is an issue with my vista, or... I don't know. If anyone can point me in the right direction, I'd be very grateful.



Additionally you can review this post from the C++ help area here on DIC Holding The Execution Window Open
Was This Post Helpful? 0
  • +
  • -

#6 alias120  Icon User is offline

  • The Sum over All Paths
  • member icon

Reputation: 123
  • View blog
  • Posts: 702
  • Joined: 02-March 09

Re: Beginning C++, question about tutorial

Posted 26 April 2010 - 07:21 PM

I had always used cin.get(), but I did not know that system("pause") was such a resource intensive command. Good to know.

-alias
Was This Post Helpful? 0
  • +
  • -

#7 csharpbacha  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 67
  • Joined: 18-March 10

Re: Beginning C++, question about tutorial

Posted 27 April 2010 - 04:33 AM

This will do the trick
#include <iostream> 
#include <conio.h> //Header file for getch() & getche()

using namespace std; //this way  you don't have to write std; again and again
int main() 
{ 
cout <<"Hello world!"<< endl; 
return 0;
getch(); 
}

Was This Post Helpful? 0
  • +
  • -

#8 damund01  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 34
  • Joined: 20-January 10

Re: Beginning C++, question about tutorial

Posted 27 April 2010 - 08:50 AM

View Postcsharpbacha, on 27 April 2010 - 04:33 AM, said:

This will do the trick
#include <iostream> 
#include <conio.h> //Header file for getch() & getche()

using namespace std; //this way  you don't have to write std; again and again
int main() 
{ 
cout <<"Hello world!"<< endl; 
return 0;
getch(); 
}



Your example here will not help you, you need to place your getch() call before the return statement like this:
#include <iostream> 
#include <conio.h> //Header file for getch() & getche()

using namespace std; //this way  you don't have to write std; again and again
int main() 
{ 
cout <<"Hello world!"<< endl; 

getch();// See by doing this you get the program to stop before the return statement there by holding the window open.

return 0;
}


Was This Post Helpful? 0
  • +
  • -

#9 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Beginning C++, question about tutorial

Posted 27 April 2010 - 09:50 AM

Sorry I didn't notice this question yesterday. Since you are using Code::Blocks the correct answer is ...

Forget about all of those system("pause"), cin.get(), getch(), etc.

Just click (on the menubar) Project / Properties which will open the Project/targets options dialog. There, click on the "Build targets" tab, and then on the right side, a few lines below "Selected build target options", check the "Pause when execution ends" box.

If you specify "console application" when you create the project, that box should normally be checked by default.
Was This Post Helpful? 0
  • +
  • -

#10 Guest_Guest*


Reputation:

Re: Beginning C++, question about tutorial

Posted 27 April 2010 - 10:40 AM

Just to let you know code::blocks, at least the version I have, doesnt require system("pause") or cin.get() to pause the program. It does it automatically if you just put return 0 on the bottom it will pause for you so it doesn't just close when you run it. Although ignoring to put cin.get() would make the program not work on a different compiler.
Was This Post Helpful? 0

#11 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Beginning C++, question about tutorial

Posted 27 April 2010 - 12:04 PM

damund01 - <conio.h> is not portable, and some COmpilers may not support it. It also contains non-standard functions (like getch()). I strongly recommend you/others not to use it anymore.
Was This Post Helpful? 0
  • +
  • -

#12 damund01  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 34
  • Joined: 20-January 10

Re: Beginning C++, question about tutorial

Posted 27 April 2010 - 12:11 PM

View Postsarmanu, on 27 April 2010 - 12:04 PM, said:

damund01 - <conio.h> is not portable, and some COmpilers may not support it. It also contains non-standard functions (like getch()). I strongly recommend you/others not to use it anymore.


Thank you for the info I will add it to the list of bad habits I need to break :)
So you would recommend the use of
cin.get
instead, am I correct being that it is more portable?

If you have any more tips keep them coming its always good to continue learning better ways to do things!
Was This Post Helpful? 0
  • +
  • -

#13 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Beginning C++, question about tutorial

Posted 27 April 2010 - 12:16 PM

Definately. But sometimes, a "cin.get()" is not enough. Consider the following example:
#include <iostream>

int main()
{
	int x;
	std::cout << "Enter a number: ";
	std::cin >> x;

	std::cout << "You have entered: " << x << std::endl;

	std::cin.get();
	return 0;
}


You can definately see that the console does not remain opened, after a number was entered. That's because of that "std::cin >> x", which captured a newline character from the Enter key that we have pressed. "std::cin.get" encounters that newline character, and skips over it's execution. Solution: add a std::cin.ignore() right before the "std::cin.get()". This will clear the buffer from the remaining characters. So the final code should look like this:
#include <iostream>

int main()
{
	int x;
	std::cout << "Enter a number: ";
	std::cin >> x;

	std::cout << "You have entered: " << x << std::endl;

	std::cin.ignore();
	std::cin.get();
	return 0;
}


Now that's better!
Was This Post Helpful? 1
  • +
  • -

#14 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Beginning C++, question about tutorial

Posted 27 April 2010 - 12:26 PM

You guys are debating endlessly about solutions that are irrelevant and unnecessary to solve the op's problem. There is no need to resort to "tricks" to keep the terminal window open in Code::Blocks** any more than it is necessary in a Linux shell terminal or a Windows command prompt window.

As I posted above, the solution is simply to check the "Pause when execution ends" box in the IDE's Project/Properties dialog.


** because Code::Blocks is the best free IDE B)

This post has been edited by r.stiltskin: 27 April 2010 - 12:30 PM

Was This Post Helpful? 1
  • +
  • -

#15 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Beginning C++, question about tutorial

Posted 27 April 2010 - 12:39 PM

"endlessly"? Not really. Based on my post, the OP knows that <conio.h> shouldn't be used, and he learned about another way to keep the console opened. I'm sorry if you think that's not helpful for him.

Quote

** because Code::Blocks is the best free IDE

That's your opinion.
Was This Post Helpful? 2
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2