about atoi

I have the following code:

#include "stdafx.h"
#include <iostream>
using namespace std;

void main()
{
int x = 0;
char letter[256];
cout<<"Push a key: ";
cin>>letter;
if (atoi(letter)==x)
{
cout<<"I push a letter"<<endl;
}
else
{
cout<<"I push a number"<<endl;
}
cout<<endl;
}

and all I want is that this code to recognize 0 as number (integer), not as a letter (I need to separate all the numbers from all the letters).Any idea is much appreciated.Thanks!
If you don't use atoi(), you can:

#include <iostream>
using namespace std;

int main()
{
char ch;

cout<<"Push a key: ";
cin>>ch;
if (ch=='0'||ch=='1'||ch=='2'||ch=='3'||ch=='4'||ch=='5'||ch=='6'||ch=='7'||ch=='8'||ch=='9')
{
cout<<ch<<endl;
cout<<"I push a number"<<endl;
}

else
{
cout<<ch<<endl;
cout<<"I push a letter"<<endl;
}

return 0;
}
Last edited on
@nrc1982
if(ch>='0' && ch<='9')
is better

or you can just use isdigit() (not sure if I got the name right) declared under cctype
@nrc1982
if(ch>='0' && ch<='9')
is better

or you can just use isdigit() (not sure if I got the name right) declared under cctype


Yup, this one is better.
misurex, i think you have your if and else case switched... :)
Topic archived. No new replies allowed.