Jul 6, 2011 #1 E etiquoe Member level 1 Joined Feb 18, 2010 Messages 35 Helped 4 Reputation 8 Reaction score 1 Trophy points 1,288 Location Indonesia Visit site Activity points 1,525 Hi everyone! i started to learn C++. i'm totally newbie i'm using Dev C++ compiler. i tried to run some simple examples from tutorials, like this: Code C++ - [expand]1 2 3 4 5 6 7 #include <iostream> using namespace std; void main () { cout << “Hello, World!\n”; } Or this: Code C++ - [expand]1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include <iostream> using namespace std; int main () { int i = 1; while (i<=257) { cout << i << "\n"; ++i; } } No Error when i compiled it, but nothing happen when i ran it. And then i tried to add getchar()function, like this: Code C++ - [expand]1 2 3 4 5 6 7 8 #include <iostream> using namespace std; void main () { cout << “Hello, World!\n”; getchar(); } and this: Code C++ - [expand]1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include <iostream> using namespace std; int main () { int i = 1; while (i<=257) { cout << i << "\n"; ++i; } getchar(); } Then it worked. I don't really understand about getchar() function.why the two examples above can work after i add the line (getchar())? anyone? please? Regards
Hi everyone! i started to learn C++. i'm totally newbie i'm using Dev C++ compiler. i tried to run some simple examples from tutorials, like this: Code C++ - [expand]1 2 3 4 5 6 7 #include <iostream> using namespace std; void main () { cout << “Hello, World!\n”; } Or this: Code C++ - [expand]1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include <iostream> using namespace std; int main () { int i = 1; while (i<=257) { cout << i << "\n"; ++i; } } No Error when i compiled it, but nothing happen when i ran it. And then i tried to add getchar()function, like this: Code C++ - [expand]1 2 3 4 5 6 7 8 #include <iostream> using namespace std; void main () { cout << “Hello, World!\n”; getchar(); } and this: Code C++ - [expand]1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include <iostream> using namespace std; int main () { int i = 1; while (i<=257) { cout << i << "\n"; ++i; } getchar(); } Then it worked. I don't really understand about getchar() function.why the two examples above can work after i add the line (getchar())? anyone? please? Regards
Jul 6, 2011 #2 H hamidmoallemi Full Member level 2 Joined Jan 28, 2010 Messages 148 Helped 41 Reputation 78 Reaction score 39 Trophy points 1,308 Visit site Activity points 2,146 getchar causes the program to wait for entering a character from user in fact your programs work but maybe the window close immediately after working once
getchar causes the program to wait for entering a character from user in fact your programs work but maybe the window close immediately after working once
Jul 6, 2011 #3 S srizbf Advanced Member level 5 Joined Apr 3, 2010 Messages 1,992 Helped 417 Reputation 840 Reaction score 329 Trophy points 1,363 Visit site Activity points 10,985 i think you are trying to execute the program from the ide. instaed , open a command prompt and type the name of your program.exe (this is without getchar(0 line program) you will see the output.
i think you are trying to execute the program from the ide. instaed , open a command prompt and type the name of your program.exe (this is without getchar(0 line program) you will see the output.