我想问为什么我得到运行时检查失败#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);
}
}
答案 0 :(得分:3)
scanf("%s", &option);
错误,因为option
是char
。因此,将%s
替换为%c
。%s
应该用于字符串(字符数组),%c
是用于字符的格式说明符。