I started C++ in Uni. Now we are using loops. I have practical assignment: Task: Input: integer number. Remove all digits that can be found on the left from most right digit 1.
For example:
- input: 314195
- result: 95
Or:
- input 45131005
- result: 005
For this task we can not use arrays or strings. However we still can use math functions. Also there must obligatory be while/do while condition.
I have already written the code that finds values before the first digit 1 using % and /. However, as a result it doesn't work properly and when I enter something like 431004 - it will just show 4, not the 004. Here is my code:
#include <iostream>
using namespace std;
int main()
{
int number;
cout << endl << "Enter integer number: ";
cin >> number;
int result;
int buffer = 0;
int divider = 10;
int check = 1;
do {
result = buffer;
buffer = number % divider;
divider = divider * 10;
check *= 10;
} while ((buffer / (check / 10)) != 1);
cout << endl << "Result = " << result;
return 0;
}
004and4are ths ame number4.If you need for example to output
4as004then use manupulatorsstd::setfillandstd::setwdeclared in header<iomanip>.Also you should declare the variable
numberas having an unsigned integer type instead of the typeint. Otherwise you need to take into account the sign of a number.Here is a demonstration program that shows how to get the expected result using the manipulators.
The program output might look like