新人求解
只是想写一个输入字符Y N来决定switch选项的小程序;不知怎么修改,求指导
C 指针 ascii switch
#include <stdio.h>
#include <Windows.h>
#include <string.h>
#include <stdlib.h>
void main()
{
char str1, str2 = 'y', str3 = 'n'; //定义变量和指针
char *p1, *p2, *p3;
int b = NULL;
printf("Do you want to close the computer in five minutes?(y/n):\n");
loop: scanf("%c",&str1); //输入以及跳转标示符;
p1 = &str1; //指针赋值;
p2 = &str2;
p3 = &str3;
if (strcmp(p1,p2) == 0) //比较字符串的ASCII大小
{
b = 1;
} else if (strcmp(p3,p1) == 0)
{
b = 2;
} else {
printf("Error press,please input again:\n");
b = 3;
}
switch(b) //运行选项;
{
case 1: system("shutdown -s -t 300"); break;
case 2: system("exit"); break;
default: goto loop; break;
}
system("pause");
}
19 void main()
20 {
21 char str1, str2 = 'y', str3 = 'n'; //定义变量和指针
22 char *p1 = NULL, *p2 = NULL, *p3 = NULL;
23 int b = NULL;
24
25 printf("Do you want to close the computer in five minutes?(y/n):\n");
26
27 loop:
28 scanf("%c",&str1); //输入以及跳转标示符;
29
30 p1 = &str1; //指针赋值;
31 p2 = &str2;
32 p3 = &str3;
33
34
35 // if (strcmp(p1,p2) == 0) //比较字符串的ASCII大小
36 if (strcasecmp(p1,p2) == 0) //比较字符串的ASCII大小 大小不敏感
37 {
38 b = 1; // 输入 Y or y
39 }
40 // else if (strcmp(p3,p1) == 0)
41 else if (strcasecmp(p1,p3) == 0)
42 {
43 b = 2; // 输入 N or n
44 } else {
45 printf("Error press,please input again:\n");
46 b = 3; //其他,重新输入
47 }
48
49 switch(b) //运行选项;
50 {
51 case 1:
52 system("shutdown -s -t 300");
53 break;
54 case 2:
55 system("exit");
56 break;
57 default:
58 goto loop;
59 break;
60 }
61
62 system("pause");
63 }
#include <stdio.h>
#include <Windows.h>
#include <string.h>
#include <stdlib.h>
void main()
{
char str1, str2 = 'y', str3 = 'n'; //定义变量和指针
// char *p1, *p2, *p3;
// int b = NULL;
int b = 0; //查一下NULL一般是给什么类型的变量赋值的?
printf("Do you want to close the computer in five minutes?(y/n):\n");
loop:
scanf("%c",&str1); //输入以及跳转标示符;
// p1 = &str1; //指针赋值;
// p2 = &str2;
// p3 = &str3;
//
/*if (strcmp(p1,p2) == 0) //比较字符串的ASCII大小*/
if (tolower(str1) == str2)
{
b = 1;
} /*else if (strcmp(p3,p1) == 0)*/
else if (tolower(str1) == str3)
{
b = 2;
}
else
{
printf("Error press,please input again:\n");
b = 3;
}
switch(b) //运行选项;
{
case 1:
system("shutdown -s -t 300");
break;
case 2:
system("exit");
break;
default:
goto loop;
}
system("pause");
}
void main()
{
char str1, str2 = 'y', str3 = 'n'; //定义变量和指针
//char *p1 = NULL, *p2 = NULL, *p3 = NULL;
int b = 0;
printf("Do you want to close the computer in five minutes?(y/n):\n");
loop:
scanf(" %c",&str1); //输入以及跳转标示符;
/****
p1 = &str1; //指针赋值;
p2 = &str2;
p3 = &str3;
***/
printf("str1:%c\n",str1);
// if (strcmp(p1,p2) == 0) //比较字符串的ASCII大小
if (str1 == 'y') //比较字符串的ASCII大小 大小不敏感
{
b = 1; // 输入 Y or y
}
// else if (strcmp(p3,p1) == 0)
else if (str1 == 'n')
{
b = 2; // 输入 N or n
} else {
printf("Error press,please input again:\n");
b = 3; //其他,重新输入
}
switch(b) //运行选项;
{
printf("%d\n",b);
case 1:
system("shutdown -s -t 300");
break;
case 2:
system("exit");
break;
default:
goto loop;
break;
}
system("pause");
}