How do I activate programs?

I have written all that I was told to by the tutorial's first lesson:http://www.cplusplus.com/doc/tutorial/, it is the lesson where you write "hello world", but unfortunately I do not know how to make that phrase appear on the screen. I said activate program because I do not know how to refer to this.

By the way: thanks in advance for all the help you give me.
Do you have a compiler?
I have just tried to compile, to click compile, and then run, when I do that a new window appear and close so quickly that I can not read anything in it.
"Do you have a compiler?"

How do I know if I have one? if I do not have, how do I get one?
There are a few ways to do this, though cin.get() is the most common from what I've seen. If your compiled code looks like this, then you're on the right track. The cin.get() function waits for the user to press a key, and then continues to the next line "return 0" which exits your program without errors.

1
2
3
4
5
6
7
8
9
#include <iostream.h>
using namespace std;

int main(int argc, char **argv)
{
  cout << "Hello World";
  cin.get();
return 0;
}
do this at the end of your console application:

1
2
3
4
5
6
7
8
int main()
{
     
    // std::cin.get(); if you use cin >> 'something' anywhere.
    std::cout << "press Enter to close" << std::endl;
    std::cin.ignore(99, '\n');
    return 0;
}


Edit;

You will find that if you use std::cin anywhere and don't have std::cin.get() to catch the user pressing 'enter'. Your exit method will get fuzzy.

I'm still new to C++ myself (coming from C#), and I find myself using std::cin.get() on everyline that uses cin >> somevariable. However with strings you can use: getline(cin, variable) but you need to: #include <string> for that.
Last edited on
Thank yo for the help, I did make the program run after copying your lines and pasting on my c++, however I do not understand why when I copy and paste from the tutorial I can not run the program. Why is that?
A lot of tutorials seem to neglect code to pause the program at the end. I had this issue when I was first learning C# years ago. however, if your using Visual Studio, you can do ctrl+F5 instead of F5. Then you can test your program without adding those lines.
Last edited on
"A lot of tutorials seem to neglect code to pause the program at the end."

What is this code to pause the program at the end, and how can I insert it into the program?
An easy way to pause a program is to put system ("Pause") at end of code, but I wouldn't recommend it.
Another easy way is to put getch(); at end of code which does the same thing in a safer way. But you need to add #include<conio.h> at the beggining of code.
Both will force you to push a button to continue, but getch(); is much safer.
I forgot to give some examples:

With System("Pause");:

1
2
3
4
5
6
7
#include<iostream>
using namespace std;
int main(){
printf("Hello world");
System ("Pause");
return 0;
}


With getch();:

1
2
3
4
5
6
7
8
#include<iostream>
#include<conio.h>
using namespace std;
int main(){
printf("Hello world");
getch();
return 0;
}


Hope I helped.
zaqcde, what do you mean when you say that you cannot run the program? This is entirely dependent on your compiler/IDE.

What program are you copying this C++ code into?
mitaka12102: thanks, I was able to close the program with
getch();

mezmiro: "What program are you copying this C++ code into?"
Unfortunately I do not even know which compiler/IDE I am using. What they are for. Or how to change them. I just click "compile" on my c++ and then "run".
Topic archived. No new replies allowed.