Click here to Skip to main content
15,906,094 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey dear able to honor!

I'm writing a program of my assignment in which I'll need to show my full name and my I'd. using an array and also find vowels from my name and show them one by one in a column and also total numbers of vowels at the end. I did the first target and also done showing the total number of vowels, while I try too much to show vowels one by one with numbers like the following method but I can't anyone can help so, please...
""""""""""""""""""""""""""""
My first name is: Bilal
MY I'd is: bc1234567890
last digit of i'd is: 0

vowel no 1 is: i
vowel no 2 is: a

Total vowels in my first name: 2
"""""""""""""""""""""""""""""""""

the above text I need to show on the time of execution.

What I have tried:

//for first name vowels
	for(i1=0;i1<n1a[i1];i1++)
	{
		switch(n1a[i1])
		{
			case 'A':
			case 'a':
			case 'E':
			case 'e':
			case 'I':
			case 'i':
			case 'O':
			case 'o':
			case 'U':
			case 'u':
			counter1++;
			break;
		}
		for(y=1;y<=counter1;y++)
		{
			cout<<"Vowel "<<y<<" is: "<<n1a[i1]<<endl;
		}
	}
	cout<<endl;
	cout<<"Total Vowels Is/Are: "<<counter1<<endl;
Posted
Updated 19-Dec-21 5:32am

That code doesn't make a lot of sense - it looks like you skimmed the assignment and leapt into code without thinking about it.
Look closely at this bit for example:
C++
	for(i1=0;i1<n1a[i1];i1++)
	{
		switch(n1a[i1])
		{
...
		}
...
	}
...

So if n1a[i1] is the character that you what to check for a vowel, why are you also using it to see if you should end the loop? WHen exactly to you expect the index to exceed the value of a chartater? Especially when the value of 'u' is actually 117? You'd need a pretty large name there ...

Do yourself a favour, and throw all of that away, sit down and think about what you want to achieve before you start to code. This may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
You can do it by just moving where you display the vowels. I would also use the toupper (C++ Reference)[^], function to make them upper case to simplify the logic. Here's how that would look :
C++
// loop while character is not null
for( i1 = 0; n1a[i1] != 0; i1++ )
{
    char upcase = toupper( n1a[i1] );
    switch( upcase )
    {
        case 'A':
        case 'E':
        case 'I':
        case 'O':
        case 'U':
            ++counter1;
            cout << "Vowel no. "<< counter1 <<" is: " << n1a[i1] << endl;
            break;
    }
}
cout << endl;
cout << "Total Vowels Is/Are: "<< counter1 << endl;
 
Share this answer
 
Comments
0x01AA 19-Dec-21 11:44am    
Yes, toupper makes it more simple. No, the for loop is same wrong like the one from OP. No vote from my side 5

[Edit]
See message to me in below comment which I think is addressed to you
Mehboob Shaukat 19-Dec-21 12:34pm    
hey sir!
Thank you! so much for this appreciation. This is really helpful for me. kindly briefly explain or share a readable link.
I've one more question that is, I want to print vowels for second name same as like this, I use this code by doing simple copy and doing some common changes but I want to start second name vowels from where first name vowels left this code is very helpful and useful but this start again from 1 but I want to start from a left number like if first name has 3 vowels and the last line is "vowel no 3: O", so second name vowels must be started from "vowel no 4: A" but this start like this "vowel no 1: A", if it is possible please answer me.
0x01AA 19-Dec-21 12:36pm    
I think you like to address this to @Rick-York
Mehboob Shaukat 19-Dec-21 12:47pm    
Okay, sir! but any solution for the second problem I mean, I want to print vowels for second name same as like this, I use this code by doing simple copy and doing some common changes but I want to start second name vowels from where first name vowels left this code is very helpful and useful but this start again from 1 but I want to start from a left number like if first name has 3 vowels and the last line is "vowel no 3: O", so second name vowels must be started from "vowel no 4: A" but this start like this "vowel no 1: A", if it is possible please answer me.
Rick York 19-Dec-21 14:24pm    
Change where you start and end the loop then. You could make this a separate function and pass in the string to check. The loop can stop when you hit a space or a null and then start again after the space. That's why it might be easier if that logic was in a separate function that you pass the address of the string to. That allows you to put the logic for separating the words outside the loop.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900