C 指针、结构和输入
因此,我制作了一个程序,以以下格式向用户询问时间: HH:MM:SS 我已经记下了密码。。除了一件事。 假设用户输入如下内容: 12 此时,由于用户点击“回车”,我希望程序显示一个错误。 所以,这就是我的问题:如果用户输入12:42141或其他类似的内容 我希望程序停止,而不是让scanf等待3个整数。 我该如何编写代码 无论如何,下面是实际的程序:C 指针、结构和输入,c,pointers,structure,scanf,C,Pointers,Structure,Scanf,因此,我制作了一个程序,以以下格式向用户询问时间: HH:MM:SS 我已经记下了密码。。除了一件事。 假设用户输入如下内容: 12 此时,由于用户点击“回车”,我希望程序显示一个错误。 所以,这就是我的问题:如果用户输入12:42141或其他类似的内容 我希望程序停止,而不是让scanf等待3个整数。 我该如何编写代码 无论如何,下面是实际的程序: #include <stdio.h> #include <stdlib.h> #include <string.h&
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define true 1
#define false 0
struct Time
{
int hours;
int minutes;
int seconds;
};
void AddOneSecond(struct Time *timePtr);
int CheckForErrors(struct Time timeVars, char charOne, char charTwo);
int main()
{
auto char firstColon;
auto char secondColon;
auto int secondsAdded;
auto int errorCheck;
struct Time userInput;
// Menu prompt
printf("Enter the start time in HH:MM:SS format: ");
scanf("%d %c %d %c %d", &userInput.hours
, &firstColon
, &userInput.minutes
, &secondColon
, &userInput.seconds);
// Check for errors
errorCheck = CheckForErrors(userInput, firstColon, secondColon);
// If the input format is correct, proceed with the program. If it's not,
// display an error message and terminate the program.
if (errorCheck)
{
printf("How many seconds would you like to add? ");
scanf("%d", &secondsAdded);
// Loop that calls the 'AddOneSecond' function 'secondsAdded' amount
// of times.
for (int i = 0; i < secondsAdded; i++)
{
AddOneSecond(&userInput);
printf("%02d:%02d:%02d \n", userInput.hours
, userInput.minutes
, userInput.seconds);
}
}
else
{
puts("Error reading input...");
return 0;
}
return 0;
}
void AddOneSecond(struct Time *timePtr)
{
timePtr->seconds++;
if (timePtr->seconds == 60)
{
timePtr->seconds = 00;
timePtr->minutes += 1;
}
if (timePtr->minutes == 60)
{
timePtr->minutes = 00;
timePtr->hours += 1;
}
}
int CheckForErrors(struct Time timeVars, char charOne, char charTwo)
{
auto int isInputValid = true;
if ((timeVars.hours < 0) || (timeVars.hours > 24))
{
isInputValid = false;
puts("The 'hours' value is invalid.");
}
if ((timeVars.minutes < 0) || (timeVars.minutes > 59))
{
isInputValid = false;
puts("The 'minutes' value is invalid.");
}
if ((timeVars.seconds < 0) || (timeVars.seconds > 59))
{
isInputValid = false;
puts("The 'seconds' value is invalid.");
}
if ((':' != charOne) || (':' != charTwo))
{
isInputValid = false;
puts("The 'colon' value is invalid.");
}
return isInputValid;
}
使用下面的SCANF语句可以获得输入,但是检查错误输入变得更复杂,并且需要在输入的中间按Enter键。p>
scanf("%d %c %d %c %d", &userInput.hours
, &firstColon
, &userInput.minutes
, &secondColon
, &userInput.seconds);
用fgets读取此格式的字符串HH:MM:SS
这里您可以检查长度,您可以检查这个字符串的0,1索引、3,4索引和6,7索引是否为数字
同时检查:借助strchr或检查字符串的2,5个索引
检查后,如果要将小时转换为值
hours=(str[0]-'0') * 10 +(str[1]-'0');
// use same for minutes and seconds
您也可以使用strtol或atoi进行转换,如果您决定使用strtol,则返回long int。您需要更改时、分、秒的类型。感谢您的回复!不过我有个问题。这条线到底是怎么工作的?str[0]-'0'*10+str[1]-'0';这将把字符串的hours部分转换为hours的int值。例如,如果输入12:32:30,则str[0]为“1”,而“1”-“0”将变为1。str[1]是'2','2'-'0'将变成2,最后str[0]-'0'*10+str[1]-'0';这将得到12个好的结果,“0”部分使其成为“减法”字符,这就是转换发生的地方?是的。确切地字符数字不是实际的整数,如果从中减去字符零,则将得到整数。48是“0”的ASCII值,57是“9”的ASCII值。减法字符意味着减法其ASCII值非常感谢您的帮助!