-5

This is my code for a program that is supposed to prompt the user to provide a single letter, and then reports whether the letter is a vowel or consonant:

#include <iostream>
using namespace std;

int main() {
    cout << "input a single letter";
    int var; 
    cin >> var;
    int vowel = 'a','e','i','o','u';
    int consonant = 'b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','y','z'
    if (var == vowel) {
        cout << "vowel";
    } else if (var == consonant) {
        cout << "consonant";
    } else if (var != vowel && var != consonant) {
        cout << "Error";
    }
    return 0;
}

I am very very new at C++, and am trying to learn why I'm getting an error message.

Bill Hileman
  • 2,798
  • 2
  • 17
  • 24
  • 3
    Take a look at [`std::vector`](https://en.cppreference.com/w/cpp/container/vector). `int vowel = 'a','e','i','o','u';` does not do what you think it does. – scohe001 Sep 27 '18 at 18:28
  • 1
    There are a lot of things wrong with that code and I don't think there's a more helpful answer than just saying that you need to learn C++. For example, since `var` and `vowel` are both integers, `var == vowel` checks if two integers have the same value. That's not what you wanted to do there at all. In C++, the comma operator does something specific that's not what you wanted. So `'a','e','i'...` is not doing what you want. – David Schwartz Sep 27 '18 at 18:29
  • Everyone is downvoting instead of offering suggestions to a clearly new programmer. Your first problem is 'int vowel = 'a', 'e'...' is not what you want. 'vowel' either needs to be an array of values or some container. – spartygw Sep 27 '18 at 18:30
  • @DavidSchwartz that's what I'm trying to do, hence why I asked. – Mason penguin Holder Sep 27 '18 at 18:38
  • This is a misunderstanding of c++ fundamental types. `int` and `char` are integral types. They have a single distinct value at a given time, they cannot represent a set of values of their type. Here `vowel` is `a`. The trailing character literals do not have any effect on the program. – François Andrieux Sep 27 '18 at 18:38
  • 4
    @spartygw New programmers should learn C++ from a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). You can't learn C++ by coding randomly. – Algirdas Preidžius Sep 27 '18 at 18:39
  • `var` is an `int`, so `std::cin` will try to parse user input as a number. Alphabet characters like `'a'` and `'b'` will fail to be converted to `int`. You would want `var` to be a `char` so that the standard input knows to expect character input data. – François Andrieux Sep 27 '18 at 18:40
  • `==` can't generally be used to check if a set contains a value. You would first need to make `vowel` and `constant` collections of characters (I'd recommend `std::array` or `std::vector`) and then use standard algorithms like `std::find` to see if the collection contains the character. – François Andrieux Sep 27 '18 at 18:41
  • 1
    @AlgirdasPreidžius it's an assignment for class, my professor has a thick Iranian accent. I can't understand a word he says half of them time. I have a book, but it is shit and doesn't cover this kind of stuff. – Mason penguin Holder Sep 27 '18 at 18:41
  • @MasonpenguinHolder "_doesn't cover this kind of stuff._" What kind of stuff are you talking about? This kind of code, that you shown, shows a lack of fundamental understanding about C++. You need to learn the fundamentals first, before trying to solve any kind of problem. Did actually tried to read said book, from the very beginning? – Algirdas Preidžius Sep 27 '18 at 18:46
  • @davidbak if the standard SO way of letting the OP know that maybe his problem is covered in a different question is not welcoming enough, thats not my fault. My best well-intentioned advice I can give the OP is to read a book and learn the language. Imho part of "the problem" is that too often each single downvote or flag as possible duplicate is interpreted as "not welcoming enough". I think we can be welcoming **and** downvote poor questions at the same time. ( i didnt downvote btw) – 463035818_is_not_an_ai Sep 27 '18 at 18:54
  • Guys I didn't mean to get roasted by you. The program has to be written using only and if else statements. I know this question is probably covered somewhere else, pretty much every single question that can be asked has been asked. If you're going to downvote and get annoyed, because I'm asking an honest question after reading the portion assigned by my prof. and trying to do the assigned code. I'm sorry, but that's not my fault. – Mason penguin Holder Sep 27 '18 at 19:01
  • 1
    1) "_pretty much every single question that can be asked has been asked._" No, not really. If every question, that can be asked, is answered, that implies that every problem is solved. Which isn't the case. 2) Did you take the [tour], and read through [ask], and [help]? If you did, you would know, that asking question on SO is meant to be the last thing you do, after doing research on your own. Not the very first thing you do. "_If you're going to downvote and get annoyed_" The fact that you take downvotes personally, and think that we are annoyed, is not our problem. – Algirdas Preidžius Sep 27 '18 at 19:08
  • @user463035818 - I thought your comment (now gone) was just fine! it pointed to exactly the advice the OP needed. Unfortunately, he doesn't agree and is now blaming us. C'est la vie. – davidbak Sep 27 '18 at 19:13
  • @davidbak well then it was a misunderstanding. It was just the autogenerated comment that appears when you flag a question as duplicate. It eventually disappears once the question is marked as duplicate, which happened and then the question got reopened again. Actually all pretty normal course of action that has nothing to do with roasting or annoyance. I guess I just had my sensitive 5 minutes when i wrote that last comment ;) – 463035818_is_not_an_ai Sep 27 '18 at 19:19

1 Answers1

3

This code is completely wrong for what you are attempting. You are reading input as an integer instead of as a character. You are not initializing the vowel and consonant variables correctly, and not comparing var to them correctly. You are not checking for input errors. You are not handling upper-case letters.

Try something more like this instead instead:

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

int main() {
    cout << "input a single letter";
    char var; 
    if (cin >> var) {
        var = (char) ::tolower( (unsigned char)var );
        if ((var >= 'a') && (var <= 'z')) {
            if ((var == 'a') || (var == 'e') || (var == 'i') || (var == 'o') || (var == 'u')) {
                cout << "vowel";
            } else {
                cout << "consonant";
            }

            /* alternatively:
            switch (var) {
                case 'a':
                case 'e':
                case 'i':
                case 'o':
                case 'u':
                    cout << "vowel";
                    break;

                default:
                    cout << "consonant";
                    break;
            }
            */
        }
        else {
            cout << "Error";
        }
    }
    else {
        cout << "Input Error";
    }
    return 0;
}

Alternatively:

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

const string vowels = "aeiou";
const string consonants = "bcdfghjklmnpqrstvwxyz";

int main() {
    cout << "input a single letter";
    char var; 
    if (cin >> var) {
        var = (char) ::tolower( (unsigned char)var );
        if (vowels.find(var) != string::npos) {
            cout << "vowel";
        } else if (consonant.find(var) != string::npos) {
            cout << "consonant";
        } else {
            cout << "Error";
        }
    }
    else {
        cout << "Input Error";
    }
    return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770