C++ 如何告诉我的程序在等待用户发出命令时执行某些操作?
我正在尝试制作一个计时器,它将从用户命令它的时间量计数到零 现在,我正在尝试添加一个暂停功能,这将要求我的程序在计时器滴答作响时接受和读取输入 这是我目前掌握的代码-C++ 如何告诉我的程序在等待用户发出命令时执行某些操作?,c++,C++,我正在尝试制作一个计时器,它将从用户命令它的时间量计数到零 现在,我正在尝试添加一个暂停功能,这将要求我的程序在计时器滴答作响时接受和读取输入 这是我目前掌握的代码- #include <iostream> #include <stdio.h> #include <time.h> #include <unistd.h> #include <dos.h> #include <windows.h> using namespace
#include <iostream>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <dos.h>
#include <windows.h>
using namespace std;
// sleep(5000);
int seconds;
int hoursLeft;
int minutesLeft;
int secondsCount=0;
void timeLeft ()
{
hoursLeft = seconds/3600;
minutesLeft = seconds/60 - hoursLeft*60;
}
void timer ()
{
if (secondsCount == 60)
{
timeLeft();
cout << "The Amount of time left is: " << hoursLeft << " hours and " << minutesLeft << " minutes left." << endl;
secondsCount=0;
}
secondsCount++;
seconds--;
Sleep(1000);
timer();
}
int main()
{
// introduction and time picking
cout << "Welcome to my Timer - Please set the amount of hours and than minutes you want the timer to run" << endl;
double requestedHours, requestedMinutes;
cin >> requestedHours;
cin >> requestedMinutes;
double requestedSeconds = requestedHours*3600 + requestedMinutes*60;
seconds = requestedSeconds;
cout << "Timer Started";
timer();
}
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
//睡眠(5000);
整数秒;
内小时左;
左一分钟;
int secondscont=0;
无效时间限制()
{
左小时=秒/3600;
分钟左=秒/60-小时左*60;
}
无效计时器()
{
如果(secondsCount==60)
{
时间限制();
不能包含
#包括
#包括
int main()
{
字符缓冲区[16];
国际旗帜;
int-fd;
INTR;
fd=0;//stdin
标志=fcntl(fd,F_GETFL,0);
fcntl(fd、F|U设置FL、标志| O|U非块);
而(1)
{
memset(buffer,0,sizeof(buffer));
r=read(0,buffer,sizeof(buffer));//返回读取的字节数
如果(r>0)//读取了某些内容
{
printf(“读取:%d\n”,缓冲区[0]);
fflush(stdin);
}
否则//
{
放置(“此处更新计时器”);
}
美国LEEP(50000);
}
返回(0);
}
在文件描述符上使用非阻塞读取也很酷
对不起,我只有C语言的解决方案
PS:你的计算机不应该无限递归地工作,你应该使用循环而不是无限递归(timer()调用自身),否则你的堆栈将溢出
#包括
#包括
int main()
{
字符缓冲区[16];
国际旗帜;
int-fd;
INTR;
fd=0;//stdin
标志=fcntl(fd,F_GETFL,0);
fcntl(fd、F|U设置FL、标志| O|U非块);
而(1)
{
memset(buffer,0,sizeof(buffer));
r=read(0,buffer,sizeof(buffer));//返回读取的字节数
如果(r>0)//读取了某些内容
{
printf(“读取:%d\n”,缓冲区[0]);
fflush(stdin);
}
否则//
{
放置(“此处更新计时器”);
}
美国LEEP(50000);
}
返回(0);
}
在文件描述符上使用非阻塞读取也很酷
对不起,我只有C语言的解决方案
PS:你的计算机不应该无限递归地工作,你应该使用循环而不是无限递归(timer()调用自身),否则您的堆栈将溢出。学习多线程编程的时间。.感谢您的指导,我阅读了它,当我试图添加#include我得到了这个按摩编辑-尽管我认为它是可行的,如果我有问题,我会与您联系。我自己也无法解决,也无法在web上找到它,我现在的问题是:#如果没有GXX##此文件需要对ISO C++ 2011标准的编译器和库支持。此支持目前是实验性的,必须启用-STD= C++ 11或-STD= GNU+++编译器选项。endif@Tomer8009您使用的是什么编译器和环境代码块,但我现在找到了答案。thnanks。是时候学习多线程程序了mming..谢谢你的指导,我读了它,当我试图添加#include我得到了这个按摩编辑-尽管我认为它是可行的,如果我有问题,我会联系你我自己可以解决,也不能在网上找到它,我现在的问题是:#如果ndef GXX_experional#cx0x#错误此文件需要编译器和库支持\ISOC++支持2011标准。这个支持目前是实验性的,必须使用-STD= C++ 11或-STD= GNU++11编译器选项来启用。endif@Tomer8009您使用的是什么编译器和环境代码块,但我现在找到了答案。thnanks。是时候学习多线程编程了。谢谢您的指导,我读了它,当我试图添加我得到了这个按摩编辑-没想到我认为它是可行的,如果我有问题我可以自己修复,也不在网上找到它,我现在的问题是:IFIFNDEF GXXX实验AxCX0X X错误。这个文件需要编译器和库支持的ISO C++ 2011标准。使用-std=c++11或-std=gnu++11编译器选项启用#endif@Tomer8009您使用的是什么编译器和环境代码块,但我现在找到了答案。
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
int main()
{
char buffer[16];
int flags;
int fd;
int r;
fd = 0; //stdin
flags = fcntl(fd, F_GETFL, 0);
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
while (1)
{
memset(buffer, 0, sizeof(buffer));
r = read(0, buffer, sizeof(buffer)); //return the number of bytes it reads
if (r > 0) //something was read
{
printf("read: %d\n", buffer[0]);
fflush(stdin);
}
else //nothing has been read
{
puts("update timer here");
}
usleep(50000);
}
return (0);
}