Vivek Kapoor
Vivek Kapoor

Reputation: 91

Why is my string only accepting three characters?

I'm trying to check if all the vowels are present in a given string or not. If they are present in the string, then the output should be "YES", otherwise it should be "NO". I have also printed the values of a, e, i, o, and u (counter variables), but the problem is that this string accepts only the first 3 letters, and not more than that.

Here if I enter the string as "aeiouqw", the output is:

a:1
e:1
i:1
o:0
u:0
NO

#include<iostream>
#include<string.h>
#include<stdio.h>

using namespace std;

int main()
{
    int j=0;
    int a,e,i,o,u;
    a=e=i=o=u=0;
    string s;
    cout<<"Enter the string:";
    do
    {
        cin>>s[j++];
    }while(s[j]!='\0');

    int k=0;

    while(s[k]!='\0')
    {
        switch(s[k++])
        {
        case 'a':a++;
            break;
        case 'e':e++;
             break;
        case 'i':i++;
             break;
        case 'o':o++;
             break;
        case 'u':u++;
             break;
        default:break;
        }
    }
    cout<<"a: "<<a<<endl;
    cout<<"e: "<<e<<endl;
    cout<<"i: "<<i<<endl;
    cout<<"o: "<<o<<endl;
    cout<<"u: "<<u<<endl;

    if((a>0)&&(e>0)&&(i>0)&&(o>0)&&(u>0))
    {cout<<"YES";}
    else
    {cout<<"NO";}
    return 0;
}

Upvotes: 0

Views: 148

Answers (3)

πάντα ῥεῖ
πάντα ῥεῖ

Reputation: 1

As for your question title:

Why is my string only accepting three characters?

That's because you are calling undefined behavior using the statements

do
{
    cin>>s[j++]; // <<<< UB here ...
} while(s[j]!='\0');  // <<< ... and here

The string s; variable isn't initialized (size is still 0 at this point), and dereferencing an arbitrary position like s[j++] isn't defined. Anything can happen, including the behavior you experience, or your hard disk gets formatted, or nasal demons flying out from your nostrils.


The simplest way to fix that is just replace your whole loop with

cin>>s;

or

getline(cin, s);

if you want to accept input containing any whitespace characters.

Both statements mentioned above will resize your string s; variable properly.

Upvotes: 3

honk
honk

Reputation: 9743

You are reading the input the wrong way. You should use the getline() function instead of your do/while loop (see article: Using cin to get user input):

cout<<"Enter the string:";    
getline(cin, s);

This solves the problem with your uninitialized string s that you are accessing in your do/while loop, because getline() replaces any content in s by the newly extracted sequence from cin. For the getline() function you have to add the following include:

#include<stdio.h>

Then you will get the following output for your test string:

Enter the string:aeiouqw
a: 1
e: 1
i: 1
o: 1
u: 1
YES

Upvotes: 3

amallard
amallard

Reputation: 1229

your code is rather confusing. simplify it by first using better variable names and more whitespace to separate different sections. it all helps for readability.

also it would be easier to just iterate through each char in the string, and test if it is a vowel, if so accumulate the counter.

#include<iostream>
#include<string>
#include<stdio.h>

using namespace std;

int main()
{
    int a, e, i, o, u;
    a = e = i = o = u = 0;

    string str;
    cout << "Enter the string: ";

    cin >> str;

    for (int index = 0; index < str.length(); index++) {
        if (str[index] == 'a') {
            a++;
        }
        else if (str[index] == 'e') {
            e++;
        }
        else if (str[index] == 'i') {
            i++;
        }
        else if (str[index] == 'o') {
            o++;
        }
        else if (str[index] == 'u') {
            u++;
        }
    }


    cout << "a: " << a << endl;
    cout << "e: " << e << endl;
    cout << "i: " << i << endl;
    cout << "o: " << o << endl;
    cout << "u: " << u << endl;

    if ((a>0) && (e>0) && (i>0) && (o>0) && (u>0))
    {
        cout << "YES" << endl;
    }
    else
    {
        cout << "NO" << endl;
    }

    system("pause");
    return 0;
}

Upvotes: 1

Related Questions