Caps

Is there a way that whether user input "a" in small caps or large caps in console but program runs it as follows instead of writing "a" with both small and large caps in program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
using namespace std;
int main()
{
	string alp;

	cout << "Enter an alphabet: ";
	cin >> alp;
// Referring to below command
	if (alp == "a" ||  alp == "e" || alp == "i" ||  alp == "o" || alp == "u" ||
	    alp == "A" ||  alp == "E" || alp == "I" ||  alp == "O" || alp == "U")
	{
		cout << alp << " is vowel.";
	}
	else
	{
		cout << alp << " is consonant."; 
	}

	cout << endl << endl;
}
Last edited on
You can use toupper() to convert to uppercase and tolower() to convert to lower case. You need to #include <cctype> at the beginning. Note it works on only a single char, not a string.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

int main()
{
	char alp;

	cout << "Enter an alphabet: ";
	cin >> alp;

	alp = tolower(alp);

	if (alp == 'a' || alp == 'e' || alp == 'i' || alp == 'o' || alp == 'u')
	{
		cout << alp << " is vowel.";
	} else
	{
		cout << alp << " is consonant.";
	}

	cout << endl << endl;
}

Thanks, is there a way to get output as large caps if user input large caps alphabet.
Last edited on
OK. Consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

int main()
{
	char inp_alp;

	cout << "Enter a letter: ";
	cin >> inp_alp;

	const char alp = tolower(inp_alp);

	if (alp == 'a' || alp == 'e' || alp == 'i' || alp == 'o' || alp == 'u')
	{
		cout << inp_alp << " is vowel.";
	} else
	{
		cout << inp_alp << " is consonant.";
	}

	cout << endl << endl;
}

What does this const do? I haven't studied it yet.
const just means that the value doesn't change - is constant - so the compiler can check that it doesn't. It's good practice to make any initialised variable const if the value doesn't/shouldn't change.

Don't worry about it now. You can leave out const and the program will still work ok.
How did it work? What did you change? The program you send here before is same as this one instead of adding const and changing name. How could it print large caps?
Last edited on
If you take out const, it still works but how? The program is same.
Sorry, I didn't notice. My bad. Is it because of char below?
char alp = tolower(inp_alp);
Unless you have more program to add then you can overcome the change by relocating where alp is printed out. alp gets changed as required but nobody will ever know.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;

int main()
{
    char alp;

    cout << "Enter an alphabet: ";
    cin >> alp;
    cout << alp; // <---

    alp = tolower(alp);

    if (alp == 'a' || alp == 'e' || alp == 'i' || alp == 'o' || alp == 'u')
    {
         cout << " is a vowel."; // <--
    } else
    {
        cout << " is a consonant."; // <--
    }

    cout << endl << endl;
}
Last edited on
Thanks
Might be a bit ahead of you in class but this is another way of doing the job using strings and the find function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string vowels{"aeiouAEIOU"};
    
    char letter;
    cout << "Enter a character: ";
    cin >> letter;
    
    size_t found = vowels.find(letter);
    
    cout << letter;
    if (found != string::npos)
        cout << " is a vowel\n";
    else
        cout << " is a consonant\n";
    
    return 0;
}
I haven't studied a lot of these yet. But thanks.
Topic archived. No new replies allowed.