Thursday, May 23, 2013

ASCTOINT

ASCTOINT function:
(I didn't bother to add any boundaries on the input, but remember that an int has a practical max, and too high of a number will cause this function to return a REALLY weird number)

#include <iostream>
#include <cstdlib>
#define MAX 99
int asctoint(char*);
int pow(int,int);
using namespace std;
int main(){
    char ghost[MAX];
    int alter=0;
    int p=0;
    cin >> ghost;
    alter=asctoint(ghost);
    cout << alter;
    cin >> p;
    return 0;
}
int asctoint(char* a){
    int grab[MAX];
    int i=0,j=0;
    int num=0;
    int toast=0;
    for(i=0;a[i];i++){
        grab[i]=a[i]-48;
        num++;
    }
    for(j=0;num>0;j++,num--){                         
        toast+=pow(grab[j],num);
    }
    return toast;
}
int pow(int dig,int exp){
    int i=0;
    if(exp>1){
        for(i=0;i<exp-1;i++){
            dig*=10;
        }
    }
    return dig;
}

No comments:

Post a Comment