I cannot get (or see) the output (‘Hello World 77’) from my first program.
Analysis
=== Version: 4.9.9.2
=== OS: Windows XP Pro
=== Program:
include <iostream>
using namespace std;
// declare the obligatory main() function
int main()
{
// call the cout function to output a string
cout << "Hello World 77\n";
// getchar (); // Try this - no
// cin.get(); // Try this - no
system("pause"); // Try this - window only
// std::cin.get(); // Try this - no
// std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n'); // Try this - no
// return 0 to confirm that the program ran ok
return 0;
}
I am new to C++ and to Dev-C++ so I know I’m doing something really silly and I know I’m going to look a twit so let’s get it over with.
I have checked the ‘PLEASE READ BEFORE …’ section and I’ve tried various combinations of commands from various examples without success so here are my questions
Q1. Why doesn’t ‘getchar ();’ work?
Q2. Why doesn’t ‘cin.get();’ work?
Q3. When using ‘system("pause");’ the DOS window does stay there but I cannot see the glorious words ‘Hello World 77’
Also, sometimes the links from the ‘PLEASE READ BEFORE …’ section look like this:-
Please come back in the begining of Dec to this page. It is curently not available
SOLUTION
I’d be ever so grateful for your comments
Many thanks
Mike
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
A) The chances are that <cstdio> or <stdio.h> are indirectly included by other headers such as <iostream>. This is by no means guaranteed, and so code that relies on this is non-portable. In C if a prototype is not found, a function returning int with unknowm parameters is assumed (usually with a warning). In C++ it is just illegal.
B) If you use certain a project templates, they create initial code in main.cpp. You are free to remove this from the project and add your own. Presumably you did remove it from your project other wise you will have had two main() functions.
C) Anywhere you like in any path that does not contain spaces, and is not in c:\dev-cpp (i.e. NOT C:\Dev-Cpp\Source as you suggested!). I use c:\projects\<projectname>. You just navigate to or create an appropriate folder from within the file select box when you are prompted to save teh project file. Dev-Cpp has a nasty habit of offereing up c:\dev-cpp as default. I think you can fix this by changing the start directory property in the shortcut proerties used for starting teh IDE. Unfortunately you will have to do this for the desktop icon, the quick-start icon and teh start-menu entry.
Clifford
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
All of the approaches you tried should work or at least do something (although you do need to include the appropriate headers for them to compile).
However there are some issues with your build:
First (and this is the cause of your problem); you appear to have told the linker to create a GUI application. (the -mwindows option says that). This supresses creation of a console window. The only reason you get a console window when you use system() is because system causes a new cmd.exe process to be spawned. Make sure you create your project initially as the correct type. You can change the type retrospectively in the Project settings dialog.
Second; there is a bug in Dev-C++ that sometimes causes projects stored in c:\dev-cpp or sub-folders of that to fail to build correctly. It is a bad idea in any case to 'pollute' an applications installation folder with user files.
Clifford
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Many thanks for your prompt reply, I really appreciate it.
I went to ‘Project Options’ and changed ‘Type’ from ‘Win32 GUI’ to ‘Win32 Console’ and it works! BRILLIANT! I’m a lot happier now!
I have a couple of follow up questions
A). When I include the ‘getchar()’ statement and OMIT the ‘#include <cstdio>’ statement the program still compiles and runs OK.
Q4. Is this correct or should there be an error/warning during compilation?
B). Under my project ‘Training01’ there are two ‘.cpp’ files. The First one is ‘HelloWorld.cpp’ which I typed in and the second is ‘main.cpp’.
Q5. Where did ‘main.cpp’ come from?
C) Re:- It is a bad idea in any case to 'pollute' an applications installation folder with user files.
Q6. Where, in your experience, should I put my user files (*.cpp files?) and what parameter should I change (I suppose it should be ‘Environment Options’ then ‘Files & Dir…’ then set ‘User’s Default Directory’ to read ‘C:\Dev-Cpp\Source’)
Thanks once again
Mike
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Problem
I cannot get (or see) the output (‘Hello World 77’) from my first program.
Analysis
=== Version: 4.9.9.2
=== OS: Windows XP Pro
=== Program:
include <iostream>
using namespace std;
// declare the obligatory main() function
int main()
{
// call the cout function to output a string
cout << "Hello World 77\n";
// getchar (); // Try this - no
// cin.get(); // Try this - no
system("pause"); // Try this - window only
// std::cin.get(); // Try this - no
// std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n'); // Try this - no
// return 0 to confirm that the program ran ok
return 0;
}
=== Compile Log:
Compiler: Default compiler
Building Makefile: "C:\Dev-Cpp\Training\Makefile.win"
Executing make...
make.exe -f "C:\Dev-Cpp\Training\Makefile.win" all
g++.exe -DDEBUG main.o HelloWorld.o -o "Training01.exe" -L"C:/Dev-Cpp/lib" -mwindows -g3
Execution terminated
Compilation successful
I am new to C++ and to Dev-C++ so I know I’m doing something really silly and I know I’m going to look a twit so let’s get it over with.
I have checked the ‘PLEASE READ BEFORE …’ section and I’ve tried various combinations of commands from various examples without success so here are my questions
Q1. Why doesn’t ‘getchar ();’ work?
Q2. Why doesn’t ‘cin.get();’ work?
Q3. When using ‘system("pause");’ the DOS window does stay there but I cannot see the glorious words ‘Hello World 77’
Also, sometimes the links from the ‘PLEASE READ BEFORE …’ section look like this:-
Please come back in the begining of Dec to this page. It is curently not available
SOLUTION
I’d be ever so grateful for your comments
Many thanks
Mike
A) The chances are that <cstdio> or <stdio.h> are indirectly included by other headers such as <iostream>. This is by no means guaranteed, and so code that relies on this is non-portable. In C if a prototype is not found, a function returning int with unknowm parameters is assumed (usually with a warning). In C++ it is just illegal.
B) If you use certain a project templates, they create initial code in main.cpp. You are free to remove this from the project and add your own. Presumably you did remove it from your project other wise you will have had two main() functions.
C) Anywhere you like in any path that does not contain spaces, and is not in c:\dev-cpp (i.e. NOT C:\Dev-Cpp\Source as you suggested!). I use c:\projects\<projectname>. You just navigate to or create an appropriate folder from within the file select box when you are prompted to save teh project file. Dev-Cpp has a nasty habit of offereing up c:\dev-cpp as default. I think you can fix this by changing the start directory property in the shortcut proerties used for starting teh IDE. Unfortunately you will have to do this for the desktop icon, the quick-start icon and teh start-menu entry.
Clifford
Hi Mike,
thats strange,
include <iostream>
int main ()
{
std::cout << "hello world 77\n" ;
system ("pause') ;
return 0 ;
}
will print the text to screen and hold the screen, for me.
getchar() belongs to the <cstdio> header.
I guess you'd have to #include <cstdio> to use it.
thats system( "pause" ) btw.
... and it needs #include <stdlib.h> or #include <cstdlib>, but that is not his problem - he supressed the console window by specifying a GUI build.
All of the approaches you tried should work or at least do something (although you do need to include the appropriate headers for them to compile).
However there are some issues with your build:
First (and this is the cause of your problem); you appear to have told the linker to create a GUI application. (the -mwindows option says that). This supresses creation of a console window. The only reason you get a console window when you use system() is because system causes a new cmd.exe process to be spawned. Make sure you create your project initially as the correct type. You can change the type retrospectively in the Project settings dialog.
Second; there is a bug in Dev-C++ that sometimes causes projects stored in c:\dev-cpp or sub-folders of that to fail to build correctly. It is a bad idea in any case to 'pollute' an applications installation folder with user files.
Clifford
Even I knew that !
Thanks for the head up Clifford.
peace.
errm, I mean heads up.
: )
Kurgusov, constantly wrong.
Cliffod,
Many thanks for your prompt reply, I really appreciate it.
I went to ‘Project Options’ and changed ‘Type’ from ‘Win32 GUI’ to ‘Win32 Console’ and it works! BRILLIANT! I’m a lot happier now!
I have a couple of follow up questions
A). When I include the ‘getchar()’ statement and OMIT the ‘#include <cstdio>’ statement the program still compiles and runs OK.
Q4. Is this correct or should there be an error/warning during compilation?
B). Under my project ‘Training01’ there are two ‘.cpp’ files. The First one is ‘HelloWorld.cpp’ which I typed in and the second is ‘main.cpp’.
Q5. Where did ‘main.cpp’ come from?
C) Re:- It is a bad idea in any case to 'pollute' an applications installation folder with user files.
Q6. Where, in your experience, should I put my user files (*.cpp files?) and what parameter should I change (I suppose it should be ‘Environment Options’ then ‘Files & Dir…’ then set ‘User’s Default Directory’ to read ‘C:\Dev-Cpp\Source’)
Thanks once again
Mike