0

user enters up to 20 charachters that are of digits 1-9
so enter a number: 109832032

i need to get each charachter and convert it to an int.
so '1''2'''3''4'=1234
how do i convert it, im using arrays and a ton of if statements, but its not working
check my code
[

#include<iostream>
using namespace std;

int main(){
    
const int max=20;    
char let[20];
char numb[10]={'0','1','2','3','4','5','6','7','8','9'};
int c[20]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int count=0;
for(int x=0;x<max;x++){

cin.get(let[x]);

if(let[x]=='\n'){
break;
}

if(let[x]=='0'){
c[x]=0;
}
if(let[x]=='1'){
c[x]=1;
}
if(let[x]=='2'){
c[x]=2;
}
if(let[x]=='3'){
c[x]=3;
}
if(let[x]=='4'){
c[x]=4;
}
if(let[x]=='5'){
c[x]=5;
}
if(let[x]=='6'){
c[x]=6;
}
if(let[x]=='7'){
c[x]=7;
}
if(let[x]=='8'){
c[x]=8;
}
if(let[x]=='9'){
c[x]=9;
}
count+=1;
}
    
int sum=0;


for(int g=1;g<count;g++){
sum=c[0];
c[0]*=10;
sum=c[0]+c[g];
c[0]=sum;
cout<<sum<<endl;

}
    

    
cout<<endl; 
system("pause");   
}

]

Edited by WaltP: Added CODE Tags

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by WaltP
0

Get rid of the "ton of IF statements"...

Each character has a value ('0'=48; '1'=49...)
To convert a digit character to an int, subtract '0'. num = chr - '0'; Do this for each character entered.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.