gpt4 book ai didi

c - 我在 main 中正确调用我的函数吗?

转载 作者:行者123 更新时间:2023-11-30 16:53:49 25 4
gpt4 key购买 nike

我被分配了一项任务,将用户输入作为 MM/DD/YY ,在我得到输入后,如果日期有效或无效,它应该打印出来。我只允许使用 %s 来获取用户输入,而不是 %d,否则会容易得多。

我是否正确调用了 getInput 函数?如果用户输入有效或无效,程序不会打印出来,我不明白为什么。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


//Declares function
void getInput(char *userInput1, char *userInput2, char *userInput3);
//Declares variables.
char userInput1, userInput2, userInput3;


int main(void) {
char userInput[100];
printf("Enter a date inthis format : MM/DD/YY :");
scanf("%s", &userInput);
char *userInput1 = strtok(userInput, "/" );
char *userInput2 = strtok(NULL, "/");
char *userInput3 = strtok(NULL, "z ");
getInput(userInput1, userInput2, userInput3);
system("pause");
return 0;

}

void getInput(char *userInput1,char *userInput2,char *userInput3)
{
// If user inputs a valid date this will run.
if (0 > userInput1<12, 0>userInput2<31, 0>userInput3 < 99) {
// If user month input is this...
switch (*userInput1){
//if User month input is 02 this will run
case 02:
//If user year is a leap year this will run
if (*userInput3 % 4 == 0) {
//If user day is inbetween 0-29 it will say the date is valid.
if (0 > *userInput2 > 29) {
printf("You entered a valid date, %s/%s/%s", *userInput1, *userInput2, *userInput3);
}
// If the day of the February isn't 0-29 it will print it is invalid.
else
printf("Invalid date.");
}
case 04:
//Their are only 30 days in April, if user Inputs 31 it is invalid.
if (*userInput2 == 31) {
printf("Invalid date.");
}
//Every other date should be valid because the main if else statement filters it all out.
else {
("You entered a valid date, %s/%s/%s", *userInput1, *userInput2, *userInput3);
}
case 06:
if (*userInput2 == 31) {
printf("Invalid date.");
}
//Every other date should be valid because the main if else statement filters it all out.
else {
("You entered a valid date, %s/%s/%s", *userInput1, *userInput2, *userInput3);
}
case 11:
if (*userInput2 == 31) {
printf("Invalid date.");
}
//Every other date should be valid because the main if else statement filters it all out.
else {
("You entered a valid date, %s/%s/%s", *userInput1, *userInput2, *userInput3);
}
break;
}
}
//If the user enters a date that isn't valid , Invalid date prints.
else {
printf("Invalid date.");
}
return 0;

}

最佳答案

将该行读取为字符串,然后使用 strtok 函数对其进行标记。

关于c - 我在 main 中正确调用我的函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40708160/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com