14 Replies - 10707 Views - Last Post: 17 March 2011 - 03:14 AM Rate Topic: -----

#1 dhuzie3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 31-January 10

Help outputting vowels from an input character sentence

Posted 12 April 2010 - 05:45 PM

Help outing vowels from an input character sentence

Hello,

I have been asked to write a program: “Write a program that prompts the user to input a sequence of characters and outputs the number of vowels ".

Below is the code I have come up with so far, but when I run the program, it returns all characters, not just vowels.
My compiler is Bloodshed Version 4.9.9.2 Any help would be much appreciated....Thanks.

# include <iostream>
# include <string>

using namespace std;

bool yesVowel(char ch);

int main()
{
  char ch, isVowel;
  int numOfVowel;
  bool result;
  
  
  cout << "Please enter a short sentence: " << endl;
  cin.get(ch);
  
  numOfVowel = 0;
   
  while (ch != '\n')
   {
      result = yesVowel(ch);
      
      if (result)      
         numOfVowel ++;
         cin.get(ch);
   }
  
  cout << "The number of vowels found is: " << numOfVowel << endl;
   
  system("pause");
  return 0;
}  

bool yesVowel(char ch)
{
   bool yesVowel (char ch);
   ch = (char) toupper(ch);
             
   switch (ch)
   {
    case 'a': 
    case 'e': 
    case 'i':
    case 'o': 
    case 'u': 
    return true;
    break;    
    return false; 
    }  
    return ch;
    
}




Added code tags KYA

Is This A Good Question/Topic? 0
  • +

Replies To: Help outputting vowels from an input character sentence

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,142
  • Joined: 14-September 07

Re: Help outputting vowels from an input character sentence

Posted 12 April 2010 - 05:52 PM

You're inputting only a single char...why? A small test to show you one way to go about this:

bool isVowel(char c){ 
	return ((c == 'a') || (c == 'e') || (c == 'i') || (c == 'o') || (c == 'u'));
}

int main(){
	string test = "Hi! This is a sample sentence.";
	for(int i = 0; i < test.size(); i++){
		if(isVowel(test[i])){
			cout << test[i] << " ";
		}
	}
	cout << endl;
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#3 dhuzie3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 31-January 10

Re: Help outputting vowels from an input character sentence

Posted 12 April 2010 - 06:02 PM

KYA,

Thanks for the reply... I input “the sky the blue"...My expected result should be 4 but I get 15 (including white space).

I need to implement a function, which returns true if a given character is vowel and otherwise false. I MUST make sure to use a function prototype to place isVowel (..) before main ().
I had to miss class for personal reasons and really don't understand this all that well....I spoke with my teacher for an explanation but there appears to be a language barrier as English is not her 1st language.
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,142
  • Joined: 14-September 07

Re: Help outputting vowels from an input character sentence

Posted 12 April 2010 - 06:08 PM

My vowel function returns true or false based on the conditional, but we can shape it to your switch:

bool isVowel(char); //prototype

int main(){
	string test = "the sky the blue";
	for(int i = 0; i < test.size(); i++){
		if(isVowel(test[i])){
			cout << test[i] << " ";
		}
	}
	cout << endl;
	return 0;
}

bool isVowel(char c){ 
	//only handles lower case, feel free to add upper 
	switch(c){
		case 'a':
		case 'e':
		case 'i':
		case 'o':
		case 'u':
			return true;
		default:
			return false;
	}
}




It's just a different way to represent the same thing. As for your issue, you need to post your updated code. You main problem (with your original code) is that you only input a single character (or at least, that's all that is being stored) and you're trying to treat it as a char[]/string.
Was This Post Helpful? 0
  • +
  • -

#5 dhuzie3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 31-January 10

Re: Help outputting vowels from an input character sentence

Posted 12 April 2010 - 06:11 PM

Great, Thanks for the help !!!!!




View PostKYA, on 12 April 2010 - 05:08 PM, said:

My vowel function returns true or false based on the conditional, but we can shape it to your switch:

bool isVowel(char); //prototype

int main(){
	string test = "the sky the blue";
	for(int i = 0; i < test.size(); i++){
		if(isVowel(test[i])){
			cout << test[i] << " ";
		}
	}
	cout << endl;
	return 0;
}

bool isVowel(char c){ 
	//only handles lower case, feel free to add upper 
	switch(c){
		case 'a':
		case 'e':
		case 'i':
		case 'o':
		case 'u':
			return true;
		default:
			return false;
	}
}




It's just a different way to represent the same thing. As for your issue, you need to post your updated code. You main problem (with your original code) is that you only input a single character (or at least, that's all that is being stored) and you're trying to treat it as a char[]/string.

Was This Post Helpful? 0
  • +
  • -

#6 Xyrdreas  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 10-March 11

Re: Help outputting vowels from an input character sentence

Posted 10 March 2011 - 10:42 PM

Hello, I am new to posting, though I admit to trolling the message boards looking for help. I am on the same problem, and need help pinpointing my problem.

Keep in mind I am an extremely novice programmer, and only trying to get through this one semester, so if you can keep it as layman as possible :)
I have 2 problems.
1) I have no idea what I am doing
2) The program, when trying to compile using Visual Studio, returns an error stating "subscript requires an array or pointer type."


#include <iostream>
#include <string>
#include <iomanip>
#include <ostream>

using namespace std;

     bool isVowel(string entry);
int main()
{
     char entry;
     int num;

     cout << "enter a sentence: ";
     cin >> entry;
     cout << endl;
     entry = isVowel(entry[x]);
     cout << "The number of vowels is:" << num << endl;
     return 0;
}
     bool isVowel (string entry);
     {
     string x = num++ 
     string y = ""
     if ((entry == 'a') || (entry == 'e') || (entry == 'i') || (entry == 'o') || (entry == 'u'))
     return x;
     else
     return y;
}


This post has been edited by Xyrdreas: 10 March 2011 - 10:50 PM

Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6064
  • View blog
  • Posts: 23,520
  • Joined: 23-August 08

Re: Help outputting vowels from an input character sentence

Posted 11 March 2011 - 05:39 AM

This code smells of bad copy/paste or just throwing a bunch of code down without thinking about it. Sorry, but this is 90% wrong. Did you even read and try to understand the previous posts in this topic?

char entry;

That's only a single character.

cout << "enter a sentence: ";
cin >> entry;


You're trying to use it as a string when you pass it to your function. If you want a string variable, use it. If you need to read an entire sentence, you'll need to use getline().

entry = isVowel(entry[x]);

Where did x come from? Whay are you using a single char variable like it's an array? Why are you assigning a bool return from the function to the char variable entry? Don't you need a loop to go through each character in the entered string?

bool isVowel (string entry);
{

Lose the semi-colon at the end of the function definition (NOT the function prototype at the top).

string x = num++
string y = ""

Where did num come from, what are you trying to do there, and where are your semi-colons?

You should probably read my tutorials, linked in my signature, about functions. This is a very simple assignment:

Read sentence
For each character in sentence
  if character is vowel, increment counter
Print counter

Was This Post Helpful? 0
  • +
  • -

#8 Xyrdreas  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 10-March 11

Re: Help outputting vowels from an input character sentence

Posted 11 March 2011 - 08:33 AM

View PostJackOfAllTrades, on 11 March 2011 - 05:39 AM, said:

This code smells of bad copy/paste or just throwing a bunch of code down without thinking about it. Sorry, but this is 90% wrong. Did you even read and try to understand the previous posts in this topic?


First, I am sure it's more like 86% wrong. Yes, I did try and understand all of the previous posts, and while admittedly, there is some code in there I threw in to see what would happen, and the rest is guess work on my part. Again, I have 0 experience in coding, and rather than just stealing what was above, I actually attempted to write my own code. One of the biggest problems is no matter what I read or try to understand, this is something I completely do not understand, plus meeting with the instructor once a week and balancing full time work, 17 unit hours, and trying to have 5 minutes a week with my wife and kid, maybe you can see where I am coming from.

Quote

char entry;

That's only a single character.

cout << "enter a sentence: ";
cin >> entry;


You're trying to use it as a string when you pass it to your function. If you want a string variable, use it. If you need to read an entire sentence, you'll need to use getline().

I know better than that with the char statement, stupid on my part.

entry = isVowel(entry[x]);

Where did x come from? Whay are you using a single char variable like it's an array? Why are you assigning a bool return from the function to the char variable entry? Don't you need a loop to go through each character in the entered string?

I don't know, I don't know, I don't know, and yes. This is the part of the code I threw together from other bits I read.

bool isVowel (string entry);
{

Lose the semi-colon at the end of the function definition (NOT the function prototype at the top).

string x = num++
string y = ""

Where did num come from, what are you trying to do there, and where are your semi-colons?

I think I came up with num as a place to dump the count data, and to output the results, as in:
 cout << "Number of vowels: " << num << endl;


You should probably read my tutorials, linked in my signature, about functions. This is a very simple assignment:

Read sentence
For each character in sentence
  if character is vowel, increment counter
Print counter


Consider it read, and hopefully, will get me closer to my goals. I'm not a quitter, so I am working on seeing this through to the end. Thanks for the first step.
Was This Post Helpful? 1
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6064
  • View blog
  • Posts: 23,520
  • Joined: 23-August 08

Re: Help outputting vowels from an input character sentence

Posted 11 March 2011 - 08:36 AM

I totally applaud you for not only not just copying verbatim and making an effort to do this on your own, but for not being a quitter.

Keep trying after a little more reading and we can hopefully prod you along. :)
Was This Post Helpful? 0
  • +
  • -

#10 Xyrdreas  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 10-March 11

Re: Help outputting vowels from an input character sentence

Posted 12 March 2011 - 09:11 PM

View PostJackOfAllTrades, on 11 March 2011 - 08:36 AM, said:

I totally applaud you for not only not just copying verbatim and making an effort to do this on your own, but for not being a quitter.

Keep trying after a little more reading and we can hopefully prod you along. :)


I've been trying and reading, and I just can't make heads or tails out of it. It's a foreign language, and I can't grasp it. I've spent the better part of a day trying to figure out how to:
1) Take the string sentence, and pass it on to the function.
2) How to write the function to count each character, and sum up the vowels
3) How to send the sum data back to the program for output.

I've tried variations of for and while loops in the main, I've tried to put the loops in the function, I've read and reread your tutorials, and it just doesn't help. I'm wishing I had taken a class that meets more regularly, and had an instructor who did a better job explaining things.

Still not giving up, here is the (what I assume) the successful part of the code:

#include <iostream>
#include <iomanip>
#include <ostream>
#include <istream>
#include <string>

using namespace std;

	string isVowel(string str);
	
int main()
{
	string str;
	int i, num;
	int num = isVowel(str);

	cout << "Enter a sentence: ";
	getline (cin,str);
	cout << endl;
	// I think we are ok up to here //
	
	


Obviously, I didn't get very far. For reference, i is what I was planning on using as a count integer in my loop (i.e. int i = num; if i < whatever; i++), and num was where I want to dump the sum data for output.

I understand the getline function. So what I would want to do at some point in what is here, is put a function call there, correct? And if it is correct, I want to pass the STRING data to the function to count the characters and return the sum, which is an integer, and introduces a whole new issue.

I think I understand the concept, but the language is a little beyond me.

Looking forward to feedback.
Was This Post Helpful? 0
  • +
  • -

#11 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1347
  • View blog
  • Posts: 4,642
  • Joined: 19-February 09

Re: Help outputting vowels from an input character sentence

Posted 12 March 2011 - 09:53 PM

A string is a sequence of characters. So a string and a char are not exactly the same.


Also here:
09	    string isVowel(string str);



The function name isVowel() suggests it is testing whether a character is a vowel or not. So it would take a character (char) as a parameter, and return a boolean (bool) value.

You say you want function (countVowels()?) that counts the number of vowels in a string. It would take a string and return a number (int).
Was This Post Helpful? 0
  • +
  • -

#12 Xyrdreas  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 10-March 11

Re: Help outputting vowels from an input character sentence

Posted 13 March 2011 - 04:41 AM

View Post#define, on 12 March 2011 - 09:53 PM, said:

A string is a sequence of characters. So a string and a char are not exactly the same.


Also here:
09	    string isVowel(string str);



The function name isVowel() suggests it is testing whether a character is a vowel or not. So it would take a character (char) as a parameter, and return a boolean (bool) value.

You say you want function (countVowels()?) that counts the number of vowels in a string. It would take a string and return a number (int).


Yes, it should be:

09            string countVowels(whatever syntax says take this string data and pass it to function)


Was This Post Helpful? 0
  • +
  • -

#13 Xyrdreas  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 10-March 11

Re: Help outputting vowels from an input character sentence

Posted 16 March 2011 - 03:35 PM

I finally got my code to work, using my original isVowel function (changing the name of course), and a suggestion of running a do...while loop, and it works! Here is my final result.

// Ted Guinness Chapter 5 Exercise 2
//

#include <iostream>
#include <iomanip>
#include <ostream>
#include <string>

using namespace std;

	bool countVowel(char entry);
	
int main()
{
	char entry;
	int num;
	
	num = 0;
	cout << "Enter a sentence (Put ! At end): ";
	do
	{
		cin >> entry;
	if (countVowel(entry) == true)
		
			num = num++;
	}	
	while 
		(entry != '!');
	cout << "The number of vowels in this sentence is: " << num << endl;
	return 0;
}
	bool countVowel(char entry)
	{
	if ((entry == 'a') || (entry == 'e') || (entry == 'i') || (entry == 'o') || (entry == 'u'))
		return true;
	else 
		return false;
	cout << endl;
	}



It actually worked! Thanks for the motivation on this one... now on to my next dilemna!
Was This Post Helpful? 0
  • +
  • -

#14 sushantmagu  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 7
  • Joined: 16-March 11

Re: Help outputting vowels from an input character sentence

Posted 16 March 2011 - 10:51 PM

easy and simple way,no functions also....

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
 clrscr();
 char ch[20];
 int i,count=0;
 gets(ch);    //inputting the sentence
 
 for(i=0;i<strlen(a);i++)
 {
  switch(ch(i))
  {
  case 'a': 
  case 'A':  
  case 'e':  
  case 'E':  
  case 'i':  
  case 'I':  
  case 'o':  
  case 'O':  
  case 'u':  
  case 'U':cout<<ch[i]<<endl; count++; break; //outputing and counting
}
}
cout<<"No of vovels ="<<conut;
getch();  
 

Was This Post Helpful? -1
  • +
  • -

#15 Xyrdreas  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 10-March 11

Re: Help outputting vowels from an input character sentence

Posted 17 March 2011 - 03:14 AM

View Postsushantmagu, on 16 March 2011 - 10:51 PM, said:

easy and simple way,no functions also....

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
 clrscr();
 char ch[20];
 int i,count=0;
 gets(ch);    //inputting the sentence
 
 for(i=0;i<strlen(a);i++)
 {
  switch(ch(i))
  {
  case 'a': 
  case 'A':  
  case 'e':  
  case 'E':  
  case 'i':  
  case 'I':  
  case 'o':  
  case 'O':  
  case 'u':  
  case 'U':cout<<ch[i]<<endl; count++; break; //outputing and counting
}
}
cout<<"No of vovels ="<<conut;
getch();  
 


I had to do it as a function as part of thr assignment.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1