Thursday, July 30, 2009

Help with C++ code?

Im supposed to take a sentence ending with a '.' and determine which words have 3 or more different vowels. It should also use the isVowel function(which is below). I can determine how many vowels are in the string by the following code however i have no idea how to split up the string into words and then analyze each word nor how to determine if it has 3 DIFFERENT vowels. The program must use files. Can anyone help me. Im new to c++. Thnx.





#include %26lt;string%26gt;


#include %26lt;fstream%26gt;


using namespace std;





int isVowel(char letter)//required


{


switch(letter)


{


case 'a':


case 'e':


case 'i':


case 'o':


case 'u': return 1;break;


default: return 0;break;


}


}





void main()


{


string line;


int count=0, vowel;


char currentLetter;





ifstream data("input.dat");


ofstream results("output.dat");





while ( (currentLetter = data.get()) != EOF)


{


line += currentLetter;





vowel = isVowel(currentLetter);


if(vowel)


count++;


}





results %26lt;%26lt; line;


results %26lt;%26lt; count;


}

Help with C++ code?
Let's break it down into even smaller pieces. Splitting up a sentence into separate words can be done by writing a loop that looks for the space character.





At the moment you have a loop that reads a single character from input and checks to see if it's a vowel. You could modify this so that you read characters and store them in an array until you hit a space. When you hit the space, you process the word that is now stored in the array.





Checking for three different vowels could be done by building a very simple little data structure that records whether each possible vowel has occurred in a word. From this, it will be easy to tell if at least three different vowels have occurred.





Okay, I haven't given all the details because this is obviously an assignment, but hopefully this will get you started. Also, you should begin to see the value of breaking problems down into smaller pieces.
Reply:%26gt; Im supposed to take a sentence ending with a '.' and determine which words have 3 or more different vowels.


Use [cin.getline] function with the '.' as delimiter.


Then make a custom function to break up the sentence into words. This is accomplished using a 2d array of strings.





%26gt; It should also use the isVowel function(which is below). I can determine how many vowels are in the string by the following code however i have no idea how to split up the string into words


I recommend you go through a proper textbook [Herbert Schildt's C++: The Complete Reference is a very good and comprehensive book, and the chapter on strings is EXACTLY what you need.]





%26gt; and then analyze each word nor how to determine if it has 3 DIFFERENT vowels. The program must use files. Can anyone help me. Im new to c++. Thnx.


Once again, first you have to be thorough with the syntax. Then go through the problem in logical steps. Each 'step' becomes a function for you. [This is not Object Oriented Programming, but it doesn't really matter for this particular problem]


No comments:

Post a Comment