Hello Folks: I had previously posted this problem and someone on this forum suggested that i used s == 'a' || s == 'A' .... but what I have here completely makes sense..

Can you please take a look at it ?


#include<iostream>
#include<string>
using namespace std;

int main() {

        string s;
        cin>>s;

        for(int i=0;i<s.length();i++) {
                cout<<"length is "<<s.length()<<endl;
            if (isalpha(s[i]))
                if (s[i] == 'a'||'A'||'e'||'E'||'i'||'I'||'o'||'O'||'u'||'U')
                        cout<<s[i]<<" is a vowel"<<endl;
                else
                        cout<<s[i]<<" is a consonant"<<endl;

                 if (isdigit(s[i]))
                        cout<<s[i]<<" is a digit"<<endl;
                else
                        cout<<s[i]<<" is a special character"<<endl;
        }
}
~                                                                                                              
~                                                                                                              
~                                                                                                              
~                                                                                                              
~                                                                                                              
~

Recommended Answers

All 5 Replies

but what I have here completely makes sense..

To you maybe, but it doesn't work for the compiler. The other poster was correct.

The problem that you have is understanding how the ordering works.
(It is like the maths problems of multiply before addition. e.g 6*5+3 is 33 not 48 )

You have written

if (s[i] == 'a'||'A'||'e'||'E'||'i'||'I'||'o'||'O'||'u'||'U')

which is the same as

if ((s[i]=='a') || true || true || true)

Since each character ('A', 'O' ... ) is not zero, they each evaluate to true.
Thus it is very very unlikely to do what you think it does.

Thanks Folks.. It helped me understand better with the example of ||true||true.. thanks a lot and the correct code is

#include<iostream>
#include<string>
using namespace std;

int main() {

        string s;
        int i;
        cin>>s;

        for(int i=0;i<s.length();i++) {
        //      cout<<"length is "<<s.length()<<endl;


            if (isalpha(s[i])) {
                if (s[i] == 'a'|| s[i] == 'A'|| s[i] == 'e'|| s[i]=='E'||s[i]=='i'||s[i]=='I'||s[i]=='o'||s[i]=='O'||s[i]=='u'||s[i]=='U')
                        cout<<s[i]<<" is a vowel"<<endl;
                else
                        cout<<s[i]<<" is a consonant"<<endl;
            }

                else  if (isdigit(s[i])) {
                        cout<<s[i]<<" is a digit"<<endl;

                }
                else {
                        cout<<s[i]<<"is a special character"<<endl;
                }

        }
}

~
~
~
~
~

Salem.. thanks to you too.. though i understood wht u said in the previous thread.. i was not sure then.. now when I saw ur thread and related it to the example given by the other poster.. with the example ||true||true.. that also helped.. I just did not notice it earlier


> if (s == 'a'||'A'||'e'||'E'||'i'||'I'||'o'||'O'||'u'||'U')
I explained why this was wrong in your previous thread :@
http://www.daniweb.com/forums/thread262721.html

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.