2

i made this code to print out vowels from array. i have wrote code before but back then only error was that it was printing whole array instead of just vowels. i wrote it today again i dont think that 2nd time i have made any change in code than the previous one(if i made i cant see) but now it takes blank input of array and ends the program. can u help me out? here is the code:

int size=0;
    cout<<"Enter the size of array=";
    cin>>size;
    char arr[size];
    cout<<"Enter the array=";
    for(int i=0;arr[i]='\0';i++){
        cin>>arr[i];
    }
    for(int i=0;arr[i]!='\0';i++)
    {
        if(arr[i]=='a'||'e'||'i'||'o'||'u'||'A'||'E'||'I'||'O'||'U')
        {
            cout<<arr[i];
        }
    }
tadman
  • 208,517
  • 23
  • 234
  • 262
  • 1
    Tip: In C++ use `std::string` whenever possible, especially for input buffers. `std::getline()` can help here. – tadman Mar 19 '21 at 08:23
  • C++ Variable Length Arrays are a non-standard feature. If you need one, use `std::vector` or `std::string`. – tadman Mar 19 '21 at 08:23
  • `for(int i=0;arr[i]='\0';i++){` is expecting your input to be a NUL byte? – tadman Mar 19 '21 at 08:24
  • 2
    There's a *lot* going wrong here, probably because of gaps in your knowledge of C++. This is usually a sign you need a better [reference](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to work from. – tadman Mar 19 '21 at 08:25
  • [Operator precedence rules](https://en.cppreference.com/w/cpp/language/operator_precedence) dictate that `if (x == y || z)` doesn't mean "`x` is either `y` or `z`" it means "either `x` is `y`, or `z` is truthful". If you're testing if a value is in a set of things, either use a look-up table, a "character present in string" function, or `switch`. Easy way: `std::string vowels = "aeioU.."; vowels.find(c)` where `c` is the character you're looking for. – tadman Mar 19 '21 at 08:26
  • 2
    thnku so much guys actually im a begineer i have made alot of syntax error:/ – potato_girl Mar 19 '21 at 09:06
  • Nothing at all wrong with learning! – tadman Mar 19 '21 at 09:07
  • also actually im not supposed to use functions and string right now – potato_girl Mar 19 '21 at 09:09

1 Answers1

3

Try this:

#include <iostream>
using namespace std;

int main()
{
    int size = 0;
    cout << "Enter the size of array=";
    cin >> size;
    char *arr= new char[size]; /*Requires a pointer to declare a dynamic array or a vector*/
    cout << "Enter the letters array " <<endl;
    for (int i = 0; i < size; i++) {
        cout << i + 1 << " : ";
        cin >> arr[i];
    }
    for (int i = 0; i < size; i++)
    {
        switch (arr[i])
        {   case 'a' :
            case 'A':
            case 'e':
            case 'E':
            case 'i':
            case 'I':
            case 'o':
            case 'O':
            case 'u':
            case 'U':
                cout << arr[i];
            break;
        default:
            break;
        }
    }
}
Zach_Mose
  • 357
  • 3
  • 7
  • Pleasure, don't forget to accept the answer as correct – Zach_Mose Mar 19 '21 at 09:22
  • but can u also tell me whats wrong with how i gave the condition in if else – potato_girl Mar 19 '21 at 09:22
  • As @tadman explained earlier, ```if(arr[i]=='a'||'e'||'i')``` is incorrect, it should be ```if(arr[i]=='a' || arr[i] == 'e' || arr[i] == 'i')``` But generally the ```switch``` is a good solution – Zach_Mose Mar 19 '21 at 09:25
  • `if (thing == this || that || theOther)` doesn't quite work like that. You unfortunately can't offer a list of things to compare with, you need to specify the whole comparison each time. `if (thing == this || thing == that || thing == theOther)`. What's actually going on is it's checking if `arr[i]=='a'` and if it's not, it's checking if `'e'`. Not like `if (arr[i]=='e')` but literally `if ('e')` which is always true. – Ashley Miller Mar 19 '21 at 09:32