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.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142`` `````` #include #include #include #include #include using namespace std; int main() { long unsigned int num; int i; cout<<"ENTER A 9 DIGIT NUMBER : " <>num; if (num > 999999999) { cout <<"ERROR: Enter a 9 digit number" <
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
 ``1234567`` `````` if (num > 999999999) { cout <<"ERROR: Enter a 9 digit number" <

to

 ``1234567`` `````` while (num > 999999999) { cout <<"ERROR: Enter a 9 digit number" <>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

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.
 ``123456789101112131415161718192021222324252627282930313233343536`` ``````#include #include #include #include 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
 ``123456`` `````` for (i= 0; i < result.length(); i++) { cout<< result.at(i) << " : " << i <
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.

 ``12345678910111213`` ``````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.