我正在尝试创建一个结构来在我的主方法中接收用户输入,以便我可以将其传递以供以后使用。我浏览过该网站以获取结构帮助,但我看到人们试图将其创建为它自己的功能;我想将其作为我的 main 的一部分。以下是我尝试为闹钟实现的一些详细信息:
- 接受用户输入的所需闹钟时间
- 与本地时间进行比较
- 以秒为单位递减计时器,并在到达闹钟时打印
我收到了有关重新声明/未声明变量的轻微编译错误。这是我迄今为止的进展!
#include <time.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <termios.h>
#include <signal.h>
struct sc_alarm_struct{
int uhour, uminute, usecond;};
struct sc_alarm_struct am;
void sc_alarm();
int main(int argc, char *argv[])
{
if (argc > 1)
{
am.uhour = atoi(argv[1]);
am.uminute = atoi(argv[2]);
am.usecond = atoi(argv[3]);
}
}
void sc_alarm(int uhour, int uminute, int usecond)
{
struct tm *tm;
time_t ctime;
ctime = time(NULL);
tm = localtime(&ctime);
int alarm_total;
int local_time_total;
int difference;
int chour = tm->tm_hour;
int cminute = tm->tm_min;
int csecond = tm->tm_sec;
alarm_total = (uhour * 3600) + (uminute * 60) + usecond;
local_time_total = (chour * 3600) + (cminute * 60) + csecond;
difference = alarm_total - local_time_total;
do
{
printf("%d seconds remaining.\n", difference);
difference--;
sleep(1);
}
while(!(chour == am.uhour && cminute == am.uminute && csecond == am.usecond));
printf("\nAlarm reached, wake up!\n\n");
}
任何建议或想法将不胜感激。
最佳答案
如果您可以提供您的构建信息,以便其他人可以看到更多详细信息,那就更好了。对于这个问题,alarm()是unistd.h(man 3 Alarm,你会看到的)中定义的一个函数,你不能用这个名字声明一个变量,而且,第51行的'u'是什么?
关于c - 如何使用结构体接收用户输入以传递给另一个方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38135941/