0

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;
        }
}
~                                                                                                              
~                                                                                                              
~                                                                                                              
~                                                                                                              
~                                                                                                              
~
4
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by kavithabhaskar
1

but what I have here completely makes sense..

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

3

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.

0

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;
                }

        }
}

~
~
~
~
~

Edited by Nick Evan: Added code-tags

0

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

Edited by kavithabhaskar: n/a

This question has already been answered. 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.