Compiler trouble

Pages: 12
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>
using namespace std;

int main ()
{
  cout << "Funky Monkey!";
  return 0;
}



Please help!
Last edited on
closed account (Dy7SLyTq)
define not working. thats very vague. is it not compiling? is the window not staying open? it is giving wierd errors?
When I click compile and run, what looks like a window flashes quickly on screen; then disapears. Nothing happens after that.

closed account (Dy7SLyTq)
search the articles for keeping your window open. thats the source of your issues
It works, but the console is closed.
Try this:
1
2
3
4
5
6
7
8
9
#include <iostream>
using namespace std;

int main ()
{
  cout << "Funky Monkey!";
  getchar();
  return 0;
}


The getchar() wait for a key hit before the console is closed.
Last edited on
Thank you!

So getchar (); is what made it work? Why is that?
closed account (Dy7SLyTq)
it waits for the user to input a character. i wouldnt rely on it too much for that use. it can be dangerous www.cplusplus.com/articles/iw6AC542/
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.
Last edited on
closed account (Dy7SLyTq)
you can use it for testing purpose

you can use a lot of things for testing purposes. that doesnt mean you should though

System("pause")

you shouldnt use system. its evil
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?
closed account (Dy7SLyTq)
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).
or maybe search for a setting in your IDE that says "keep console open after execution" something like that ..
closed account (Dy7SLyTq)
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
You can click and the drag the program from windows explorer into the command line, and it copies the path there...
closed account (Dy7SLyTq)
and like i said. that is bad practice
now please mark the article as solved...
Is _getch() okay to use for the same purpose ?
closed account (Dy7SLyTq)
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.
Pages: 12