set<int> numbers;
char userLetter;
int userNumber;
cout << "Please enter a letter: ";
cin >> userLetter;
while(userLetter == 'X')
{
cout << "Please enter a number: ";
cin >> userNumber;
if (numbers.find(userNumber) != numbers.begin())
{
cout << "Invalid!";
continue;
}
else
{
numbers.insert(userNumber);
}
if(userNumber == 2)
cout << "Yes :)";
else
cout << "No :(";
cout << "Please enter a letter: ";
cin >> userLetter;
}
這是當我運行該程序會發生什麼:C++代碼不工作...爲什麼?
Please enter a letter: X
Please enter a number: 2
Yes :)
Please enter a letter: X
Please enter a number: 2
Yes :)
Please enter a letter: X
Please enter a number: 2
Yes :)
...
當然,「X」和「2」的東西,我進入。當我輸入第二個2時,不應該輸出程序「無效」?
任何幫助?
爲什麼你標記到Java,C#,Python的和matlab? – bharath
請不要用5種不同的編程語言來標記您的問題,其中一種(您實際使用的語言)將執行此操作。 –
http://www.cplusplus.com/reference/stl/set/begin/ – sarnold