我尝试使用 scanf_s 从用户那里获取日期和月份的输入(带有 visual studio 的编译器总是要求您使用 scanf_s,这就是我使用它的原因)。它允许我输入日期,但是当我输入月份(作为字符串)时,它退出代码并且不执行其余部分。如果我首先放置字符串的 scanf_s,它会在我输入后退出代码。我将如何解决这个问题?
#include "iostream."
#include "string.h"
#include "stdlib.h"
void main()
{
char month[10];
int date;
printf("enter date\n");
scanf_s("%d", &date);
printf("enter month\n");
scanf_s("%s", month);
printf("Month: %s Date %d\n", month, date);
}
最佳答案
我只是禁用了 scanf_s 的警告,这让我可以改用 scanf。我还尝试使用 sizeof 语句(例如 scanf_s("%s", string1, sizeof string1))这似乎不需要我禁用警告就可以工作。
感谢大家的帮助,并留意我遇到的更多问题,因为问题的来源还有很多!!
关于c++ - 使用 scanf_s 获取字符串值的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58924956/