request for member '...' in '...', which is of non-class type 'int'
Aug 3, 2016 at 10:30pm UTC
i want to be able to get ANY numbers
ex: i want third number of: 99169876393 (1)
i want 6th number of: 841433 (6)
Convert the number to an array of digits?
Aug 4, 2016 at 7:12am UTC
oh god, no
i want to be able to get any number of a string of numbers
1 2 3 4 5 6
int number;
int lenght;
cin>>number; //99599
cin>>lenght //3
//...
cout<<result<<endl; //5
im bad at explaination, sorry
Aug 4, 2016 at 7:31am UTC
Convert the number to an array of digits?
Yes, you place the digits, as they come off each iteration of my loop, and put them into an array of int's. That's one way.
You can modify the code and add each digit to a string or c-style string array of char values.
You can count digits and stop at the one you want.
Don't forget the digits come off in reverse order.
(I know this sounds boring and I know VS takes a while to load but I would (and did) dump codeblocks mingw and all the rest - it's all badly supported crap - it's very disappointing but life is too short to waste on their junk.)
Aug 4, 2016 at 8:41am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#include <iostream>
size_t no_of_digits( int );
int main()
{
const int number = 9123456;
int digit = 0;
int temp = 0;
int count = 0;
size_t SIZE = no_of_digits(number);
std::cout << " No. of digits: " << SIZE << '\n' ;
std::cout << " One way: " << std::to_string(number).at(2) << '\n' ; // <--
count = 0;
temp = number;
while (temp > 0 && count < SIZE - 2) // <--
{
digit = temp % 10;
temp /= 10;
count++;
}
std::cout << " Another way: " << digit << '\n' ;
std::string numStr = "" ;
temp = number;
while (temp > 0)
{
numStr += '0' + temp % 10;
temp /= 10;
}
std::cout << "Yet another way: " << numStr.at( (SIZE - 1) - 2) << '\n' ; // <--
return 0;
}
size_t no_of_digits(int aNumber)
{
size_t size = 0;
while (aNumber != 0)
{
aNumber /= 10;
size++;
}
return size;
}
Last edited on Aug 4, 2016 at 10:27am UTC
Aug 4, 2016 at 12:21pm UTC
The 2nd way is perfect for me, thanks a LOT.
Sorry if i was 'stressful'.
There was a problem with it but i fixed it :)
Thanks all for trying to fix my problem!
Aug 4, 2016 at 12:51pm UTC
:)
Topic archived. No new replies allowed.