lundi 5 janvier 2015

I'm trying to use stoi(), but the compiler is trying to give me replacements and won't compile

I'm just practicing my C++ through HackerRanks, and I'm doing this problem:



Problem Statement


You are given an integer N. Find the digits in this number that exactly divide N(division that leaves 0 as remainder) and display their count. For N=24, there are 2 digits − 2 & 4. Both of these digits exactly divide 24. So our answer is 2.


Note


If the same number is repeated twice at different positions, it should be counted twice, e.g., For N=122, 2 divides 122 exactly and occurs at ones' and tens' position. So for this case, our answer is 3. Division by 0 is undefined. Input Format


The first line contains T (number of test cases) followed by T lines (each containing an integer N).


Constraints 1≤T≤15 0



This is my program:



1 #include <cmath>
2 #include <string>
3 #include <iostream>
4 #include <cstdlib>
5 using namespace std;
6 void checkDigits(string);
7 int main (){
8 string val;
9 int count = 0, i;
10
11 cin >> i;
12 for (int j = 1; j <= i; j++){
13 cin >> val;
14 checkDigits(val);
15 }
16 return 0;
17 }
18
19 void checkDigits(string s){
20 int len, count = 0;
21 int full, digit;
22
23 len = s.length();
24 for(int i = 0; i < len; i++){
25 full = stoi(s);
26 digit = stoi(s[i]);
27 if(full % digit == 0)
28 count++;
29 }
30 cout << count;
31 }


What would cause my compiler to give me this error?



hackerrank2.cpp: In function ‘void checkDigits(std::string)’: hackerrank2.cpp:26:20: error: call of overloaded ‘stoi(char&)’ is ambiguous digit = stoi(s[i]); ^ hackerrank2.cpp:26:20: note: candidates are: In file included from /usr/include/c++/4.8/string:52:0, from hackerrank2.cpp:2: /usr/include/c++/4.8/bits/basic_string.h:2823:3: note: int std::stoi(const string&, std::size_t*, int) stoi(const string& __str, size_t* __idx = 0, int __base = 10) ^ /usr/include/c++/4.8/bits/basic_string.h:2823:3: note: no known conversion for argument 1 from ‘char’ to ‘const string& {aka const std::basic_string&}’ /usr/include/c++/4.8/bits/basic_string.h:2926:3: note: int std::stoi(const wstring&, std::size_t*, int)

stoi(const wstring& __str, size_t* __idx = 0, int __base = 10) ^ /usr/include/c++/4.8/bits/basic_string.h:2926:3: note: no known conversion for argument 1 from ‘char’ to ‘const wstring& {aka const std::basic_string&}’



Aucun commentaire:

Enregistrer un commentaire