trying to remove first digit of any number

hi im trying to remove the first digit so if the user enters 12345 it should output 2345 the code i have works only for removing the last digit how would i go about removing the first one?

#include <iostream>
using namespace std;
int removeFirst(int n);
int main(){
int n, m;
cout << "enter number" << endl;
cin >> n;
m = removeFirst(n);
cout << m << endl;
system("pause");
}

int removeFirst(int n){
if(n>10) return n/10;
if(n<10)return 0;
return removeFirst(n/10);




}
meh ;D
Last edited on
1
2
3
4
std::string s;
std::cin >> s;
s.erase(s.begin());
std::cout << s;


use std::to_string or boost::lexical_cast for converting the string to a number. If you want to do this at the int level; use modulo operations until the last digit is removed, then re-apply the previously fetched digits:

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
#include <iostream>
#include <cmath>

int removeFirst(int n)
{
    int tmp(0);
    for (int i(0);; ++i)
    {
        int m = n % 10;
        n /= 10;
        if (n != 0)
        {
            tmp += std::pow(10, i) * m;
        }
        else
        {
            break;
        }
    }
    return tmp;
}


int main(){
    int s;
    std::cin >> s;
    s = removeFirst(s);
    std::cout << s;
}

You just need some math

1
2
3
4
5
6
7
8
9
10
#include <cmath>
#include <string>

void remove_first(std::string &num) {
    num = num.substr(1);
}

void remove_first(int &num) {
    num %= static_cast<int>(pow(10, static_cast<size_t>(log10(num))));
}
Last edited on
1
2
3
4
5
unsigned int remove_first_digit( unsigned int n )
{
    if( n < 10 ) return 0 ;
    else return n%10 + remove_first_digit(n/10) * 10 ;
}
Topic archived. No new replies allowed.