PDA

View Full Version : C++ problems



kitaeshi
August 23rd, 2005, 10:56 AM
i not sure is it appropriate to post it here there seen to be not a single thread on C++ and i urgently need to solve this problem.

its about like this, in c++ how do u compare a numberic and a non-numberic?

My mentor gave us a test on C++ and one of the question is to test the characters the user input.

the code will loop and ask for input for 5 character(a-z & 0-9), and you have to tell how many numberic and how many non-numberic is there in the loop.

below is my source codes

thxs

#include <iostream>
using namespace std;
int main(void){
char letter[5];
int num=0, non=0;
int i;
for(i=0 ; i<5 ; i++){
cout << "Enter " << i+1 << " char: ";
cin >> letter[i];
}
for(i=0 ; i<5 ; i++){
if(letter[i] > 0){
num++;
}else{
non++;
}
}
i=0;
cout << "You entered ";
while(i<5){
cout << letter[i];
i++;
}
cout <<endl<< "No. of Numeric: " << num << endl;
cout << "No. of Non-numeric: " << non << endl;
return 0;
}

bandinopla
August 23rd, 2005, 02:21 PM
And... whats the problem?
mabie you should do something like: if (letter[i]*1!=1) is not numeric
I don't know c++ very well...

λ
August 23rd, 2005, 02:25 PM
if the char is numeric it'll be between 48 and 57:


char c = '0';
int is_numeric = 0;

if (c >= 48 && c <= 57) {
is_numeric = 1;
}

kitaeshi
August 24th, 2005, 09:41 AM
THXSSSSSSSSSSSSSSS
It works!!!!!

my mentor said have to use string to compare. but i can't get it to work.. haiz..