c++ - Counting the number of even digits in a number? -
i looking calculate numbers in given string c++. code is:
#include <iostream> #include <sstream> #include <string> using namespace std; int i(0), c(0), z(0), j(1), n1, x(0); string n; int main() { cout << "hello n world!" << endl; cin >> n; //the number while (true) { if (( n[i] >= '0' && n[i] <= '9')) { x++; } else break; i++; } //counts numbers cout << "the number has " << x << " digit(s)." << endl; istringstream convert(n); if ( !(convert >> n1) ) n1 = 0; //converts string int while (c < x) { if ( n1 % 2 == 0 ) { z++; }; n1 = n1 % 10; c++; }; //counts numbers (i can feel part wrong, don't know how) cout << "the number has" << z << " digit(s)." << endl; return 0; } but when use it, when enter 1022, 4 numbers , if enter 696969 gives 0 numbers. do? wrong code?
n1 = n1/10; not %
that should solve basic logic calculate number of numbers. there other errors in code. don't have c++ on machine - can't run it.
Comments
Post a Comment