Hi,
Yes, I'm fully aware that console window disappearing has been covered in the "PLEASE READ BEFORE POSTING A QUESTION" but I'd like to ask anyway...
First, and I'm guessing the answer is no, is it possible to get a pane at the bottom of the main Dev-C++ window showing program output (like the output you would get if you were developing from a DOS prompt)?
Second, I have tried using the cin.get() to prevent the console window disappearing before I see what it's displaying and this has mostly worked. However, with the following code, this falls over and I'm not sure why:
// ask for a person's name, and greet them.
include <iostream>
include <string>
int main()
{
// ask for their name
std::cout << "What is your first name?" <<std::endl;
// read in the namestd::stringname;// define namestd::cin>>name;// read into name// write the greetingstd::cout<<"Hello,"<<name<<".Nicetomeetyou."<<std::endl;// wait for the person to press enter before exiting...std::cin.get();return0;
}
Very simple program which should ask for a person's name and greet them. The program seems to exit prematurely when I enter a name and press enter to confirm the input and I don't see the pleasant greeting. Anyone got any suggestions?
Cheers,
jon
P.s. Just started learning C++ hence the simple program. I've done plenty of Java before but thought I'd start right at the beginning.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thanks for your extensive reply, Clifford. Now I understand a bit more about what the code was doing, in particular the input stream. I've tried your first solution and it works exactly as expected. Thanks again,
jon
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
You should still apply the buffer clearing solution so that the 'wait before closing' is guranteed to work regardless of changes you may make to this code.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
A few things to appreciate about console input and istream:
1) Input form stdin is 'line oriented'. That means the OS buffers and entire line and only when <newline> is pressed does it make the input available to the istream object cin.
2) Now formatted input (using the >> operator on an istream) uses whitespace (<space>, <tab> and <newline>) as a delimiter.
The consequences of teh above two facts is that when you enter:
What is your first name?
Jon<newline>
The string 'name' contains "Jon", the <newline> remains buffered. When you then call an input function on cin such as cin.get(), it is immediatly satisfied by the remaining buffered input so does not block on further input.
There are two obvious solutions to your problem:
1) Use std::getline(). This gets input upto and including a delimiter, and discards the delimiter. The default delimiter is <newline>, so simply:
std::getline( std::cin, name) ;
will work.
2) Discard buffered input before getting further input, thus:
include <limits>
...
// wait for the person to press enter before exiting...
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
std::cin.get();
The following simpler method also works, but has ill-defined behaviour and non-portable:
// wait for the person to press enter before exiting...
std::cin.sync();
std::cin.get();
The answer to your other question is as you suspect 'no'. I think you can in fact do this via the debugger command interface, but it is cumbersome, and given the flakiness of the GDB debugger integration in Dev-C++ in any case you probably don't want to.
There are other solutions to the 'closing the console' problem, you can configure the tools menu to run your code via cmd.exe with the /k switch. But whether it is worth it depends on how much of a problem you think the other workarounds are.
Clifford
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi,
Yes, I'm fully aware that console window disappearing has been covered in the "PLEASE READ BEFORE POSTING A QUESTION" but I'd like to ask anyway...
First, and I'm guessing the answer is no, is it possible to get a pane at the bottom of the main Dev-C++ window showing program output (like the output you would get if you were developing from a DOS prompt)?
Second, I have tried using the cin.get() to prevent the console window disappearing before I see what it's displaying and this has mostly worked. However, with the following code, this falls over and I'm not sure why:
// ask for a person's name, and greet them.
include <iostream>
include <string>
int main()
{
// ask for their name
std::cout << "What is your first name?" <<std::endl;
}
Very simple program which should ask for a person's name and greet them. The program seems to exit prematurely when I enter a name and press enter to confirm the input and I don't see the pleasant greeting. Anyone got any suggestions?
Cheers,
jon
P.s. Just started learning C++ hence the simple program. I've done plenty of Java before but thought I'd start right at the beginning.
Thanks for your extensive reply, Clifford. Now I understand a bit more about what the code was doing, in particular the input stream. I've tried your first solution and it works exactly as expected. Thanks again,
jon
You should still apply the buffer clearing solution so that the 'wait before closing' is guranteed to work regardless of changes you may make to this code.
A few things to appreciate about console input and istream:
1) Input form stdin is 'line oriented'. That means the OS buffers and entire line and only when <newline> is pressed does it make the input available to the istream object cin.
2) Now formatted input (using the >> operator on an istream) uses whitespace (<space>, <tab> and <newline>) as a delimiter.
The consequences of teh above two facts is that when you enter:
What is your first name?
Jon<newline>
The string 'name' contains "Jon", the <newline> remains buffered. When you then call an input function on cin such as cin.get(), it is immediatly satisfied by the remaining buffered input so does not block on further input.
There are two obvious solutions to your problem:
1) Use std::getline(). This gets input upto and including a delimiter, and discards the delimiter. The default delimiter is <newline>, so simply:
std::getline( std::cin, name) ;
will work.
2) Discard buffered input before getting further input, thus:
include <limits>
...
// wait for the person to press enter before exiting...
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
std::cin.get();
The following simpler method also works, but has ill-defined behaviour and non-portable:
// wait for the person to press enter before exiting...
std::cin.sync();
std::cin.get();
The answer to your other question is as you suspect 'no'. I think you can in fact do this via the debugger command interface, but it is cumbersome, and given the flakiness of the GDB debugger integration in Dev-C++ in any case you probably don't want to.
There are other solutions to the 'closing the console' problem, you can configure the tools menu to run your code via cmd.exe with the /k switch. But whether it is worth it depends on how much of a problem you think the other workarounds are.
Clifford