-
November 16th, 2012, 01:13 AM
#1
Can someone please help me with this coding?? Plz plz need help!!!
CAN SOMEONE PLEASE HELP ME WITH THIS CODING??
I have to finish this code for homework but I have no idea at all?? could anyone help me?
The directions are:
1.) A function function that takes, as an in parameter, a single character. If this character
is a vowel (a, e, i, o or u) it will return true. Otherwise it will return false. You must
deal with the fact that these vowels could be uppercase or lowercase. To do this add,
#include <cctype> to the top of your program. Then, you may use the function
tolower with prototype, char tolower(char), to convert a character to lowercase
before deciding if it is a vowel or not.
2.)
A function that takes a file name as an in parameter and four in/out parameters and
returns a Boolean. The in/out parameters are four counters, the number of vowels, the
number of white space characters, the number of digits, and the number of symbols.
This function will open the fille for reading if the open fails, the function should return
false. Otherwise, the function will look at every character in the file and update the
appropriate counter. When the end-of-fille (EOF) is encountered, the function should
close the le and return true to the caller. You will want to use two functions from
the cctype library, isdigit and isspace. The function isdigit, with prototype
bool isdigit(char), takes a character and returns trueif the character is a digit and
false otherwise. The function isspace, with prototype bool isspace(char), takes
a character and returns true if the character is a white space character and false
otherwise.
I have the following program so far FINISHED for question 1(I am 100%% sure it is right) but I do not understand how to do Question 2 AT ALL PLEASE HELP:
#include <iostream>
#include <string>
using namespace std;
// Prototypes
void incrementStatistics(string, int&, int&, int&);
void displayStatistics(int, int, int);
bool isVowel(char);
int main()
{
string word; // Input: the word entered by the user.
int numVowels = 0; // Holds the number of vowels.
int numConsonants = 0; // Holds the number of consonants.
int numLetters = 0; // Holds the total number of letters.
bool doneWithInput = false; // Flag to control when the loop is done.
while (!doneWithInput)
{
// Prompt for a word
cout << "Please enter a word (\"bye\" to exit): ";
cin >> word;
// If the word is "bye" stop updating the statistics. Othewise,
// update the statistics using the current word.
if (word == "bye")
doneWithInput = true;
else
incrementStatistics(word, numVowels, numConsonants, numLetters);
}
// Dispaly the statistics
displayStatistics(numVowels, numConsonants, numLetters);
return 0;
}
// This function will increment the statics using the in/out parameters numVowels,
// numConsonants, and numLetters.
// Pre: numVowels >= 0, numConsonants >= 0 and numLetters >= 0
// Post: numVowels, numConsonants and numLetters are incremented appropriately
void incrementStatistics(string word, int& numVowels, int& numConsonants, int& numLetters)
{
int currPosition; // The current position in the string.
// Increment the number of letters by the number of letters in the string.
numLetters += word.length();
// Count the number of vowels and the number of consonants. We will use
// a for loop to test each character in the string.
for (currPosition = 0; currPosition < word.length(); currPosition++)
{
if (isVowel(word.at(currPosition)))
numVowels++;
else
numConsonants++;
}
}
// This function will deteremine if a letter is a vowel or not.
// Pre: a valid character
// Post: true if the letter is a vowel false otherwise
bool isVowel(char letter)
{
// convert letter to a lowercase letter
letter = tolower(letter);
if (letter == 'a' || letter == 'e' || letter == 'i' || letter == 'o' || letter == 'u')
return true;
else
return false;
}
// This function will display the statistics nicely to the screen
// Pre: numVowels >= 0, numConsonants >= 0, and numLetters >= 0.
// Post: none.
void displayStatistics(int numVowels, int numConsonants, int numLetters)
{
cout << endl;
cout << " Vowels: " << numVowels << endl;
cout << " Consonants: " << numConsonants << endl;
cout << "Total Letters: " << numLetters << endl;
}
Please Help I know it could be a lot of work but I really need help right now... thanks
-
November 16th, 2012, 04:30 AM
#2
Re: Can someone please help me with this coding?? Plz plz need help!!!
This is your homework - so why don't you do it yourself?
Anyway, just some hints for you to carry on (Q 2) -
Use -
#include <iostream>
#include <fstream>
sample code to read files -
Code:
ifstream fin;
fin.open("my-input-file.txt", ios::in);
char my_character ;
int number_of_lines = 0;
while (!fin.eof() ) {
fin.get(my_character);
cout << my_character;
if (my_character == '\n'){
++number_of_lines;
}
}
cout << "NUMBER OF LINES: " << number_of_lines << endl;
or you can also use -
Code:
int v=0;
fstream file_op("c:\\proj3cpp.txt",ios::in);
while(!file_op.eof())
{
file_op.getline(str,3000);
cout <<str;
}
use str[] for every char read -
while(str[v] != '\0') {
<your code goes here>
}
Also, for your first Question, the isVowel() function, here is another approach -
Code:
bool isVowel(char letter)
{
switch(letter){
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U': return true;
default: return false;
}
}
** Please use
Code:
<your code goes here>
to preserve the code indentation and it's true sence.
-
November 18th, 2012, 12:27 PM
#3
Re: Can someone please help me with this coding?? Plz plz need help!!!
Hello, thank you for helping me get started but I honestly have no idea how to start this.. could you or anyone help??
-
November 18th, 2012, 01:29 PM
#4
Re: Can someone please help me with this coding?? Plz plz need help!!!
Originally Posted by sunghun94
I honestly have no idea how to start this..
You always have to start with identifying the parts in requirements that you 1) understand completely (and you definitely did something similar in the past), 2) understand more or less (you did something but not exactly like that), 3) you heard something about but not sure of, and finally 4) you have no idea what this is about.
You pay special attention to nouns (which are going to be your data) and verbs (which are going to be your actions).
You pay some very special attention to what is expected as a result: for example, in your case you are expected to write a function that takes some parameters and returns some value of predefined type.
After having this put in list you just go top to down, complete your understanding in all parts (be prepared to make an elementary app on each problem to confirm the understanding to yourself), and ultimately put all together in one solution.
So, would you be so kind to give us the list with qualification of the parts. Something like this:
- function: I definitely know what is C++ function
- function that takes five parameters: I did write functions before, with number of parameters 0 to 3
- etc.
Best regards,
Igor
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|