我想问为什么我在执行程序时遇到运行时检查失败#2?
我是C编程的新手。
我试图制作一个控制台应用程序,在键入Y/N后可以选择一些选项,
但是,只要我到达所有选项的末尾,我都会收到该错误。
谁能告诉我如何解决?进行这种编程的正确方法是什么?
#define _CRT_SECURE_NO_WARNINGS // To allow Visual studio to use "scanf" function
#include <stdio.h> // Standard Input output . header
#include <Windows.h>
void codername() {
printf("Coder: Rong Yuan\n");
}
void projectname() {
printf("Project name: NPoly Learning\n");
}
void loadcurrentdate() {
SYSTEMTIME str_t;
GetSystemTime(&str_t);
printf("Date: %d . %d . %d \n"
, str_t.wDay, str_t.wMonth, str_t.wYear);
}
int main() {
char option;
int input;
int mincome, fmember, total;
printf("Do you like to see our option? Y/N \n");
scanf("%s", &option);
if (option == 'y' || option == 'Y') {
printf("1. Display Coder Detail\n");
printf("2. Display Project Name\n");
printf("3. Load Current Date\n");
printf("4. Calculator PCI\n");
printf("5. Exit\n");
scanf("%d", &input);
}
else
exit(1);
switch (input) {
case 1:
codername();
printf("Do you like to return to main?");
break;
case 2:
projectname();
break;
case 3:
loadcurrentdate();
break;
case 4:
printf("Enter your house monthly income: ");
scanf("%d", &mincome);
printf("Enter total family member: (INCLUDING YOURSELF) ");
scanf("%d", &fmember);
total = mincome / fmember;
printf("Total PCI: %d / %d = %d \n", mincome, fmember, total);
system("pause");
break;
case 5:
exit(0);
}
}
最佳答案
scanf("%s", &option);
是错误的,因为
option
是char
。因此,将%s
替换为%c
。 %s
应该用于字符串(字符数组),而%c
是用于字符的格式说明符。关于c - 接收运行时检查失败#2,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26489944/