How do I activate programs?

Feb 16, 2013 at 1:06am
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.
Feb 16, 2013 at 1:11am
Do you have a compiler?
Feb 16, 2013 at 1:18am
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.
Feb 16, 2013 at 1:19am
"Do you have a compiler?"

How do I know if I have one? if I do not have, how do I get one?
Feb 16, 2013 at 3:11am
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;
}
Feb 16, 2013 at 3:53am
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 Feb 16, 2013 at 4:10am
Feb 16, 2013 at 12:43pm
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?
Feb 16, 2013 at 4:43pm
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 Feb 16, 2013 at 4:43pm
Feb 16, 2013 at 6:56pm
"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?
Feb 16, 2013 at 7:51pm
closed account (LAM21hU5)
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.
Feb 16, 2013 at 7:58pm
closed account (LAM21hU5)
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.
Feb 16, 2013 at 10:01pm
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?
Feb 17, 2013 at 2:48am
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.