Y/N :最好使用char数组进行实现 ,不要用cin.get() getchar()或者scanf一切关于字符的函数实现
C++使用cin.getline() 不包含换行符
C使用fgets() 包含 换行符
1 /*************************************************************************
2 > File Name: YN.cpp
3 > Author: gaopeng
4 > Mail: gaopp_200217@163.com
5 > Created Time: Thu 05 May 2016 01:58:08 PM CST
6 ************************************************************************/
7
8 #include<iostream>
9 #include<string.h>
10 using namespace std;
11
12 int main(void)
13 {
14 char ch[20];
15 //cin >> ch;
16 cin.getline(ch,20);
17 int cot=0;
18 // cout<< strlen(ch) << endl;
19 while (1)
20 {
21 cot++;
22 // cout<< strlen(ch) << endl;
23 if (!strcmp(ch,"Y") || !strcmp(ch,"y" ) )
24 {
25 cout<<"true"<<endl;
26 break;
27 }
28 else if(!strcmp(ch,"N") || !strcmp(ch,"n" ))
29 {
30 cout<<"flase"<<endl;
31 break;
32 }
33 else if(cot >10)
34 {
35 cout<<"max is rech"<<endl;
36 break;
37 }
38 else
39 {
40 cout<<"unkown"<<endl;
41 cin.getline(ch,20);
42 continue;
43 }
44 }
45 return 0;
46 }
<endl;
<endl;
<endl;
<endl;
<endl;
<endl;
C:
8 #include
9 #include
10
11 int main(void)
12 {
13 char ch[20];
14 fgets(ch,20,stdin);
15 ch[strlen(ch)-1]='\0';
16 printf("%ld\n",strlen(ch));
17 while (1)
18 {
19 if (!strcmp(ch,"Y") || !strcmp(ch,"y" ) )
20 {
21 printf("%s\n","true");
22 break;
23 }
24 else if(!strcmp(ch,"N") || !strcmp(ch,"n" ))
25 {
26 printf("%s\n","flase");
27 break;
28
29 }
30 else
31 {
32 printf("%s\n","unkown");
33 fgets(ch,20,stdin);
34 ch[strlen(ch)-1]='\0';
35 continue;
36 }
37 }
38 return 0;
39 }
~
</endl;
</endl;
</endl;
</endl;
</endl;
</endl;
C++使用cin.getline() 不包含换行符
C使用fgets() 包含 换行符
1 /*************************************************************************
2 > File Name: YN.cpp
3 > Author: gaopeng
4 > Mail: gaopp_200217@163.com
5 > Created Time: Thu 05 May 2016 01:58:08 PM CST
6 ************************************************************************/
7
8 #include<iostream>
9 #include<string.h>
10 using namespace std;
11
12 int main(void)
13 {
14 char ch[20];
15 //cin >> ch;
16 cin.getline(ch,20);
17 int cot=0;
18 // cout<< strlen(ch) << endl;
19 while (1)
20 {
21 cot++;
22 // cout<< strlen(ch) << endl;
23 if (!strcmp(ch,"Y") || !strcmp(ch,"y" ) )
24 {
25 cout<<"true"<<endl;
26 break;
27 }
28 else if(!strcmp(ch,"N") || !strcmp(ch,"n" ))
29 {
30 cout<<"flase"<<endl;
31 break;
32 }
33 else if(cot >10)
34 {
35 cout<<"max is rech"<<endl;
36 break;
37 }
38 else
39 {
40 cout<<"unkown"<<endl;
41 cin.getline(ch,20);
42 continue;
43 }
44 }
45 return 0;
46 }
<endl;
<endl;
<endl;
<endl;
<endl;
<endl;
C:
8 #include
9 #include
10
11 int main(void)
12 {
13 char ch[20];
14 fgets(ch,20,stdin);
15 ch[strlen(ch)-1]='\0';
16 printf("%ld\n",strlen(ch));
17 while (1)
18 {
19 if (!strcmp(ch,"Y") || !strcmp(ch,"y" ) )
20 {
21 printf("%s\n","true");
22 break;
23 }
24 else if(!strcmp(ch,"N") || !strcmp(ch,"n" ))
25 {
26 printf("%s\n","flase");
27 break;
28
29 }
30 else
31 {
32 printf("%s\n","unkown");
33 fgets(ch,20,stdin);
34 ch[strlen(ch)-1]='\0';
35 continue;
36 }
37 }
38 return 0;
39 }
~
</endl;
</endl;
</endl;
</endl;
</endl;
</endl;