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);
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:
#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;
}