我是新來的C++,並提出了這個問題,我真的不知道如何進行。C++搜索號碼的數字
我會得到一個從0-999的數字,我試圖確定數字中是否出現了數字1,4或7。
我試過使用find_first_of()
,但我有很多錯誤。
這裏是我到目前爲止的代碼:
double ctemp;
cin >> ctemp;
if(ctemp.find_first_of(1)==-1 && ctemp.find_first_of(4)==-1 && ctemp.find_first_of(7)==-1)
{
cout << "Found digits 1, 4, 7.\n";
}
然而,當我運行此代碼,我得到以下錯誤:
11:18: error: request for member 'find_first_of' in 'ctemp', which is of non-class type 'double'
我試圖改變變量類型爲字符串,但它提出了更多的錯誤和不足。有沒有人有任何方法讓搜索數字更容易?
您需要將您的號碼加載到'std :: string'或將您的double加載到它。 (但如果它是整數,則使用int而不是double) –
字符串是一串字符。數字不是一個數字序列。如果你讀入一個字符串,你需要'find_first_of('1')'。 – molbdnilo
閱讀錯誤信息如何?這一點特別清楚。 – Cthulhu