problem using isalpha()

I want to write a program which take three float input from user and break if user enters an alphabet.kindly help me.i m doing it somthing like this but it isn't working

cout<<"Input 1st number:";
cin>>a;
if(!isalpha(a))
{cout<<"Input 2nd number:";
cin>>b;
if(!isalpha(b))
{cout<<"Input 3rd number:";
cin>>c;}
else
cout<<"invalid no.";



}
else
cout<<"invalid no.";


First of all, please place your code in the code blocks (see the Format: box to the right.)

Second of all: What errors are you getting?
I want to write a program which take three float input from user
I presume a, b, and c are floats? isalpha works only for characters.

break if user enters an alphabet
If youser will enter an alphabetic character instead of floating point number, input will already breaks. You will have to detect this and recover from it, or all other input will fail too. read this: http://www.parashift.com/c++-faq/istream-and-ignore.html
if a,b,c are declared floats then all you need to do is check there wasn't an error during input.


replace !isalpha(a) with cin.good(), also when i format your code it becomes obvious that c isn't checked at all. layout is important, it will help you see problems.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
cout<<"Input 1st number:";
cin>>a;
if(cin.good())
{ 
   cout<<"Input 2nd number:";
   cin>>b;
   if (cin.good())
   {
      cout<<"Input 3rd number:";
      cin>>c;
   }
   else
       cout<<"invalid no.";
}
else
    cout<<"invalid no.";

Topic archived. No new replies allowed.