split numbers into digits using vectors

I want to split number entered into digits using vectors (correct me if I'm calling it wrong). It doesn't work and I don't really have any idea why. It compiles well, but after I enter a number, it shows nothing for a while and exterminates.

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
#include <iostream>
#include <vector>
using namespace std;
int main()
{
    int x; int digit; int t=0;

    vector<int> digitstack;
    cin >> x;
    while(x>=0)
    {
        digit = x % 10;
        x = x / 10;
        digitstack.push_back(digit);
    }

    sort(digitstack.begin(), digitstack.end());

        while(t!=digitstack.size())
    {
        cout << digitstack[t] << " "; t++;

    }
}
I dont think this actually splits a number into its digits.

Say x=15
digit=x%10=15%10=5
x=x/10=15/10=1.5 and since the variable type is int, it is rounded up to 2

So you eventually see that it is not dojng what you expect.

I stand for correction. Thanks,
Aceix.
@aceix
it is rounded up to 2


I'm pretty sure it rounds down

Anyways, I found a problem - it seems that the problem was while>=0, as it actually reaches zero because of rounding down.
Now as I can see the program sorts numbers in the container from lowest to highest which I don't really need. How do I sort numbers in container so they are shown normally? I need to reverse digits in container.

EDIT:

found out that I just have to use

reverse(digitstack.begin(), digitstack.end());

instead of

sort(digitstack.begin(), digitstack.end());
Last edited on
Aceix wrote:
x=x/10=15/10=1.5 and since the variable type is int, it is rounded up to 2
actually the result is 1 because this is integer division. 10 goes into 15 once.

what is this? sort(digitstack.begin(), digitstack.end());
Last edited on
@yanson

AFAIK that function sorts values in the container from highest to lowest.
Yeah thanks.

Aceix.
uzferry wrote:
AFAIK that function sorts values in the container from highest to lowest.
Yes but it won't even compile for me because there is not function prototype. So my compiler dosn't know what sort means. and the function definition should be outside the main function.
yanson, you have to include <vector> library
I did I think its <algorithm>
Last edited on
not sure this is what you are going for.

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
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
int main()
{
    string x; int digit; int t=0;
	string temp;

    vector<int> digitstack;
    getline(cin, x);
    
	for(int i = 0; i < x.length(); i++)
    {
        temp = x.at(i);
		digit = stoi(temp);
		digitstack.push_back(digit);
    }

    sort(digitstack.begin(), digitstack.end());

        while(t!=digitstack.size())
    {
        cout << digitstack[t] << " "; t++;

    }

	cin.ignore();
	return 0;
}
*EDIT*
Sorry, didn't notice that the problem was already solved.
Last edited on
@Yanson

My compiler won't compile this code, what exactly is stoi?
stoi is a function from the C++11 standard that converts a string to an integer.

http://www.cplusplus.com/reference/string/stoi/
Topic archived. No new replies allowed.