So I have been trying to compile and run the "hello world" exercise from the tutorial for the last 45 minutes. I am using Dev-C++ and typing in the source code exactly as it appears in the tutorial, but it is not working.
1 2 3 4 5 6 7 8
#include <iostream>
usingnamespace std;
int main ()
{
cout << "Funky Monkey!";
return 0;
}
thegodamnbatman, you can use it for testing purpose, it let you have a chance to see the output before console closed by your compiler. If you run the file directly from console, you dont even need getchar(). If you are in windows, you can use System("pause");, but still for testing purpose only, because they are unsafe.
DTSCode, I just read www.cplusplus.com/articles/iw6AC542/. Thank you for the info.
If I want to do simple testing and not for final developed system, just to look at the result or just to have it pause and I want to avoid include so many lines of code, what is the best way to do this with one short line of code?
http://www.cplusplus.com/reference/istream/istream/ignore/
i think its like cin.ignore(1024, '\n'); im not dealing too much with buffers (at least not in that way) so i forget the proper thing to pass to it.
The other option is to either run your programs from the console in the first place (they are called console applications for a reason), or to update your IDE... I mean, seriously, Dev C++ is about 8 years old has numerous bugs. I would recommend changing IDE to something like eclipse, code::blocks, visual studio, or at least something like Orwell DevC++ or wxDevC++, as those are at least less out of date (and probably have fewer bugs).
The other option is to either run your programs from the console in the first place (they are called console applications for a reason)
ok... he is running it in the console... where do you think its run when he just clicks on the exe? it creates a new instance of the command prompt and runs the exe in it. and that is not a smart idea. it is much better to learn to pause the program than have to tediously open up your terminal/command prompt, cd to the folder, and type it in. and it is bad practice to depend on your ide for anything
no, and heres why: lets say i have a variable, char c;. and i want to get user input with it. so i type in 'r' and enter. but on accident i type 'rt' and then enter. the c variable only grabs the the letter r, but the t is stored in a buffer, which is what cin, getch, _getch, etc, grab from. so lets back up. when i typed rt[enter] it was stored in a buffer. using cin>> c; it grabbed the r. but the t is still stored in the buffer (most input functions/objects ignore \n as it marks the end of the buffer, so we wont worry about it). so if we want to pause with just _getch() or getch(), we still have something in the buffer (ie 't') so it takes that instead of user input
^ cin.ignore() would have a similar issue.
You cannot guarantee that it wouldn't be more than 1024 before the linebreak either
Using std::cin.ignore( std::numeric_limits< std::streamsize >::max() ); may still fail
By instance
- taking input from the user
- then follows a lengthy process, the user may accidentally press keys
- trying to "pause", it pass because the buffer had something previously
> it is much better to learn to pause the program than have to tediously open up your terminal
It is very irritating when a program ends its processing but would not terminate because it expects user input.
You could simply leave the terminal open.
> it is bad practice to depend on your ide for anything
It is not a `dependency', we have other methods to solve the issue.
But it would be nice that the IDE behaves as it should.One of its responsibilities is to provide a testing environment.