Help problem with a program

Hello, I am starting programming and am on my second assignment.

The program is required to ask the user for an integer, then convert the integer into a string
Then the string should display each number (no duplicates) with it's position.
Like so:

7377683
3 : 0 5
6 : 2
7 : 3 4 6
8 : 1
7377683

Using the positions of each number (the last number has the position 0 ) the full number should be reconstructed as an integer.

Note: The integer should be cast as an unassigned long int.

I have the code to should the numbers with positions but i cannot get rid of the duplicates and enter their position next to the same numbers position. Also to reconstruct the integer at the end i would like some guidance as i am not sure.

I am new to C++ so you may need more information. Thank you in advance for your time.

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
  #include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
#include <algorithm>

using namespace std;

int main()
{
    long unsigned int num;
    int i;   

     cout<<"ENTER A 9 DIGIT NUMBER : " <<endl;  //ask user to enter integer
     cin>>num;

    if (num > 999999999)

    {
        cout <<"ERROR: Enter a 9 digit number" <<endl;
        cin.clear();

        return 0;
    }



    string result;
    stringstream out;               //convert int to string
    out << num;
    result = out.str();

   reverse(result.begin(), result.end());     //reverse the string

    for (i= 0; i < result.length(); i++)

    {
            cout<< result.at(i) << " : " << i <<endl;

    }
   return 0;
}
I still cannot manage to figure out why this is not working. Can someone please help me :)
What is not working?

The program works for me as long as I enter 9 (or less) digit number.
To better this program change
1
2
3
4
5
6
7
    if (num > 999999999)
    {
        cout <<"ERROR: Enter a 9 digit number" <<endl;
        cin.clear();

        return 0;
    }


to

1
2
3
4
5
6
7
    while (num > 999999999)
    {
        cout <<"ERROR: Enter a 9 digit number" <<endl;
        cin.clear();

        cin>>num;
    }
The problem is if there are two of the same number the program will not give two positions to the number like this:

7377683
3 : 0 5
6 : 2
7 : 3 4 6
8 : 1
7377683

Instead i will get this:

7377683
3:0
8:1
6:2
7:3
7:4
3:5
7:6
7377683
I cannot seem to get this to work and have spend multiple hours changing the code to loop correctly but am really having no luck. If someone could please give it a glance i would be most appreciative.
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
#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>

int main()
{
    // long unsigned int num;
    unsigned long long num;
    // int i;

    std::cout << "ENTER A 9 DIGIT NUMBER : " ;  //ask user to enter integer
    std::cin >> num;

    std::cout << "\nnumber: " << num << '\n' ;
        
    std::ostringstream out ;
    out << num ;
    std::string result = out.str() ;
    std::reverse( result.begin(), result.end() ) ;

    for( int i = 0 ; i < 10 ; ++i )
    {
        const char digit = i + '0' ;

        if( std::count( result.begin(), result.end(), digit ) > 0 )
        {
            std::cout << i << ": " ;

            for( std::size_t pos = 0 ; pos < result.size() ; ++pos )
                if( result[pos] == digit  ) std::cout << pos << ' ' ;

            std::cout << '\n' ;
        }
    }
}

http://coliru.stacked-crooked.com/a/3f97e3b8dc253754
1
2
3
4
5
6
    for (i= 0; i < result.length(); i++)

    {
            cout<< result.at(i) << " : " << i <<endl;

    }
This just outputs the digit at position i then outputs i. If you want to do it the way desired you would first have to find all the unique values then find all the positions of that unique value.
Yes, that is exactly the problem giblit, I understand how the program should go but having problem actually coding it in the program. I am not still new to coding but willing to learn :)

Thank you JLBorges for the response I will spend an hour or two understanding the code. I will mark this thread as solved when i have understood :D
I have understood the code and understand why my code was not working. I need to reconstruct the initial integer with the positions that have been listed. Would someone be able to give me a hint so that I know i am going the right ways

Thanks
HINT: write a nested loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
for( int digit = 0 ; digit < 10 ; ++digit ) // for each digit 0, 1, 2, 3, ... 9
{
      for( std::size_t pos = 0 ; pos < result.size() ; ++pos ) // for each character in the string
      {
           if( result[pos] == digit + '0'  ) // is the character this particular digit?
           {
                  // ...
           }
           // .... 
      }
       // ....
   }
}
Thank you for the hint i am going to complete the program. This topic should be solved
Thanks again :)
Topic archived. No new replies allowed.