I have a Win32 program that runs on a loop. I would like to be able to pause that program while awaiting a keypress. It doesn't matter whether I use 'any key' or a specific key, but I need to have the program freeze until I press something.
I am wondering which command I should use. I am working with Visual C++ and the compiler doesn't recognise any of the following commands:
cin.get()
std::cin.get()
getch()
I am relatively new to C++. I understand that in a console app this is a fairly simple action to take (cin.get), but that it can be more difficult in Win32. Any simple solution or workaround would be appreciated. The program is bespoke to be used in a single scientific experiment, so for now I'm not fussed if the solution is a little botchy(!)
Apologies if I've missed any important info from my question.
You should use neither.
You should use
#include <iostream>
...
int main()
{
...
std::cin.ignore(); //why read something if you need to ignore it? :)
}'
Here's the documentation
Example:
#include <iostream>
#include <conio.h>
int main()
{
std::cout << "Press any key to continue . . ." << std::endl;
_getch(); // wait for keypress
}
_getch() is C++ equivalent to C getch()
Try
#include <iostream>
using namespace std;
char temp;
cin >> temp;
Assuming that you are looking for an alternative for getch ( which does not echo to screen).
If you are using windows and visual studio to be precise try using _getch.
Here is a link to it
http://msdn.microsoft.com/en-us/library/078sfkak(v=VS.100).aspx
You should #include <iostream> and use std::cin.get();
I think the getch() is a C function, but since you are using C++, then the cin would be more appropriate.
HWND hwnd = ::GetConsoleWindow();
while (!((::GetForegroundWindow() == hwnd) &&
((::GetKeyState(VK_SPACE) & 0x8000) != 0)))
::Sleep(0);
Suppose it is not the best way but it solved my problem.
Replace VK_SPACE with any other value you like.
And it is not portable.
Related
I am new to trying to write anything. While I can read what's happening most of the time, I have no idea how to build a delay. In Arduino I have used delays but it doesn't seem to work the same here.
I have been searching the internet trying to find something that will work, but with no luck. I think I could make something work but I don't know how to add more '#includes' either. Currently I have-
#include <xbee_config.h>
#include <types.h>
#include <utils.h>
#include <xbee/atcmd.h>
I have the general idea of what is needed but now idea how to write it. I'm turning on an LED that I need delayed before turning off.
gpio_set(LED1, 1); //Turn on LED
**Delay here!!!!**
gpio_set(LED1, 0); //Turn off LED
My first thought is building a void_delay function that will increment a counter until x time is reached, then return to the program. I know that's not the best way as it will be keeping the program from other tasks while counting, but it should work for my purpose. The problem, I have no idea how to write that.
In c++ you can use Sleep(milliseconds) you only have to include <windows.h>.
Example:
#include <iostream>
#include <windows.h>
using namespace std;
int main() {
cout << "Before delay" <<endl;
Sleep(5000);
cout << "After delay" <<endl;
return 0;
}
I'm learning Cpp programming and I'm using Dev-C++ as compiler. I made this example to see how class & objects works in this programming language but the problem is the compiler does not even running the code! Here's the code:
#include <iostream>
using namespace std;
class BuckysClass{
public:
void coolSaying(){
cout << "Some Sentences" << endl;
}
};
int main(){
BuckysClass buckysObj;
buckysObj.coolSaying();
return 0;
}
I have saved the file with .cpp extension and tried to run it by pressing Ctrl+F10.
Please if u know what I'm doing wrong ,let me know I really appreciate that. Thanks in advance...
First of all:
F9 - compile the source program
F10 - run the source program
In case if your terminal disappears, you can add getchar() before return 0. This will make the command prompt wait for your input, and thus you will be able to see the results.
PS: Don't use Dev-C++. It hasn't been updated for a long time.
You just need to add a getchar() or system("pause") before return 0
Nothing! The command prompt just appears in just 1 second and then
disappear...
Need to add a system("PAUSE"); (I'm supposing you are under Windows)
Try this:
class BuckysClass{
public:
void coolSaying(){
cout << "Some Sentences" << endl;
}
};
int main(){
BuckysClass buckysObj;
buckysObj.coolSaying();
system("PAUSE");
return 0;
}
Try to build it first then run it. also you need to add system("pause") before return 0 to pause the screen so you can see the result.
I highly recommand you use either CodeBlocks or Eclipce rather than Dev-C++.
I've re installed my dev c++ and now I have problem that when I run the application of this simple code:
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
cout<<"hi mateen";
return 0;
}
it shows me this:
How can I delete this? It wasn't showing before always used to use
getche()
but even when I use
return 0;
the problem is still the same.
thanks
Turn off "Pause console programs after return" option in "Tools > Environment Options..."
my file structure for executing a .exe is something like this
c:\Documents and settings\Desktop\Release\abc.exe
i want to execute this from other c++ program in vb c++ after building, it generates an error that c:\Document is not external or internal command
few lines of code are as follows:
#include<stdlib.h>
#include<stdio.h>
int main( void ) {
int result;
result=system("c:\\Documents and settings\\Desktop\\Release\\abc.exe");
getchar();
return 0;
}
As I suspected when writing an earlier comment, the way to do it is to wrap the entire string in double-quotes. 'Escaping the spaces' sounds non-sensical to me. 25 seconds of googling and I don't see (nor have I heard of in over 20 years) an escape-sequence for a space character in C.
The solution is indeed to include quotes in the string - not to just wrap the string in a single pair of them, as you've done. The following will do the trick:
#include <stdlib.h>
#include <stdio.h>
int main()
{
int result;
result = system("\"c:\\Documents and settings\\Desktop\\Release\\abc.exe\"");
getchar();
return 0;
}
However, that said - you shouldn't really be using the system call for this job. Since you're on a windows machine, you should use the ShellExecute function instead. There are many reasons for this, which I wont go into here, you can look them up yourself. But suffice to say it's an infinitely better way to invoke another program.
More on ShellExecute: http://msdn.microsoft.com/en-us/library/windows/desktop/bb762153(v=vs.85).aspx
I am trying to make a program to ask for their name, and then say "Hello, (their name)!" back.
Here's my code so far, the "getchar()" is just so it pauses and I can see the output.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout<<"What is your name?:";
cin>>name;
cout<<"Hello, "<<name<<"!";
getchar();
return 0;
}
This asks me for input, and I input my name, and then the application closes!
I don't know why and how to fix it! Please help!
EDIT: Found out how to solve it. Finished code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout<<"What is your name?: ";
cin>>name;
cout<<"Hello, "<<name<<"!\n";
system("PAUSE");
return 0;
}
Dietmar wrote the correct answer, unfortunately as a comment for some strange reason.
getchar() is already a hack but I'll let you off. Replacing it with something like system("PAUSE") is even more of a hack, so let's not go there.
Your getchar() is working, but there is still a \n in the buffer from just after the name (remember, you had to type ENTER to submit it!) and this is satisfying getchar() without further user intervention.
You can get rid of that ghost newline:
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
getchar();
However, please consider configuring your execution environment to keep the console window, rather than making your program take on responsibility for this. It's there to take user input and give computed output, not to manage terminal windows.
If you're on Windows, I find cmd.exe /K myProgram to be helpful — the /K runs your program then keeps the command prompt open.
just use the function getch() from <conio.h> !
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;
int main()
{
string name;
cout<<"What is your name?:\n";
cin>>name;
cout<<"Hello, "<<name<<"!\n";
getch();
return 0;
}
Very easy to use, safe and way better than system ("pause")!