pat乙级1067

1、用cin输入数据后,再用getline 输入,还是会输入cin已经输入的数据,即cin和getline互相独立。

2、题目中没有说尝试的密码不包含空格,因此不能用cin,而用getline。

 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 bool cmp(string a, string b)
 5 {
 6     if (a.size() != b.size())
 7         return false;
 8     else
 9     {
10         for (int i = 0; i < a.size(); i++)
11         {
12             if (a[i] != b[i])
13                 return false;
14         }
15     }
16     return true;
17 }
18 int main()
19 {
20     string answer, s;
21     int n;
22     cin >> answer >> n;
23     int count = 0;
24     getline(cin, s);
25     while (true)
26     {
27         getline(cin, s);
28         if (s == "#") break;
29         count++;
30         bool equal = cmp(s, answer);
31         if (equal)
32         {
33             cout << "Welcome in" << endl;
34             break;
35         }
36         else
37             cout << "Wrong password: " << s << endl;
38         if (count == n)
39         {
40             cout << "Account locked" << endl;
41             break;
42         }
43     }
44     return 0;
45 }
原文地址:https://www.cnblogs.com/lxc1910/p/8624686.html